joxxe
joxxe

Reputation: 259

JButton .doClick() without doing anything?

Is it possible to simulate a click on a button, similar to doClick() but just graphical simulate it, not generate any ActionEvent´s. If know that i can extend the class, do my own doClick with a simple if-statement. But is it any other possibility?

I want to this because I have a button that the user can press, but sometimes the computer (it s in a game) "presses" the button. All the logic is done in another thread, I just wanna display it for the user.

Upvotes: 0

Views: 747

Answers (4)

trashgod
trashgod

Reputation: 205785

I think the simplest solutions is to just check the model in the ActionListener.

I don't see a way to distinguish between the two; the model is oblivious as to who calls its methods. Instead, you could save all the listeners, invoke doClick(), and restore the listeners. It looks like you would have to check action, change and item listeners.

Upvotes: 1

jocopa3
jocopa3

Reputation: 796

You can set whether it's enabled or not, so use "buttonName.setEnabled(false);"

Upvotes: 0

exomic
exomic

Reputation: 486

If you want to click on the button try this:

try
        {
            robot = new Robot();
            robot.mouseMove(0,500);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
        }catch(Exception e){}

But maybe you will prefer to simply disable the button when the user shouldn't click it

Button.setEnabled(false);

Upvotes: 0

John Gardner
John Gardner

Reputation: 25126

if you dig into the details in the look and feel you're using, you might be able to see how it detects and paints the button in its "down" state, and then simulate that?

or you could extend/implement ButtonModel, and mess with the setPressed/isPressed state?

Upvotes: 1

Related Questions