is it possible to convert a MouseEvent to an ActionEvent?
Not without losing some information. The MouseEvent contains information about the mouse location (x, y) and which buttons that are pressed (if any).
I would do the conversion like this:
MouseEvent me = ...;
ActionEvent ae = new ActionEvent(me.getSource(), me.getID(), me.paramString());
Sure, that's what a Button does (to my understanding). It processes a MouseEvent and creates (sends) an ActionEvent.
Action events are semantic events - like a signal, that a certain button (widget!) has been "pressed". The trigger for this action event may have been a mouse event ("left button has been pressed and released while the mouse pointer was in the rectangle defined by a AWT Button widget") or a keyboard event ("Space bar has been pressed and released while the focus was at AWT Button widget").
I guess you're not looking at a technical conversion. Practiacally, you'll have to listen to mouse events and fire new action events to your action listeners.
Related
I want to get mouse event while mouse click is held down but is not moving.
mouseDragged only works while the mouse is moving, but how can I get the event while mouse is not moving.
I'm using java.awt.event.MouseListener
Thank you.
There is a full javadoc for AWT (also for a lot of other api): https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html
void mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.
void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component.
The mouse doesn't detect any events while mouse click is held down and the mouse is in place
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 a continous Action which I can call by pressing down a JButton b1. Currently I have added a MouseListener to b1 and I control the continous nature of this action by calling StartAction() in MousePressed() and StopAction() in MouseReleased(), if the source of the mouse event happens to be b1.
Now I want to be able to trigger the button by keyboard as well. One would usually do that by ActionListeners. I can do:
if (source.equals(b1)) {
startAction();
stopAction();
}
Only problem is, this way a mouse click would trigger both the ActionListener and the mouseListener. I thought I would try setting the action listener as:
if(source.equals(b1) && !(e instanceof MouseEvent))
{
...
}
But that gives me: inconvertible types cannot cast MouseEvent to ActionEvent.
So Question is: is there any way of knowing if an action event was a mouse click or an Keyboard trigger? Should I use another type of Buttons?
I have a mouseListener on my Component and I need do the action on mousePressed in any case except one: if the focus is on another window and user clicks on my Component.
How can I ignore the mousePressed for Component if the focusOwner was another window before MouseEvent was fired? The FocusListener.focusGained and checking the focusOwner at the click moment can't help.
UPD: My task is to detect the case when focusGained fired on cause of mousePressed.
Use a WindowListener and handle the windowActivated and windowDeactivated events to register/deregister your MouseListener.
You may need to place the adding of the MouseListener in a SwingUtilities.invokeLater() to make sure the listener is added after the window has focus.
I had a similar Problem.
If you implement and register FocusListener you can ask in the focusGained method FocusEvent.getOppositeComponent() and detect if the user has changed the window. After that you can register the Mouslistener and unregister it in the focusLost method.
Is there any way to listen for mouse wheel presses (not moving the wheel, just pressing it)?
I have checked the MouseWheelListener API but there is nothing on mouse wheel presses, just wheel movings.
Mouse wheel button is usually mouse button 2:
public void mouseClicked(MouseEvent evt) {
if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0) {
System.out.println("middle" + (evt.getPoint()));
}
}
or even better:
SwingUtilities.isMiddleMouseButton(MouseEvent anEvent)
Mouse wheel presses are reported through the MouseListener interface.
Use the mousePressed and mouseReleased events and check the MouseEvent.getButton() method to return the button number pressed or released.
You can also detect clicks with the mouseClicked event, but I have found that the built-in criteria for mouse clicks is too narrow. In this case, however, multiple mouse buttons could be clicked and you can use MouseEvent.getModifiers() to get a bitmask of the pressed buttons.