I would like to check whether a certain javax.swing.JButton (regular push-button) is pressed down (before it was released). Is there any option at all to check whether a button is down?
The most trivial solution is to add a MouseListener that will respond to the mouse click and release events. But, this does not cover the case where the button was activated by the Enter key, or any other way. I don't want to disable activating the mouse by keyboards or other ways - I just want to know when is it pressed down without restricting it's behaviour.
I tried listening to all the different events, and the only two that do respond to button press are the ActionPreformed (ActionEvent) and the StateChanged (ChangedEvent) events. ActionPreformed is executed once per click, meaning only after the button was pressed and released, so it's not good. StateChanged is indeed invoked several times when I click a button, and several times when I release it. But, the event object only includes information about the source widget (the button) and no information about the state change itself. This prevents from distiguishing which of the events we want to catch.
Thanks in advance!
ButtonModel can do that, more here or here or maybe off-topic JMenuItem & ChangeListener by #kleopatra
Related
I am new to JavaFX and see that there are different types of event handlers. What is the difference between MouseEvent, ActionEvent and Event in JavaFX?
Event is the superclass of all event types.
Sample event types are:
KeyEvents which are generated when a key is pressed.
MouseEvents that are generated by a mouse interaction like a move or button click.
There are many more.
Events don't have to only be generated by the JavaFX system. You can emit and consume your own custom events if you wish, but, usually, most events are generated by the JavaFX system.
An ActionEvent is a kind of event which often makes it easier to code to and respond to something being activated.
Often multiple events will be generated for a single action. For example, if you click on a button with a mouse you could get MOUSE_PRESSED, MOUSE_RELEASED and MOUSE_CLICKED events in addition to an ActionEvent.
If you wanted to respond to the button activation, you could listen for a MOUSE_CLICKED event, however that would not be recommended. This is because there are other ways to activate a button or the button could be disabled in which case you wouldn't want to take action on it. If it is a default button, the ENTER key can trigger the button, or the user could activate the button by pressing SPACE when they are focused on the button. When the button is activated by the keyboard, then there is no associated mouse event, so listening to mouse events for mouse activation is not recommended. Usually, you just want to know that the button was activated and not what caused it and you don't want to monitor all event types yourself that could cause the activation and under what conditions that activation should actually occur when the event triggers.
JavaFX provides the ActionEvent which will be emitted whenever the button is activated, regardless of the method that was used to activate it. This makes it much easier for you to code, as all you need to write is button.setOnAction(event -> handleButtonAction());.
An ActionEvent is also used in many places where it didn't seem worthwhile or necessary to create a specific type of event, for example in an animation KeyFrame when the key frame is activated. So ActionEvents aren't just used to handle button events, but may be used in many places.
I have set up a mouse dragged listener. I trying to set it up where you can click one button then drag your mouse over others to click the other ones. The problem I am having is when you click the first button it turns grey like its waiting for you to release the mouse button. When you move your mouse off the button (still holding the left mouse button) it returns back to its normal color but you cant highlight anything until you let go. Is there anyway to simulated letting the mouse go and "unclicking" the button so you can highlight other things?
What you observe is the typical behavior of the ButtonModel used by Swing buttons. A complete example is examined here, but note how the effect depends on the chosen Look & Feel's ButtonUI delegate.
To get the effect you want, you would have to create buttons using your own variation of BasicButtonUI and a custom ButtonModel that uses isRollover() to add buttons to your program's notion of a selection model.
As an alternative, consider JList, which contains a ListSelectionModel that allows MULTIPLE_INTERVAL_SELECTION. A compete example is shown here.
I'm working on a minesweeper in Java with Swing and I figured it'd be a fast way to get "rid" of a button that was clicked by using
JButton.setEnabled(false); (with a proper icon too, of course).
But do I have to remove all the listeners connected to this button later or is it enough and I can just forget about the said button then?
You have 2 different questions, one in your title, and one in your description.
Is removing actionListener necessary when you disable the button?
As stated in the previous comments, no.
But do I have to remove all the listeners connected to this button later...
Yes, if you have other kinds of listeners. For example, a MouseListener will still fire if the button is disabled. Usually, there is no need for a MouseListener on JButton, but there may be in some corner cases. I'm not sure about the other types of listeners that can be added to a JButton.
Just wanted to clarify.
I have a frame with some JTextFields on it. When the user tabs out from a JTextField, I execute custom logic in the FocusTraversalPolicy, which sets some properties and based on them the FocusTraversalPolicy decides where should the focus go next. So far so good.
But I want this custom logic to execute everytime the JTextField loses focus(in an InputVerifier, because there is also validation in it).
Now the problem and the question: If the user leaves the field with the mouse the InputVerifier will be triggered and everything will be fine. But if the user presses TAB, both the FocusTraversalPolicy and the InputVerifier will be triggered and the custom logic will be executed twice, which I don't want. How can I detect in the InputVerifier that TAB was already pressed and don't execute anything? I want to execute the custom logic in the InputVerifier only when the field is left with the mouse. Or is there a better solution?
Hope my question is clear.
Regards, Petar
I ended up setting a boolean flag from the FocusTraversalPolicy if the custom logic is executed. Then in the InputVerifier if it is already executed I don't execute it again. Then I reset the flag.
Can you disable a JButton without graying out the button itself?
When you use setEnbaled(false), the button disables and turns to gray. Is it possible to disable the button but make the appearance of the button still the same?
You could ignore the button press in your ActionListener if a flag is set; however, you should not disable the button without showing it as disabled: it will only confuse the users of your application.
I suggest setting a ButtonModel with overridden setArmed and setPressed. Exactly how you override it depends on exactly what you want it to do - should it appear to be pressed when pressed, for instance? Only set the ButtonModel once for a JButton. Add or repurpose state on the ButtonModel to indicate how you want it o behave at any given moment.
Clearly, GUIs with non-standard behaviour are likely to confuse users.