[英]Jtable is not adding row in runtime
我试图在运行时添加一行,但它一直在中断。 这只是一个让一切正常工作的简单测试。 它只会在将所有内容添加到表格后才会显示所有内容,但在此之前会显示一个黑色窗口。
public static void main(String[] args) throws IOException, InterruptedException{
JFrame dashboard = new JFrame("Dashboard");
dashboard.setVisible(true);
dashboard.setTitle("Dashboard Information");
dashboard.setBounds((960 - 250), (540 - 250), 500, 500);
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
dashboard.add(new JScrollPane(table));
model.addColumn("Col2");
model.addColumn("Col1");
model.addColumn("Col3");
model.addRow(new Object[] {"test", 1, "test"});
for (int i = 0; i < 10; i++) {
model.addRow(new Object[] {"test2", 2, "test2"});
Thread.sleep(100);
}
dashboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dashboard.pack();
}
Jframe
仅在我们添加行Jframe
被打包,这意味着该表不会出现,因为我们还没有完成它! 将dashboard.pack()
或Jframe.pack()
到循环上方和dashboard.add(new JScrollPane(table));
下方dashboard.add(new JScrollPane(table));
首先使表存在,然后在运行时正确添加行。
public static void main(String[] args) throws IOException, InterruptedException{
JFrame dashboard = new JFrame("Dashboard");
dashboard.setVisible(true);
dashboard.setTitle("Dashboard Information");
dashboard.setBounds((960 - 250), (540 - 250), 500, 500);
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
dashboard.add(new JScrollPane(table));
//move lines to here
dashboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dashboard.pack();
model.addColumn("Col2");
model.addColumn("Col1");
model.addColumn("Col3");
model.addRow(new Object[] {"test", 1, "test"});
for (int i = 0; i < 10; i++) {
model.addRow(new Object[] {"test2", 2, "test2"});
Thread.sleep(100);
}
//put these lines beofore we add rows to the table
//dashboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//dashboard.pack();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.