【发布时间】:2014-04-09 05:51:55
【问题描述】:
所以我有一个包含 JTable 的应用程序。 JTable 位于 JScrollPane 内部,而 JScrollPane 绘制在 JFrame 上。
现在在我的应用程序中,我打开一个新窗口以向该表添加新行,然后单击按钮保存更改后,新窗口将关闭。
现在我尝试在新窗口关闭后添加这些行:
askTableInfo(); //a method to save the info in database to table and then save the table to variable 'table'
table.repaint();
scrollPane.repaint();
当然还有 repaint();通过它自己。但它似乎仍然没有更新我在 JFrame 中的表。
这可能是什么问题?
public class AppWindow extends JFrame implements ActionListener {
String user = "";
JLabel greetText = new JLabel();
JPanel panel = new JPanel();
JPanel panel2 = new JPanel(new GridLayout(1, 3));
JScrollPane scrollPane;
JTable tabel;
JButton newBook = new JButton("Add a book");
JButton deleteBook = new JButton("Remove a book");
JButton changeBook = new JButton("Change a book");
int ID;
public AppWindow(String user, int ID) {
this.ID = ID;
this.user = user;
setSize(500, 500);
setTitle("Books");
setLayout(new BorderLayout());
greetText.setText("Hi "+user+" here are your books:");
add(greetText, BorderLayout.NORTH);
askData();
panel.add(scrollPane);
add(panel, BorderLayout.CENTER);
panel2.add(newBook);
panel2.add(deleteBook);
panel2.add(changeBook);
add(paneel2, BorderLayout.SOUTH);
newBook.addActionListener(this);
setVisible(true);
}
private void askData() {
DataAsker asker = null;
try {
asker = new AndmeKysija(ID);
} catch (SQLException e) {
e.printStackTrace();
}
table = asker.giveTable();
scrollPane = new JScrollPane(tabel);
}
public static void main(String[] args){
AppWindow window = new AppWindow("Name", 2);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == newBook){
new BookAdded(ID);
panel.revalidate();
panel.add(scrollPane);
panel.repaint();
repaint();
}
}
}
【问题讨论】:
-
听起来您要更新的表格和屏幕上的表格不一样和/或表格模型的情况相同...
-
在我的“askTableInfo()”方法中,我确实有一个“scrollPane = new JScrollPane(table);”。这可能是问题所在?在这种情况下,如何使用新信息更新已经绘制好的 Scrollpane?
-
更新表模型
-
我编辑了我的原始帖子。您可以在那里看到它创建了一个新的 DataAsker,它从数据库中获取数据,然后使用数据创建表。之后,信息被保存到变量中。我认为这应该足以更新表模型?
-
我的意思是,不要。将原始表格和滚动窗格留在原处。只需创建一个新的
TableModel并将其应用于现有表或更新现有的TableModel...