JTable 不显示在 Jframe 中

JTable is not displayed in Jframe

我无法在 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();}
    });
}
DefaultTableModel model = new DefaultTableModel();

您在 TableModel 中有 0 列。显示的列是由 TableModel 中的列决定的,而不是每行中的项目数。所以添加数据行没有效果。

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

谢谢!我没有正确初始化 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;
}