-1

I am using java.swing GUI toolkit. I am trying a keep a data in JTextField and when I move to next frame and return to previous frame the data in JTextField disappears.

Can anyone suggest me a way to keep data in the JTextField fixed while moving between frames?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sabari
  • 11
  • 2

2 Answers2

2

An easy way to achieve this is to use a single Document for both text fields. The Document is the model that stores the data (in this case, plain text).

Here is a simple example showing two text fields, one in a frame and the other in an option pane. Update the text in the field in the dialog to see the other text field update instantly.

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;

public class TwoFieldsUsingOneDocument {

    private JComponent ui = null;

    TwoFieldsUsingOneDocument() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        JTextField frameTextField = new JTextField(15);
        Document doc = frameTextField.getDocument();

        final JTextField dialogTextField = new JTextField(15);
        // get both fields to use a single Document
        dialogTextField.setDocument(doc);

        final JButton showInputDialog = new JButton("Get Input!");
        showInputDialog.setMargin(new Insets(50, 120, 50, 120));
        ActionListener showInputActionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(
                        showInputDialog, 
                        dialogTextField, 
                        "Change text", 
                        JOptionPane.QUESTION_MESSAGE);
            }
        };
        showInputDialog.addActionListener(showInputActionListener);
        ui.add(showInputDialog);

        ui.add(frameTextField, BorderLayout.PAGE_END);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                TwoFieldsUsingOneDocument o = new TwoFieldsUsingOneDocument();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

I don't know how that could be done, but here's a small trick if there are only one or two textFields.

I suggest you to getText() when you change the frame and store it in a variable and then setText() back as soon as you come back.

Uma Kanth
  • 5,659
  • 2
  • 20
  • 41