1

I have one jFrame and its have one jTextbox and a button. Another jFrame have a single jLabel. I want to bring out the text written in first frame's textbox to second frame's jLabel when button is pressed. And as i have searched about this than i got some unreliable answers. But as per my knowledge it could be done by creating another static class or by calling this reference.

Ronak Joshi
  • 1,553
  • 2
  • 20
  • 43
  • you should use `observer pattern` or/with a `Mediator`. And also you sure don't need 2 JFrame. Please provide code what have you tried to do and we will help you. – nachokk Sep 02 '13 at 20:02
  • I would say that, in general, you want your GUI objects backed by a data class (i.e. the class whose fields contain the values of interest that are displayed by your GUI). Making your GUI the same thing as your data class diminishes encapsulation and risks making your code fragile and difficult to maintain. – scottb Sep 03 '13 at 00:13
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Sep 03 '13 at 03:12

2 Answers2

8

It is a question of "what" you want to achieve that will drive the "how"...

For example...

You could maintain a reference to the second frame within the first frame and when the button is clicked, tell the second frame that a change has occurred...

public class FirstFrame extends JFrame {
    // Reference to the second frame...
    // You will need to ensure that this is assigned correctly...
    private SecondFrame secondFrame;
    // The text field...
    private JTextField textField;

    /*...*/

    // The action handler for the button...
    public class ButtonActionHandler implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            secondFrame.setLabelText(textField.getText());
        }
    }
}

The problem with this is it exposes the SecondFrame to the first, allowing it to do nasty things to it, like remove all the components for example.

A better solution would be to provide a series of interfaces that would allow the two classes to talk with each other...

public interface TextWrangler {
    public void addActionListener(ActionListener listener);
    public void removeActionListener(ActionListener listener);
    public String getText();
}

public class FirstFrame extends JFrame implements TextWrangler {
    private JButton textButton;
    private JTextField textField;

    /*...*/

    public void addActionListener(ActionListener listener) {
        textButton.addActionListener(listener);
    }

    public void removeActionListener(ActionListener listener) {
        textButton.removeActionListener(listener);
    }

    public String getText() {
        return textField.getText();
    }
}

public class SecondFrame extends JFrame {
    private JLabel textLabel;
    private JTextField textField;
    private TextWrangler textWrangler;

    public SecondFrame(TextWrangler wrangler) {
        textWrangler = wrangler;
        wrangler.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                textLabel.setText(textWrangler.getText());
            }
        });
        /*...*/
    }
}

This basically restricts what the SecondFrame can actually gain access to. While it can be argued that the ActionListener in the SecondFrame could use the ActionEvent source to find out more information, by it's nature, it would be an unreliable mechanism, as the interface makes no mention of how it should be implemented...

This is a basic example of the Observer Pattern

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

The simplest way to pass the data between two JFrames is to create a public method. By this you can create the public method in your class where you want to get the data and call this method directly with an object of second class. And also if you want to call it without creating an extra object than just make you method static and call is directly with the class name. for further details visit this link