0

This is my code which I am using but when I am trying to print dataArray object, then data is not show in JTable. Which model properties of table to print Object array values can used and how?

public class ShowAddressForm extends javax.swing.JFrame {

    Object data[][];
    Object dataArray[][];
    int count = 0;
    String st;

    public ShowAddressForm(String fname , String str) {
        super(fname);
        st = str;
        initComponents();

        fillTable();
    }

    public void fillTable()
    {
int count = 0;
        String str;

        try
        {
            BufferedReader br = new BufferedReader(new FileReader("D:\\JavaPrograms\\Contact Management System\\InputFiles\\AddressFile"));

            while((str = br.readLine()) != null)
            {
                count++;
            }
            br.close();
        } catch (Exception e)
        {

        }

        Object id;
        Object name;

        data = new Object[count][7];

        int i = 0 , j = 0 , m;

        try
        {
            BufferedReader buffrea = new BufferedReader(new FileReader("D:\\JavaPrograms\\Contact Management System\\InputFiles\\AddressFile"));

            while((str = buffrea.readLine()) != null)
            {
                StringTokenizer token = new StringTokenizer(str , "*");

                int n = token.countTokens();

                id = token.nextElement();
                name = token.nextElement();

                String strNameLow = name.toString().toLowerCase();
                String strNameUpp = name.toString().toUpperCase();

                if(strNameLow.startsWith(st.toLowerCase()) || strNameUpp.startsWith(st.toUpperCase()))
                {
                    data[i][0] = id;
                    data[i][1] = name;

                    for(j = 2 ; j < n ; j++)
                    {
                        data[i][j] = token.nextElement();
                    }
                    i = i + 1;
                }
            }

            buffrea.close();
        } catch(IOException ioe){
            System.out.println("Error : " + ioe.toString());
        }

        dataArray = new Object[i][7];
        for(int a = 0 ; a < i ; a++)
        {
            for(int b = 0 ; b < 7 ; b++)
            {
                dataArray[a][b] = data[a][b];
            }
        }

        //Here is the code to print dataArray object which i used but it is not working, when i am run my program it is print "[Ljava.lang.Object;@1cc2e30" in table's first cell[0][0] position  

        DefaultTableModel model = (DefaultTableModel)this.data_table.getModel();
        model.addRow(dataArray);
}
2
  • 1
    1) For better help sooner, post a minimal reproducible example or Short, Self Contained, Correct Example. Use some hard coded data to replace the text file. 2) "..in java in Netbeans" The solutionis to be found in Java code, not the IDE, so 'Netbeans' is irrelevant. 3) catch (Exception e) { } Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call Throwable.printStackTrace(). Commented Jun 10, 2017 at 6:29
  • You never add the JTable/JScrollPane to the frame Commented Jun 10, 2017 at 13:56

1 Answer 1

1

I filled data in a JTable like this. You might want to give it a try adapting it to your code. Variable and stuff are in spanish, just replace them with what you need. In my case it's a table with 4 columns representing a date, a score, duration and max viewers.

    private void fillJTable(){
        //creating data to add into the JTable. Here you might want to import your proper data from elsewhere
        Date date = new Date();

        UserReplay rep1 = new UserReplay(date, 12, 13,14);
        UserReplay rep2 = new UserReplay(date, 2,34,5);

        ArrayList<UserReplay> usuaris = new ArrayList<>();
        usuaris.add(rep1);
        usuaris.add(rep2);
//----Filling Jtable------
        DefaultTableModel model = (DefaultTableModel) view.getTable().getModel();
        model.addColumn("Fecha");
        model.addColumn("Puntuación");
        model.addColumn("Tiempo de duración");
        model.addColumn("Pico máximo de espectadores");

        for (int i = 0; i < usuaris.size(); i++){
            Vector<Date> fecha = new Vector<>(Arrays.asList(usuaris.get(i).getDate()));
            Vector<Integer> puntuacion = new Vector<>(Arrays.asList(usuaris.get(i).getPuntuacion()));
            Vector<Integer> tiempo = new Vector<>(Arrays.asList(usuaris.get(i).getTiempo()));
            Vector<Integer> espectadors = new Vector<>(Arrays.asList(usuaris.get(i).getTiempo()));

            Vector<Object> row = new Vector<Object>();
            row.addElement(fecha.get(0));
            row.addElement(puntuacion.get(0));
            row.addElement(tiempo.get(0));
            row.addElement(espectadors.get(0));
            model.addRow(row);
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.