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.
Related
I want to generate mouse click events for a Java application. I am aware that there is java.awt.Robot, which would allow me to do this. However, I would like that the generated mouse events do not actually affect the "cursor". That is, I would like to be able to minimize the window of the Java program and do other things with the mouse cursor, while mouse events are still generated for that program. How could I do that?
Edit: Furthermore, I cannot be assumed that there is a number of buttons that I need to interact with. Rather I'd like to generate mouse click events as if I had clicked at a certain position within the window.
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.
In Java's AWT FocusEvent class:
There are two levels of focus events: permanent and temporary.
Permanent focus change events occur when focus is directly moved from one Component to another, such as through a call to requestFocus() or as the user uses the TAB key to traverse Components.
Temporary focus change events occur when focus is temporarily lost for a Component as the indirect result of another operation, such as Window deactivation or a Scrollbar drag. In this case, the original focus state will automatically be restored once that operation is finished, or, for the case of Window deactivation, when the Window is reactivated.
In JavaFX, a ChangeListener can be added to the focusedProperty as shown here, but how does one determine if the change is permanent?
The list of SWT constants in the SWT API supplies two very similar event types (for use with various event listeners):
Show
Paint
What's the difference? Wouldn't all 'Show' events require a 'Paint' event and all 'Paint' events require a 'Show' event?
According to this page:
Show:
The widget is becoming visible
Paint:
A control was asked to draw
So the main difference is the following:
SWT.Show is dispatched once the Widget becomes visible. SWT.Paint is called whenever the Widget changes state, i.e. when it has to be redrawn. For example when it is resized or the content changes.
To answer your last question. Every Show event is coupled with a Paint, but not every Paint is coupled with a Show.
To see how it works just attach two Listeners to a Shell that just print out "Show" or "Paint" and see when they are fired.
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