2017-06-10 75 views
0

這是我正在使用的代碼,但是當我試圖打印dataArray對象時,則數據不會顯示在JTable中。要打印的表格的哪些模型屬性可以使用對象數組值以及如何使用?如何在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

1)爲了更好地提供幫助,請發佈[MCVE]或[簡短自包含正確示例](http://www.sscce.org/)。使用一些硬編碼的數據來替換文本文件。 2)*「..在Netbeans中的java」*解決方案可以在Java代碼中找到,而不是在IDE中,所以'Netbeans'是無關緊要的。 3)catch(Exception e){}'不要忽略異常!他們告訴我們到底出了什麼問題。除非實現日誌記錄,否則至少要調用'Throwable.printStackTrace()'。 –

+0

您永遠不會將JTable/JScrollPane添加到框架 – camickr

回答

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

    }