gpt4 book ai didi

java - JTable 不显示在 Jframe 中

转载 作者:行者123 更新时间:2023-12-01 08:52:47 24 4
gpt4 key购买 nike

我无法在 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”变量作为参数。

关于java - JTable 不显示在 Jframe 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42281781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com