Yogesh Sahu
Yogesh Sahu

Reputation: 1

How to display Object array in JTable?

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);
}

Upvotes: 0

Views: 2303

Answers (1)

afarre
afarre

Reputation: 532

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);
        }

    }

Upvotes: 1

Related Questions