【问题标题】:How to update a jtable from a different form?如何从不同的表单更新 jtable?
【发布时间】:2023-03-16 17:23:01
【问题描述】:
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 的新模型替换。怎么可能做到这一点?希望有人可以指导我。谢谢。

【问题讨论】:

    标签: java swing jtable jdialog


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      相关资源
      最近更新 更多