2017-02-16 236 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