RegularMoments
RegularMoments

Reputation: 9

How to update .getText()?

I am making a Login Screen in Java and so far its going good. Now I only have 1 more problem. How to update .getText()? If you don't know what I mean keep listening and you'll know.

JTextField T1 = new JTextField("USERNAME");
    JTextField T2 = new JTextField("PASSWORD");

String ST1 = T1.getText();
    String ST2 = T1.getText();


if (LOGIN.isEnabled()){
        if (ST1.equals("Jbot")) {

        HandlerClass handler = new HandlerClass();
                LOGIN.addActionListener(handler);

Now if I change the "USERNAME" part in line 1 to Jbot, it lets me login. But if it starts off as USERNAME and then you type in Jbot it wont work. I think the problem is that my code is not updating and only does .getText(); at the beginning.

Upvotes: 0

Views: 159

Answers (1)

camickr
camickr

Reputation: 324197

First of all variable names should NOT start with an upper case character.

If you want to know the value in a text field then you need to invoke the getText() method when an event occurs. In your case your form will probably have a "Login" button. So when the user clicks the "Login" button you get the text and do your test.

So you need to add an ActionListener to the "Login" button and invoke the getText() method in the ActionListener

Read the section from the Swing tutorial on How to Use Buttons for more information. Or you can read How to Write an ActionListener

I suggest you look at the table of contents for tutorial on all the Swing basics.

Upvotes: 3

Related Questions