nani
nani

Reputation: 399

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.

Upvotes: 0

Views: 949

Answers (2)

milez
milez

Reputation: 2201

There are a couple of things to fix here and I will not give you complete code but I will point out some errors. First let's consider your button_19 listener

public void actionPerformed(ActionEvent ae) {

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

}

When this is performed, it creates a totally new object of newFoodUI and gives it no parameters. So how could this frame know about anything that happened before its creation if you give it nothing? Additionally, you explicitly say DISPOSE_ON_CLOSE, when you could use HIDE_ON_CLOSE if you wish to reuse it.

Then in your JButton button_4 = new JButton("SAVE"); listener you want to save the data in the text field, but your implementation does nothing to the text field. You should, for example, get the text from textField_3 and write it to a file or send back to the first JFrame.

Then there is the issue of using multiple JFrames in the first place.

Upvotes: 1

JoGe
JoGe

Reputation: 910

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.

Upvotes: 1

Related Questions