2

In a Java Swing desktop app. I have a number of smaller classes, all of which are made use of in an overarching App Class. One of such smaller classes is a JPanel that represents my login page. I've added a mouselistener to the login button on this page, that goes thus:

Public class loginPage extends JPanel{
String username;
boolean capturedName=false;
JTextField nameField;

...

loginButton.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    username = nameField.getText(); 
                    capturedName=true;    //for redundant checking of mouse click event
                    System.out.println(username);    //error checking
                    System.out.println(capturedName);    //error checking
                }
            });
    }

In a separate display class that represents my JFrame, I make the login page an attribute of said class and I instantiate this display class in my app class, after adding the login page to it. I am trying to capture the login page username attr in the App class and pass it to other methods. But when I run the code, and click on the login button, the value in the textbox isn't captured.

To error check, I tried the ff:

//Set login page GUI up 

while(display.loginPage.username==null){ //this is initially true

        if (display.loginPage.capturedName){  //boolean to check if button has been clicked
            display.loginPage.username=display.loginPage.nameField.getText(); //intentional redundancy
            String username=display.loginPage.username; 
            System.out.print(username);
            //pass username to other methods
        }

        }

When I run the code, enter a name on the username textfield, and click login, the typed name and a true value for the capturedName boolean are both printed, but the

if (display.loginPage.capturedName)

condition is never fulfilled. Also when I add in print display.loginPage.username, I get a null value . What could be the reason for this discrepancy between the same values?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Suggestion for you (un-related to your problem): The class name "loginPage" is not a good choice. Rename it "LoginPage". It is a Java convention for class names to start with an upper-case letter and for variables to start with a lower-case letter. If you follow this convention your code is easier for others to understand. – EJK Dec 20 '14 at 03:34
  • 2
    For better help, consider creating and posting a [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve). We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem. As an aside, you will never want to use a MouseListener with a JButton as you're doing. Instead use an ActionListener as that is what it was built for. – Hovercraft Full Of Eels Dec 20 '14 at 03:34

3 Answers3

4

What could be the reason for this discrepancy between the same values?

You could have two objects of the same type, one displayed, whose state is being changed, and one not displayed that you're checking in the if block.

But again, for better help, consider creating and posting a Minimal, Complete, and Verifiable Example Program. We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem. As an aside, you will never want to use a MouseListener with a JButton as you're doing. Instead use an ActionListener as that is what it was built for.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks for your response. I believe you're right about the fact that I may be working with two different instances of the LoginPage, but to my eyes, there's been nothing to indicate this discrepancy in my code. Perhaps I'm wrong? – Dirichi D Ike-njoku Dec 20 '14 at 03:51
3
  1. "I've added a mouselistener to the login button" - Not a good idea, buttons should use ActionListener, see How to Write an Action Listeners and How to Use Buttons, Check Boxes, and Radio Buttons;

  2. while(display.loginPage.username==null){ is a REALLY, REALLY bad idea which could lock up your UI and suggests that you violating the single thread rules of Swing. See Concurrency in Swing for more details.

What it sounds like you need is a modal dialog, which can block the execution of the code at the point the dialog is made visible...

See How to Make Dialogs for more details

Take a look at Java and GUI - Where do ActionListeners belong according to MVC pattern? for an example of a Login dialog/form using the MVC paridigm (Model-View-Controller)

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • My apologies, I should point out that I misquoted the login button as a button. It serves that function, but it's actually a JLabel for reasons best known to my colleague who actually wrote this part up. Does the same issue with mouselisteners apply to JLabels? – Dirichi D Ike-njoku Dec 20 '14 at 03:47
  • Well, that raises usability issues, as not all users do or can use a mouse. But no, a MouseListener in this case is you're only choice (other then using a proper button) – MadProgrammer Dec 20 '14 at 09:03
0

Turns out as @MadProgrammer suggested, that I was creating two different loginPage objects in my display class. It's curious, however, that the mouselistener on the login page GUI that was visible actually listened for, and returned expected results of the better defined login page object (which was invisible).