wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Translating from low-level event to semantic event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Translating from low-level event to semantic event" Watch "Translating from low-level event to semantic event" New topic
Author

Translating from low-level event to semantic event

Steve Simon
Greenhorn

Joined: Jan 28, 2012
Posts: 12
Is it possible to translate a low level event (such as MouseEvent or KeyEvent) into a semantic event (such as ActionPerformed)?

To simplify my question, is it possible to fire an ActionEvent as a consequence of a particular key being pressed?

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
Yes, but you should consider other methods first. You can write methods to fire an event, but if you can set up a binding to a particular key, that might work better.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
not sure I'm reading this right, but if you want a keyEvent to fire off what an actionEvent does,
the simple way is to put the actionEvent stuff into a separate method, which is then available to
be called from elsewhere

e.g.

changes to

now, any other event/whatever can call doSomething()
Steve Simon
Greenhorn

Joined: Jan 28, 2012
Posts: 12
Thanks guys,

Campbell, the key-bindings are definitely relevant to my needs and I had no idea there was such a thing.
That certainly solves a big part of my problem, thanks for that.

Michael, you're on the right track.
In actual fact, it is currently the solution I am using for my custom component, but I would really like to simplify how it is used.
The problem is that this approach is implementation specific. Every time I use the component, I need to remember to intercept all related low level events and call doSomething() from them.
This would be fine if I didn't plan to reuse this component regularly, and I will be using this component regularly.
So what I actually want to be able to do is group all related low level events and define that group as an "ActionEvent" or even "MyCustomEvent".

Hypothetically, I would prefer to say some thing like: "A mouse release, tab key, mouseWheelMove should be treated as one kind of event, called "MyCustomEvent". The only thing you need to do when using this component is to intercept "MyCustomEvent".

Is this possible? (sorry if I wasn't clear enough before)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this is not what you're after, but it might give you something to play around with.
(just an example of grouping the events - in the 'masks')

run the code (as is), click the textArea and the mouse messages will print out.
type a key, and the key messages will print out.
uncomment the block (leave the commented-out line as is), now repeat the above
the events that display the messages have been consumed, but the key typed will still appear in the textArea
now uncomment that extra line, re-run and no key typed will appear

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36566
    
  16
Steve Simon wrote:Thanks guys, . . . I had no idea there was such a thing. . . . thanks for that.
You’re welcome
. . . this approach is implementation specific. Every time I use the component, I need to remember to intercept all related low level events . . .
Not sure what to recommend, but that sort of thing worries me. It may be possible for some components to be fired by several different things, however. I think you can fire a button with a click, space or enter key, but you would have to verify that. In the Java Tutorials, you tend to find things I don’t like, eg addActionListener(this) or the whole program inside the main method, but when you allow for that, it is one of the best sources of information available. That section should give you all sorts of information about listeners, (I think, including advice to use the highest‑level listener you can).
Steve Simon
Greenhorn

Joined: Jan 28, 2012
Posts: 12
Ok cool thank you very much to both of you, I really appreciate the attention.

The Java tutorials are indeed great, so I guess I should rather look at them more often than stick with my current preference: looking at the source code (which, by the way, involves such 'tight-coupling' of classes and interfaces its just bewildering to trawl through!)

Take care

regards
Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Translating from low-level event to semantic event
 
Similar Threads
java.awt.Event Constructors
Semantic Events
Example of Low Level Events.
MouseListener active for Disabled JLabel
difference between semantic events and low-level events