Search code examples
javaeclipsejbuttonjtextfield

How to update JTextfield after click SAVE button


I have a main frame : JFrame>contentFrame>ScrollPane>BigPanel>panel_1T

private JPanel contentPane;
private JPanel BigPanel;
private JPanel panel_1T;

In panel_1T, I have put a FOOD button WITH its actionListener:

JButton button_19 = new JButton("FOOD");
button_19.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {

            newFoodUI nf = new newFoodUI();//Open other class
            nf.setVisible(true);
            nf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);      

                                                 }
                                               });
panel_1T.setLayout(new GridLayout(0, 2, 0, 0));
panel_1T.add(button_19);

When user click FOOD button, new JFrame in newFoodUI class will be shown.: JFrame>contentPane>panel>tabbedPane>panel_3>panel_5

In panel_5, I put a JTextField:

public static JTextField textField_3;

textField_3 = new JTextField();
panel_5.add(textField_3, "9, 4, fill, default");
textField_3.setColumns(10);

User will write some text into textField_3. Then user click SAVE button in panel_3, it will perform this:

JButton button_4 = new JButton("SAVE");
button_4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

                setContentPane(contentPane);
                panel_3.revalidate();
                panel_3.repaint();
                panel_3.updateUI();

                panel_5.revalidate();
                panel_5.repaint();
                panel_5.updateUI();


                contentPane.revalidate();
                contentPane.repaint();

            JOptionPane.showMessageDialog(null, "Saved !");

        }
    });
    button_4.setBounds(873, 396, 75, 33);
    contentPane.add(button_4);
}

The result is, when I click SAVE button and close the Frame in newFoodUI, I will reopen back by click the FOOD button to check whether the text I wrote has been saved or not. But its not saving the text I wrote.


Solution

  • You have to save the value from the textfeld textField_3.getText() and set this value manually to textfeld when showing textField_3.setText(value). So you have to keep your value in your project or store persistent somewhere.