JButton .doClick() without doing anything? - java

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.

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?

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.

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);

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

Related

Can I change combobox value when a button is clicked in java?

By using doClick() method, the action of a button can be triggered. Is there such a way to change combobox value when a button is clicked in java? Thank You.
As you didn't state the graphical framework I assume you use Swing, as it contains a doClick function.
The doClick() function is only used to emulate a button click programmatically, for example in testing. This will fire all actions that are normally associated with that button, however you need to register those yourself, using a listener.

How to check if and which mouse button is pressed in Swing

How can I check if currently any mouse button is pressed and if so, which one it is?
The thing is that I need to use this kind of information in MouseListener.mouseEntered(). I checked MouseEvent but I could not find the method which would help me.
The getButton() method seems to only return value if there has been change in buttons' state.
Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods.
How can I check if currently any mouse button is pressed and if so, which one it is?
Presumably you want to invoke specific code depending on the button pressed so you can do something like:
if (SwingUtilities.isLeftMouseButton(...))
// do something
You could start by looking at How to write a Mouse Listener and the JavaDocs for MouseEvent in particular, the getButton method.
However, there are cross platform considerations that need to taken into consideration, which are overed by SwingUtilities.isLeftMouseButton and equivalent methods...
This will solve your problem
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.out.println(e.paramString()+"-"+e.getSource());
}
}, eventMask);
This is a Global Event Listeners.
Get the source and button from AWTEvent and do whatever you want to perform.

equivalent of getch() for Android

Is there an equivalent for C's getch() in Android or Java? I want the execution to stop until the user does some action, like tap on the screen or maybe press one of the hardware buttons like volume control or whatever is available. Another option would be to show a modal message box window, which does not let the program continue until the user presses on OK. Is it possible to do this in a simple way in Android? What is the simplest way to get something equivalent to getch() function in android?
I need to be able to use this in a Thread as well
There's no getch() equivalent function/method in java.
You must event handlers to do that.
Like having click handler for button,Which let you to some stuff on onClick() method,Once you click the button.
This example might helpful:Button Click Listeners in Android
There's an event listener for android like onCLickListener. Then you can also used Jdialog then set its .isEditable value to false like dialog.isEditable(false);

Java add ActionListener to special button

Is there a way to add/override the action event of THIS button ? I couldn't find anywhere how to acces this button and override it's action. In my case I need to do this because I need to save the resources before I exit my window, and if I press the x button, It will exit automatically.
On the JFrame:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Then add a WindowsListener and in the windowClosing(WindowEvent event) methods do your job and then call dispose()
I'd use a shut-down hook instead that way it'll save even if you close the app via another method.
Looky here
You can try using a WindowListener interface. If I remember correctly, it should allow you to do things when something on the frame is clicked on(like exiting).
See http://docs.oracle.com/javase/6/docs/api/java/awt/event/WindowListener.html

Can we tell if the mouse actually clicked on a checkbox in Java?

Here is the scenario. I have an swing applet with tons of checkboxes. some of them are disabled/unchecked when checking another. Each ItemStateChange() event executes a method to parse the entire form for changes. Is there a way to tell if an ItemStateChange() event was triggered due to a mouse click or from a setSelected() call?
The ItemStateChange() for each checkbox has the standard parameter java.awt.event.ItemEvent evt
I'd like to only call the processOrder() method once when a box is clicked. Right now it fires for each change thats made, regardless of whether the change happened from setSelected(). Sometimes there are 10+ parseForm(); calls from a single click.
You can't tell whether the source of the event is a mouse click or a setSelected call from the ItemEvent.
It sounds like you have a loop in your check box logic. You might want to add a controller that handles the events and sets each checkbox yet ignores events that occur due to calling setSelected on other check boxes.
Is there a way to tell if an ItemStateChange() event was triggered due to a mouse click or from a setSelected() call?
If your application manually invokes the setSelected() method then you can use code like:
checkBox.removeItemListener(...);
checkBox.setSelected(...);
checkBox.addItemListener(...);
If you are able to change to use a MouseListener instead of an ItemListener and respond to the mouseClicked() event you will only receive the events for the checkbox selected by the user.

Categories

Resources