gpt4 book ai didi

java - 如何重新绘制我的 JTable

转载 作者:行者123 更新时间:2023-12-01 13:10:02 25 4
gpt4 key购买 nike

所以我有一个带有 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();
}
}
}

最佳答案

不是最好的方法,但它会让你越过界限......

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

JTable table = askData();
scrollPane.setViewportView(table);
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 JTable askData() {
DataAsker asker = null;
try {
asker = new AndmeKysija(ID);
} catch (SQLException e) {
e.printStackTrace();
}
return asker.giveTable();
}

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);
JTable table = askData();
scrollPane.setViewportView(table);
}
}

您应该做的是根据 AndmeKysija 的结果创建一个新的 TableModelAndmeKysija 应该有 UI 的想法或概念。然后,您只需使用 JTable#setModel 来更新 View ...

Swing 使用 Model-View-Controller 的变体范例

关于java - 如何重新绘制我的 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22953913/

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