1

I have Jframe that has a JTextField and a JButton. It should return text of Jtextfield to anotherClass (MainPage). but when program starts, It returns null to the class.

    public class JframeFoo extends JFrame {

    private String username = new String();

    public JframeFoo() {
        // --------------------------------------------------------------
        // Making Frame for login

        final JTextField usernameFiled = new JTextField();
        this.add(usernameFiled);

        JButton signinButton = new JButton();
        // ------------------------------------------------------------

        signinButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                setVisible(false);
                Main.mainpage.setVisible(true);

            }
        });
        // ----------------------------------------------------------------------
        username = usernameFiled.getText();
    }

    public String getuserName() {
        return this.username;
    }
}

(this Jframe should run at the start of program and when it gets text, it should go to invisible and another class should become visible.)

Sw4Tish
  • 202
  • 4
  • 12
s_puria
  • 397
  • 1
  • 8
  • 19
  • See this linke: [**Pass values entered in one JFrame's text field as an input parameter in other JFrame**](http://stackoverflow.com/questions/17412498/pass-values-entered-in-one-jframes-text-field-as-an-input-parameter-in-other-jf/17413372#17413372) – Azad Jul 30 '13 at 13:29

3 Answers3

3

You need to move the call to username = usernameField.getText() into the actionPerformed method. It only gets set to null the way you currently have it.

Choker
  • 875
  • 6
  • 9
3

The constructor JFrameFoo() is called when that frame is created. Therefore, this line:

username = usernameFiled.getText();

is also called at that moment. What you want to do instead is:

signinButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {            
        username = usernameFiled.getText();
        setVisible(false);
        Main.mainpage.setVisible(true);
    }
});

EDIT

What I expect is also going wrong is that you use userName in your main class before it is initialized. I would recommend two things:

  1. Learn about event-driving programming and callbacks. The simple fact that a line is below another in the source does not mean that it is executed later.
  2. Instead of calling mainPage.setVisible, do something like

signinButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {            
        setVisible(false);
        Main.mainpage.open(usernameFiled.getText());
    }
});

and add that method in your mainpage, doing something like

public void open(String username) {
    this.setVisible(true);
    // do whatever you want to do with username
}
Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61
  • @Shaftak then something else is going wrong in a part of the code you haven't provided. Can you show: 1) how you create `JFrameFoo` and 2) how you call `getUserName()` – Vincent van der Weele Jul 30 '13 at 13:25
  • I have an instance of JFoo in my main class. and in MainPage `System.out.println(Main.JFrameFoo.getuserName());` – s_puria Jul 30 '13 at 13:32
2

In addition to calling the getText() method from within the actionPerformed overridden method, you can also use this.dispose(); rather than setVisible(false);

so your code would look something like:

        signinButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                username = usernameFiled.getText();
                if ((username != null) || !(username.length() == 0)) {
                    this.dispose();
                    Main.mainpage.setVisible(true);
                } else {
                    // Appropriate error here...
                }
            }
        });

Calling getText() from within actionPerformed will also allow you to do some checks on the username variable before you dispose of the frame and proceed (again, see above snippet).

Good luck!

STM
  • 36
  • 2
  • I am a bit confused now, how will after disposing, one will call the method `getuserName()` ? Won't this completely erase everything from the memory, which is associated with the reference ? Though no doubt the idea that you have put forth is good, but the class extends JFrame for no purpose. Though if we use the reference alone instead of extending, then in my opinion it can work. – nIcE cOw Jul 30 '13 at 15:17
  • Though +1 for the answer, though this will come in some time. My limit is gone for today :-) – nIcE cOw Jul 30 '13 at 15:23