Java Swing:如何从另一个页面按钮刷新 JTable 模型

发布于 2024-09-03 09:06:22 字数 3225 浏览 4 评论 0原文

我有2个窗口,其中一个窗口显示JTable模型数据,双击该行时会弹出一个新窗口来编辑数据,提交后如何刷新JTable

Customer.java :

JPanel getJPanel() {
    if (jPanel == null) {

        jPanel = new JPanel();
        jPanel.setLayout(null);
        jPanel.setSize(new Dimension(792, 420));
        jPanel.add(getJScrollPane(), null);
        setUpTableData();
    }
   JScrollPane getJScrollPane() {
    if (jScrollPane == null) {
        jScrollPane = new JScrollPane();
        jScrollPane.setBounds(new Rectangle(20, 166, 759, 241));
        jScrollPane.setViewportView(getJTable());
    }
    return jScrollPane;
}

   JTable getJTable() {
    if (jTable == null) {
        jTable = new JTable();
        jTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent e) {
                new ContactEdit(name,email,phoneNo,phoneNo2,id).getJInternalFrame().setVisible(true);
                }
            }
        });
        DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
        String[] colName = {"No." ,"Name", "Email","Contact No. 1","Contact No. 2","ID"};
        tableModel.setColumnIdentifiers(colName);
        jTable.getTableHeader().setBackground(Color.WHITE);
        jTable.getTableHeader().setForeground(Color.BLUE);
        Font Tablefont = new Font("DialogInput",Font.BOLD,12); 
        jTable.getTableHeader().setFont(Tablefont);

    }
    return jTable;
}
   public void setUpTableData() {
    DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
    ArrayList<Address> list = sql.getContactLists();
    int i = 0;
    for (i = 0; i < list.size(); i++) {
        String[] bill = new String[6];
        for (int j = 0; j < 6; j += 6) {
            bill[j] = Integer.toString(i);
            bill[j + 1] = list.get(i).getName();
            bill[j + 2] = list.get(i).getEmail();
            bill[j + 3] = list.get(i).getPhoneNo();
            bill[j + 4] = list.get(i).getPhoneNo2();
            bill[j + 5] = list.get(i).getId();
        }
        tableModel.addRow(bill);
    }
    jTable.setModel(tableModel);
}

Edit.java

private JButton getJButton() {
    if (jButton == null) {
        jButton = new JButton();
        jButton.setBounds(new Rectangle(155, 138, 85, 25));
        jButton.setText("Update");
        jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                Address a = new Address(jTextField1.getText(),jTextField.getText(),jTextField3.getText(),jTextField2.getText(),"");
                                    sql.updateContact(a, Integer.parseInt(getJTextField4().getText()));                 
                            Customer c = new Customer();
                            c.setUpTableData();
                getJInternalFrame().setVisible(false);
                getJInternalFrame().dispose();
            }
        });
    }
    return jButton;
}

On JPanel Load 将调用此 setUpTableData 从数据库检索数据。 在编辑窗口上,我在更新按钮上添加了一个动作侦听器来刷新表,但我不知道如何使jTable刷新更新的数据? ActionListener 上的 setUpTableData 给我空指针。

I have 2 windows one window shown the JTable Model data, when double click the row will pop up a new window to edit the data, once submit how can I refresh the JTable?

Customer.java :

JPanel getJPanel() {
    if (jPanel == null) {

        jPanel = new JPanel();
        jPanel.setLayout(null);
        jPanel.setSize(new Dimension(792, 420));
        jPanel.add(getJScrollPane(), null);
        setUpTableData();
    }
   JScrollPane getJScrollPane() {
    if (jScrollPane == null) {
        jScrollPane = new JScrollPane();
        jScrollPane.setBounds(new Rectangle(20, 166, 759, 241));
        jScrollPane.setViewportView(getJTable());
    }
    return jScrollPane;
}

   JTable getJTable() {
    if (jTable == null) {
        jTable = new JTable();
        jTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent e) {
                new ContactEdit(name,email,phoneNo,phoneNo2,id).getJInternalFrame().setVisible(true);
                }
            }
        });
        DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
        String[] colName = {"No." ,"Name", "Email","Contact No. 1","Contact No. 2","ID"};
        tableModel.setColumnIdentifiers(colName);
        jTable.getTableHeader().setBackground(Color.WHITE);
        jTable.getTableHeader().setForeground(Color.BLUE);
        Font Tablefont = new Font("DialogInput",Font.BOLD,12); 
        jTable.getTableHeader().setFont(Tablefont);

    }
    return jTable;
}
   public void setUpTableData() {
    DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
    ArrayList<Address> list = sql.getContactLists();
    int i = 0;
    for (i = 0; i < list.size(); i++) {
        String[] bill = new String[6];
        for (int j = 0; j < 6; j += 6) {
            bill[j] = Integer.toString(i);
            bill[j + 1] = list.get(i).getName();
            bill[j + 2] = list.get(i).getEmail();
            bill[j + 3] = list.get(i).getPhoneNo();
            bill[j + 4] = list.get(i).getPhoneNo2();
            bill[j + 5] = list.get(i).getId();
        }
        tableModel.addRow(bill);
    }
    jTable.setModel(tableModel);
}

Edit.java

private JButton getJButton() {
    if (jButton == null) {
        jButton = new JButton();
        jButton.setBounds(new Rectangle(155, 138, 85, 25));
        jButton.setText("Update");
        jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                Address a = new Address(jTextField1.getText(),jTextField.getText(),jTextField3.getText(),jTextField2.getText(),"");
                                    sql.updateContact(a, Integer.parseInt(getJTextField4().getText()));                 
                            Customer c = new Customer();
                            c.setUpTableData();
                getJInternalFrame().setVisible(false);
                getJInternalFrame().dispose();
            }
        });
    }
    return jButton;
}

On JPanel Load will call this setUpTableData to retrieve the data from database.
On the edit window, I have added an action listener on the update button to refresh the table, but I don't know how to make the jTable refresh the updated data? The setUpTableData on ActionListener give me Null Pointer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2024-09-10 09:06:22

一种解决方案是为编辑窗口提供另一个 JPanel 的实例。 (您可以通过将面板传递给编辑窗口的构造函数来完成此操作)然后您可以简单地从 ActionListener 内部调用 setUpTableData() 方法。

执行此操作的代码可能如下所示:

编辑框架构造函数

public EditFrame(CustomerListFrame cListFrame, ... other params) {
    this.cListFrame = cListFrame;
}

更新 ActionListener

public void actionPerforment(ActionEvent e) {
    cListFrame.setUpTableData(the new data);
}

One solution would be to give the edit window an instance of he other JPanel. (You can accomplish that by passing the panel in to the constructor of the edit window) Then you can simply call the setUpTableData() method from inside the ActionListener.

The code to do this may look like the following:

Edit frame constructor

public EditFrame(CustomerListFrame cListFrame, ... other params) {
    this.cListFrame = cListFrame;
}

Update ActionListener

public void actionPerforment(ActionEvent e) {
    cListFrame.setUpTableData(the new data);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文