0

I have a dilema. This might sound stupid but i have no idea how to do this. I have a password class and a main screen. My main screen has a button that when pressed pops up the password class. Here is the call to the passwordClass from an actionlistener on my main class.

public PasswordClass login(){
        pressMe.setVisible(true);
        String player="?";
        final String playerT = player;
        boolean nameCorrect = false;
        final PasswordClass hold = new PasswordClass(null); 
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    //Turn off metal's use of bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            PasswordClass.createAndShowGUI();   
                }
            });
        return hold;
        }

the return statement isn't anything related to this I never used it but I had it their for testing purposes. In my password class i have a boolean that tells me if the user input matches the correct login info. i call it worked i run the password class and i had the problem that while the window is popping up my code to check if it worked is running simultaneously. This is a problem because i only want to check if it worked after the user has pressed ok. Here is the code

 public void actionPerformed(ActionEvent ae)
    {
    else if(ae.getActionCommand().equals("Login")){
    login();
    }
    else if(ae.getActionCommand().equals("Press Me To Continue")){
    if(PasswordClass.worked){
    //worked is a static variable from the PasswordClass class              
    }
    pressMe.setVisible(false);
    }
    }

So whenever OK is pressed on The PasswordClass JFrame a little button pops up and asks a SECOND time for it to save. I want it to save from the first OK button. The reason i make another button is because i don't know how to stop and wait for the OK button to be pressed. My if loop to check if it worked already returns false automatically before the user presses OK. That is my problem and I am really confused on how to solve it. Any help? If any more code is needed I will provide it but i think this is enough.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Sam Liokumovich
  • 91
  • 1
  • 2
  • 6
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Sep 09 '15 at 04:01
  • 1
    I'd also consider having a look at [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer Sep 09 '15 at 04:01
  • Based on your, loose, description, I would suggest having some kind of flag which would know the value it saved last and only prompt the user if the values have changed – MadProgrammer Sep 09 '15 at 04:03
  • You might also find [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) on some interest – MadProgrammer Sep 09 '15 at 04:03
  • 1
    deadly confusing explanation(or I'm stupid), but What about remove the button action listener, or change the action command to something !@#$, so it will never reach or invoked in action commands –  Sep 09 '15 at 04:06

2 Answers2

1

The reason i make another button is because i don't know how to stop and wait for the OK button to be pressed

Use a modal dialog of some kind, see How to Make Dialogs for more details

Conceptually, you want to display a modal dialog, which prompts the user for some information, while blocking at the point in your code that the dialog was made visible. When the dialog is dismissed (for what ever reason), you'll want to check the results from the dialog and take appropriate actions based on what the user did

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

There needs to be some form of synchronization between the objects (not necessarily related to the statement of that name).

If the code that wants to check the result wants to block until the result is set, you could use a CountDownLatch: the actionPerformed method calls CountDownLatch.countDown() while the other code calls CountDownLatch.await().

On the other hand, if the code checking the result does not want to block, then a simple two-boolean approach would work well. Have one boolean indicate whether the button press complete and the other to tell whether OK was the button pressed.

ash
  • 4,867
  • 1
  • 23
  • 33
  • I'd be, very, very careful with suggesting something like this (`CountDownLatch`) in a single threaded environment, this could lead to a unresolvable dead lock and given that there features in the (Swing) API to resolve this issue safely, would be my least considered option – MadProgrammer Sep 09 '15 at 04:08
  • Using Swing events would actually be the best form of synchronization. With that said, from the Ops description, I understand there are already two threads involved and the issue is one is not waiting (i.e. blocking). Perhaps I misread. – ash Sep 09 '15 at 04:09
  • @MadProgrammer - by the way, I'm not sure what you mean by a "single threaded environment"; obviously, there's no need for synchronization primitives without more than one thread. – ash Sep 09 '15 at 04:10
  • 1
    Swing is a single threaded framework, all interactions made to the UI MUST be made from within the context of the EDT. The OP is already using an `ActionListener`, but because they are using a `JFrame`, the code is executing without them been able to ascertain the information the user is inputing, where a `JDialog` would block the code execution at the point of the dialog become visible until it was closed by the user (in a manner which is safe to use with Swing), allowing them to capture the return result more cleanly – MadProgrammer Sep 09 '15 at 04:13
  • Yeah, good point about swing event handling being single-threaded. Ironic that an event-driven solution is single-threaded since event-driven processing is ideal for asynchronous processing. – ash Sep 09 '15 at 15:14