场景是我的 JFrame 上有一个名为“MainFrame”的 JTable,我可以使用名为“EditFrame”的 JDialog 编辑保存在 xml 文件中的数据。我可以通过单击“EditFrame”上的保存按钮来更改 xml 文件中的数据,但它不会重新加载“MainFrame”上的 JTable。MainFrame 的代码如下:
package ca.ism.wen.gui;
import java.awt.BorderLayout;
import java.awt.Container;
...
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class MainFrame extends JFrame implements ActionListener{
static JTable jt = null;
static List<RdpServ> rdps = null;
static Vector<String> header = null;
static Vector<Vector<String>> data = null;
static TableModel tm = null;
public MainFrame(){
header = new Vector<String>();
...
loadData();
jt = new JTable(tm);
Container con = this.getContentPane();
JScrollPane p = new JScrollPane(jt);
con.add(p);
JButton btnEdit = new JButton("Edit");
btnEdit.setActionCommand("Edit");
btnEdit.addActionListener(this);
JButton btnLogout = new JButton("Logout");
btnLogout.setActionCommand("Logout");
btnLogout.addActionListener(this);
JPanel jp = new JPanel();
jp.add(btnEdit);
jp.add(btnLogout);
con.add(jp, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,600);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("Edit")) {
EditFrame ef = new EditFrame();
ef.addWindowListener(new WindowAdapter(){
@Override
public void windowClosed(WindowEvent e) {
refresh();
}
});
}
else if (cmd.equals("Logout")) {
UserDao.signoff();
System.exit(0);
}
}
public static void refresh(){
loadData();
jt.repaint();
}
public static void loadData(){
rdps= UserDao.getRs();
data = new Vector<Vector<String>>();
for (RS rdp:rdps){
Vector<String> v = new Vector<String>();
...
data.add(v);
}
tm = new DefaultTableModel(data,header){
public boolean isCellEditable(int row, int column)
{
return false;
}
};
}
}
我在网上搜索了很多并尝试了解决方案。它们都不适用于我的代码。请帮忙。多谢你们!!!