How to update JInternalFrame by click button on JDialog (JAVA)

91 Views Asked by At

I am creating a MDI Application by Java. I design a JInternalFrame

public class account extends javax.swing.JInternalFrame

{account GUI here} that show data get from SQL. On this account, I create a JButton named btnReName

private javax.swing.JButton btnReName;

to open a new JDialog ReName.

public class ReName extends javax.swing.JDialog

This JDialog ReName is created to change the name in SQL. After rename successfull, I click on a JButton named btnConfirm

private javax.swing.JButton btnConfirm;

to close this JDialog. After click button Confirm, a JOption appear like that. I want after click OK, the JDialog ReName is closed, and at the same time, the data on the JInternalFrame account update exactly to what i changed in this JDialog ReName by re-query data from SQL(in other words, i want to update the account by clicking on a Button btnConfirm created on ReName that opened by this JInternalFrame account) but i dont know how to design it. Can someone guide me how to create the Button with the function like i was description? Link to my full-code

1

There are 1 best solutions below

2
Abra On

The below code does what you want. I did not find a minimal, reproducible example in your question so I guessed that you are using a JTable to display the Player Information. I also only added the Rename button since your question only concerns that button. Here is the code.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class GamePlay {
    private JFrame  frame;

    private void createAndShowGui() {
        frame = new JFrame("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JDesktopPane desktop = new JDesktopPane();
        Account iFrame = new Account("iFrame");
        iFrame.setVisible(true);
        desktop.add(iFrame);
        frame.setContentPane(desktop);
        frame.setSize(600, 580);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new GamePlay().createAndShowGui());
    }
}

class Account extends JInternalFrame implements ActionListener {
    private JButton  btnReName;
    private JTable  table;

    public Account(String title) {
        super(title);
        String[] columnNames = {"Ten Nguoi Choi","So Lan Thang","So Lan Choi"};
        Object[][] data = {{"Player Name", 1, 7},
                           {"Nguoi choi 1", 10, 10},
                           {"Nguoi choi 2", 10, 100},
                           {"Nguoi choi 3", 0, 10}};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);
        btnReName = new JButton("Rename");
        btnReName.addActionListener(this);
        JPanel panel = new JPanel();
        panel.add(btnReName);
        add(panel, BorderLayout.LINE_END);
        pack();
    }

    public void actionPerformed(ActionEvent event) {
        ReName dlg = new ReName(this);
        dlg.setVisible(true);
    }

    public void changeName(String newName) {
        int row = table.getSelectedRow();
        if (row >= 0) {
            table.setValueAt(newName, row, 0);
        }
    }
}

class ReName extends JDialog implements ActionListener {
    private Account  account;
    private JButton  btnConfirm;
    private JButton  cancel;
    private JPasswordField  passwordField;
    private JTextField  nameTextField;

    public ReName(Account acct) {
        account = acct;
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        add(createForm(), BorderLayout.CENTER);
        add(createButtons(), BorderLayout.PAGE_END);
        pack();
        setLocationRelativeTo(acct);
    }

    public void actionPerformed(ActionEvent event) {
        JOptionPane.showMessageDialog(this,
                                      "Successful",
                                      "Noitication",
                                      JOptionPane.INFORMATION_MESSAGE);
        dispose();
        account.changeName(nameTextField.getText());
    }

    private JPanel createButtons() {
        JPanel panel = new JPanel();
        btnConfirm = new JButton("Confirm");
        btnConfirm.addActionListener(this);
        cancel = new JButton("Cancel");
        panel.add(cancel);
        panel.add(btnConfirm);
        return panel;
    }

    private JPanel createForm() {
        JPanel form = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets.bottom = 5;
        gbc.insets.left = 5;
        gbc.insets.right = 5;
        gbc.insets.top = 5;
        JLabel nameLabel = new JLabel("Enter new name:");
        form.add(nameLabel, gbc);
        gbc.gridx = 1;
        nameTextField = new JTextField(10);
        form.add(nameTextField, gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel passwordLabel = new JLabel("Password:");
        form.add(passwordLabel, gbc);
        gbc.gridx = 1;
        passwordField = new JPasswordField(10);
        form.add(passwordField, gbc);
        return form;
    }
}

When I run the above code, the GUI initially looks as follows (using JDK 11 on Windows 10)

initial gui

This is a screen capture after selecting the name to change and pressing button Rename.

after press rename

This is a screen capture after entering the new name and pressing button Confirm.

after confirm

And finally, a screen capture after pressing button OK. Notice that the first column of the selected row in the JTable has changed to what was entered in the JDialog.

after OK