2017-02-16 586 views
0

我無法設法在JFrame中顯示JTable。實際上,我已經從ArrayList中提取數據並逐行填充了JTable。我已經檢查過JTable是否已填充,行數等於原始ArrayList中的行數。然而運行該功能會顯示一個空白的GUI。我實在看不出在我的代碼的問題:JTable不顯示在Jframe中

public Graphicalinterface4() { 
    //super (new GridLayout(1,0)); 

    //panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 

    getdata2 in=new getdata2(); 
    try { 
     ArrayList<Piece> test=in.getList(); 
     ArrayList<Piece> classified=in.classify(test); 
     JTable table=getlocaltable(classified); 
     table.setFillsViewportHeight(true); 
     JScrollPane scrPane=new JScrollPane(table); 
     //scrPane.setSize(800,690); 
     //scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     this.add(scrPane); 

     //scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.pack(); 
     this.setVisible(true); 

    } 
    catch (IOException ex) { 
     ex.printStackTrace(); 
    } 


} 


public JTable getlocaltable(ArrayList<Piece> in) { 


    //new Object[]{"Type","Company","reference","description","price","To order"})); 

    //DefaultListModel<Piece> testlst=new DefaultListModel<Piece>(); 
    int sz=in.size(); 
    String[] columns=new String[] { 
      "Type","Company","reference","description","price","To order" 
    }; 
    DefaultTableModel model = new DefaultTableModel(); 
    //JTable table=new JTable(null, in.toArray()); 
    for (int i=0;i<sz;i++) { 
     Piece p=in.get(i); 

     String type=p.gettype(); 
     String company=p.getasc(); 
     String reference=p.getref(); 
     String description=p.getdesc(); 
     String price=p.getprice(); 
     String image=p.getimage(); 
     System.out.println(type); 
     //DefaultTableModel model=(DefaultTableModel) table.getModel(); 
     model.addRow(new Object[]{type,company,reference,description,price,Integer.toString(0)}); 
    } 
    JTable table=new JTable(model); 
    System.out.println(table.getRowCount()); 
    return table; 
} 


static Graphicalinterface4 ssp; 

public static void main(String[] args) throws IOException { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() {ssp=new Graphicalinterface4();} 
    }); 
} 
+0

1)請學習常見的Java命名(命名約定 - 例如'EachWordUpperCaseClass','firstWordLowerCaseMethod()','firstWordLowerCaseAttribute',除非它是'UPPER_CASE_CONSTANT')並且一致地使用它。 2)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

回答

1
DefaultTableModel model = new DefaultTableModel(); 

你必須在TableModel 0列。顯示的列由TableModel中的列確定,而不是每行中的項目數。因此添加數據行不起作用。

閱讀DefaultTableModel正確的構造函數的API使用,將接受「列」變量作爲參數。

0

謝謝!我沒有正確初始化DefaultTableModel。在API中的String []和Object [] [] {}顛倒(的DefaultTableModel(字符串,對象[] [] {}),但是這對我的作品。

public JTable getlocaltable(ArrayList<Piece> in) { 


    //new Object[]{"Type","Company","reference","description","price","To order"})); 


    int sz=in.size(); 
    String[] columns=new String[] { 
      "Type","Company","reference","description","price","To order" 
    }; 
    DefaultTableModel model = new DefaultTableModel(new Object[][]{}, columns); 
    //JTable table=new JTable(model); 
    for (int i=0;i<sz;i++) { 
     Piece p=in.get(i); 

     String type=p.gettype(); 
     String company=p.getasc(); 
     String reference=p.getref(); 
     String description=p.getdesc(); 
     String price=p.getprice(); 
     String image=p.getimage(); 
     System.out.println(type); 
     //DefaultTableModel model=(DefaultTableModel) table.getModel(); 
     model.addRow(new Object[]{type,company,reference,description,price,Integer.toString(0)}); 
    } 
    JTable table=new JTable(model); 
    System.out.println(table.getRowCount()); 
    return table; 
} 
+1

'謝謝!我沒有正確初始化DefaultTableModel.' - 很高興我的建議幫助。不要忘了單擊答案旁邊的複選標記以「接受」我的答案,以便人們知道問題已解決。順便說一下,這不是最好的構造函數。您可以使用構造函數來獲取列名稱數組以及等於0的行數。重新讀取API。 – camickr