Micha
Micha

Reputation: 83

Java Convert MouseEvent to ActionEvent

is it possible to convert a MouseEvent to an ActionEvent?

Upvotes: 8

Views: 6636

Answers (2)

Andreas Dolk
Andreas Dolk

Reputation: 114767

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.

Upvotes: 3

dacwe
dacwe

Reputation: 43504

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

Upvotes: 9

Related Questions