如何在JTable中显示Object数组?

时间:2017-06-10 06:26:42

标签: java swing jtable tablemodel

这是我正在使用的代码但是当我尝试打印JTable对象时,数据不会显示在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); } 中。哪个表的模型属性可以使用打印对象数组值以及如何使用?

1 4 5 6 8 10.4

1 个答案:

答案 0 :(得分:0)

我在这样的JTable中填充了数据。您可能想尝试使其适应您的代码。变量和东西都是西班牙语,只需用你需要的东西替换它们。就我而言,它是一个包含4列的表格,代表日期,分数,持续时间和最大观众。

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

    }