Jable中不显示JTable

时间:2017-02-16 18:20:46

标签: java swing jtable

我无法在JFrame中显示JTable。实际上我已从ArrayList中提取数据并逐行填充JTable。我已经检查过JTable是否已填充,行数等于原始ArrayList中的行数。然而,运行该功能显示一个空白的GUI。我真的没有在我的代码中看到问题:

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

2 个答案:

答案 0 :(得分:1)

DefaultTableModel model = new DefaultTableModel();

TableModel中有0列。显示的列由TableModel中的列确定,而不是每行中的项目数。因此,添加数据行无效。

阅读DefaultTableModel API,了解要使用的正确构造函数,它将接受“columns”变量作为参数。

答案 1 :(得分:0)

谢谢!我没有正确初始化DefaultTableModel。在API中,String []和Object [] [] {}被反转(DefaultTableModel(String,Object [] [] {}),但这适用于我。

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