'How to pass text user enter into JTextField from one JFrame into cell of JTable of another JFrame

I'm trying to make the user enter their details in a form and then these details should be displayed in a table for the user to see whenever they want to open it.

I have three JFrames. A main menu from which you can open the other two JFrames which are the table page and the form to enter the details.

I have made a button in the JFrame for the form that is supposed to save a variable called name as whatever the user enters in the text field and then I use .setValueAt to update the table but the table doesn't update after I go back to the main menu and open the JFrame with the table.

this is the code for the button:

        JButton btnNewButton = new JButton("Enter");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            
            name = textField.getText();
            
            table a = new table();              
            a.table_1.setValueAt(name, 1, 0);
            
            dispose();
        }
    });

This is the main menu code (minimum reproducible example)

public class mainMenu extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                mainMenu frame = new mainMenu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public mainMenu() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    JButton btnTable = new JButton("Table");
    btnTable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            
            table tableButton = new table();                
            tableButton.openTable();
        }
    });
    btnTable.setBounds(300, 10, 126, 40);
    btnTable.setForeground(Color.WHITE);
    btnTable.setFont(new Font("Tahoma", Font.BOLD, 20));
    btnTable.setBackground(Color.RED);
    contentPane.add(btnTable);
    
    JButton btnNewButton = new JButton("User input");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            
            userInput userInputButton = new userInput();
            userInputButton.openUserInput();
        }
    });
    btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 15));
    btnNewButton.setBounds(23, 171, 126, 49);
    contentPane.add(btnNewButton);
}}

This is the cod of the JFrame of the table page:

public class table extends JFrame {

private JPanel contentPane;
private JTable table;
JTable table_1;

/**
 * Launch the application.
 */
public static void openTable() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                table frame = new table();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public table() {
    
    userInput a = new userInput();
    
    
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    table_1 = new JTable();
    table_1.setModel(new DefaultTableModel(
        new Object[][] {
            {"name"},
            {null},
        },
        new String[] {
            "New column"
        }
    ));     
    table_1.setBounds(44, 60, 238, 100);
    contentPane.add(table_1);
}}

This is the code for the JFrame of the form

public class userInput extends JFrame {

private JPanel contentPane;
private JTextField textField;
String name;

/**
 * Launch the application.
 */
public static void openUserInput() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                userInput frame = new userInput();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public userInput() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    textField = new JTextField();
    textField.setFont(new Font("Tahoma", Font.PLAIN, 15));
    textField.setBounds(53, 67, 170, 46);
    contentPane.add(textField);
    textField.setColumns(10);
    
    JButton btnNewButton = new JButton("Enter");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            
            name = textField.getText();
            
            table a = new table();              
            a.table_1.setValueAt(name, 1, 0);
            
            dispose();
        }
    });
    btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 15));
    btnNewButton.setBounds(264, 202, 117, 35);
    contentPane.add(btnNewButton);
}}

Other details:

I have seen The Use of Multiple JFrames: Good or Bad Practice? but this is for a school project and the teacher only showed us how to connect the program making multiple JFrames. I would try to do this in another way but this is supposed to be a group project so everyone else is probably going to do it the way the teacher taught us so it would be a lot better for me to figure it out this way.

For more context, everyone has different parts that they have to do. We are supposed to connect the parts by connecting our JFrames together. I am only responsible for the table page but I have to display details from various other JFrame that my group members are doing. I'm trying to simulate the other members JFrames to see how this would all work so we don't have to figure it out last minute.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source