Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Convert MouseEvent to ActionEvent

is it possible to convert a MouseEvent to an ActionEvent?

like image 201
Micha Avatar asked Mar 01 '11 09:03

Micha


People also ask

What is MouseEvent in Java?

public class MouseEvent extends InputEvent. An event which indicates that a mouse action occurred in a component. A mouse action is considered to occur in a particular component if and only if the mouse cursor is over the unobscured part of the component's bounds when the action happens.

Which method is used to processes mouse click?

A MouseEvent object is also passed to every MouseMotionListener or MouseMotionAdapter object which is registered to receive mouse motion events using the component's addMouseMotionListener method.

Which GUI component triggers an event when clicked with a mouse?

The MouseEvent class inherits many useful methods from InputEvent and a couple handy methods from the ComponentEvent and AWTEvent classes. Returns the event type, which defines the particular action. For example, the MouseEvent id reflects the state of the mouse buttons for every mouse event.

What methods do you use to register a handler for a mouse pressed released clicked entered exited moved and dragged event?

To register a handler object, you invoke the source object's registration method; for example, button. setOnAction (handler) for registering a handler for a button action event. To implement a handler interface, you implement the method defined in the handler interface.


2 Answers

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());
like image 131
dacwe Avatar answered Sep 22 '22 15:09

dacwe


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.

like image 45
Andreas Dolk Avatar answered Sep 21 '22 15:09

Andreas Dolk