如何从不同的表单更新jtable?

时间:2013-04-03 03:20:10

标签: java swing jtable jdialog

public class A extends JInternalFrame implements ActionListener
{
   private JTable table;
   private JButton button;

   public A()
   {
      button = new JButton("Load Dialog");
      button.addActionListener(this);

      initializeTable();
   }

   public void initializeTable()
   {

      table = new JTable();
      MyTableModel mymodel = new MyTableModel();

      table.setModel(mymodel);
   }

   public void changeModel(NewTableModel model)
   {
      table.setModel(model);
   }

   public void actionPerformed(ActionEvent e)
   {
      MyDialog dialog = new MyDialog(null,true);
      dialog.setVisible(true);
   }
}

public class MyDialog extends JDialog implements ActionListener
{

   private JButton button;

   public MyDialog(JFrame parent,bool modal)
   {
      button = new JButton("Change Model");
      button.addActionListener(this);

      super(parent,modal);
   }

   public void actionPerformed(ActionEvent e)
   {
      NewTableModel newModel = new NewTableModel();
      A a = new A();

      a.changeModel(newModel);
   }
}

我想从第二种形式(MyDialog)的第一种形式(A)更新表格。我想为它设置一个新模型,当我点击MyDialog中的更改模型按钮时,它将自动以第一种形式(A)更新模型,并且所有显示的值将被来自MyDialog的新模型替换。怎么可能这样做?希望有人可以指导我。感谢。

1 个答案:

答案 0 :(得分:2)

如果您想要使用A中的模型完全替换MyDialog中的模型,为什么不在MyDialog中提供一个返回新模型的getter,只需替换为{{ 1}}一旦对话框关闭(假设它是一个模态对话框),否则你需要将A的引用传递给A