Curious Guy 007
Curious Guy 007

Reputation: 571

How to wait for a mouse click

class GameFrameClass extends javax.swing.JFrame  {

    public void MyFunc()
    {
        UserButton.setText(Str);
        UserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UserButtonActionPerformed(evt);
            }
        });
        getContentPane().add(UserButton);
    }

    private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {

        //print some stuff after mouse click
    }
}

In someother class i define this function

void functionAdd()
{
    GameFrameClass gfc = new GameFrameClass()
    gfc.MyFunc()
    System.out.println("PRINT THIS AFTER MOUSE CLICK")  
}

If someone can look into this code. I want to wait on the mouse click . Is there a way i can print the line System.out.println("PRINT THIS AFTER MOUSE CLICK") after the mouse is being clicked . For now this happens immediately and i am not able to wait for the mouse click . Is there a way of doing it ? Apart from doing it inside the function UserButtonActionPerformed() . Please let me know .

Upvotes: 2

Views: 13414

Answers (4)

Rags
Rags

Reputation: 434

You can always wait in UserButtonActionPerformed by defining it in the same class. If that is the case then you should not have the problem you are facing

Upvotes: 1

LanguagesNamedAfterCofee
LanguagesNamedAfterCofee

Reputation: 5952

It's hard to tell from the wording, but I assume he or she simply wants to execute code after the button is triggered (and not actually wait). For that, you need to add the code inside the method being invoked inside the actionlistener (in this case UserButtonActionPerformed).

So:

private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {

 System.out.println(...);

}

Also, following the Java coding conventions will help people answering your questions in the future.

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347332

This is a really "bad" way to do it...

private void UserButtonActionPerformed(java.awt.event.ActionEvent evt) {
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            System.out.println("PRINT THIS AFTER MOUSE CLICK");
            removeMouseListener(this);
        }
    });
}

A better way would be to have a flag in the actionPerformed method that would "enable" a mouse listener (which you added earlier). This listener would check the flag on each click and when set to true it would flip the flag (to false) and process the event...

Upvotes: 3

Jack
Jack

Reputation: 133669

Events are managed on a different thread which is the event dispatching thread, they are not managed by the thread that is executing your code (which presumably is the main thread).

This means that you can attach listeners to GUI elements but the only thing you can do to "wait" for the click is to execute the code inside the actionPerformed callback.

There is no way to pause the execution since the addActionListener doesn't do anything to effectively catch the event, it just adds the listener. Theoretically you could lock the main thread waiting to be notified by the event dispatch one but that would just be bad design.

Upvotes: 1

Related Questions