Query:is there the concept of priority level in java's event responsesystem?

J

Jack Dowson

Hello Everybody:
I am indulging in java's gui programing these days.
Yesterday,I created a tree using class JTree.I've tried two ways to
response TreeSelectEvent,one is by implementing interface
treeSelectionListener,the other is by MosueListener.
They both worked well when applied alone.But when I employed them both
at the same time,only MouseListener worked,so I think there might be
some concepts like priority level in java's event response system?
Am I right? Or there might have something else happened?

Thanks.
Dowson.
 
T

Tom Hawtin

Jack said:
Yesterday,I created a tree using class JTree.I've tried two ways to
response TreeSelectEvent,one is by implementing interface
treeSelectionListener,the other is by MosueListener.
They both worked well when applied alone.But when I employed them both
at the same time,only MouseListener worked,so I think there might be
some concepts like priority level in java's event response system?

AWT's MouseListener and MouseMotionListener handling is broken. This
causes many bugs and peculiar behaviour.

Unlike every other event, if a component has no mouse [motion] listener
then mouse events are bubbled up to the parent container. This means you
can get away with listening to a parent component, and still receive
events for all of its children.

However, events are supposed to be multicast and listeners should not
interfere with one another. If some other code, quite reasonably, adds a
mouse [motion] listener to a child component, then the events wont get
bubbled up.

This is spectacularly bad design.

So, prefer the higher level event types. Mouse [motion] listeners are
fine if and only if you are defining your own subclass of JComponent or
implementing a PL&F.

Otherwise there are hacks to get the low-level events. The best is to
push a java.awt.EventQueue that intercepts events as they are
dispatched. This works with standard applet/web start permissions (from
1.2.2, IIRC). However, you do lose system copy & paste support and
potentially other features of trusted libraries.

Another approach is to use Toolkit.addAWTEventListener. This does not
work at all for untrusted code, so avoid if you can. This technique is
used by the Basic PL&F to implement pop ups on components from 1.5, and
activating internal frames from 1.6.

The most popular approach is 'glass panes', which are really broken.
Avoid at all costs.

Tom Hawtin
 
J

Jack Dowson

So, prefer the higher level event types. Mouse [motion] listeners are
fine if and only if you are defining your own subclass of JComponent or
implementing a PL&F.

Otherwise there are hacks to get the low-level events. The best is to
push a java.awt.EventQueue that intercepts events as they are
dispatched. This works with standard applet/web start permissions (from
1.2.2, IIRC). However, you do lose system copy & paste support and
potentially other features of trusted libraries.

Another approach is to use Toolkit.addAWTEventListener. This does not
work at all for untrusted code, so avoid if you can. This technique is
used by the Basic PL&F to implement pop ups on components from 1.5, and
activating internal frames from 1.6.

The most popular approach is 'glass panes', which are really broken.
Avoid at all costs.

Tom Hawtin

Thank you so much!
Dowson.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top