How can JComboBox listener to MouseEvent?  
Author Message
RC





PostPosted: 2007-3-20 4:30:00 Top

java-programmer, How can JComboBox listener to MouseEvent?
Since JComboBox and JButton both are
extends from JComponent.
When I addMouseListener(this);
for jButton, it listens to the MouseEvent.
But jComboBox doesn't listen to the MouseEvent.

public void mouseEntered(MouseEvent me) {
System.out.println("mouse entered");
jComboBox.setToolTipText("mouse entered"); // not work
jButton.setToolTipText("mouse entered button"); // works fine
}

mouseEntered just move your mouse pointer on
the object, no need to click (pressed and released).

I just want to detect the mouse entered on JComboBox
(i.e. popup a ToolTip) before I click it for select an item.

Do I miss something? Do I need to setXXX and/or enableXXX
JComboBox before I addMouseListener?

Thank Q for your help!
 
Tom Hawtin





PostPosted: 2007-3-20 4:50:00 Top

java-programmer >> How can JComboBox listener to MouseEvent? RC wrote:
>
> Since JComboBox and JButton both are
> extends from JComponent.
> When I addMouseListener(this);
> for jButton, it listens to the MouseEvent.
> But jComboBox doesn't listen to the MouseEvent.

JComboBox is implemented as a number of components (a text field, the
button and the container for them both). If you add a low level listener
to the container, it wont receive the events of its children.

Oddly, mouse events do bubble up if the child has no mouse listeners.
Unfortunately if you add mouse listeners recursively, you might end up
preventing events bubbling up that the PL&F expects.

As an alternative, you can either push an EventQueue and filter through
dispatched events, or add an AWTEventListener through Toolkit.

But probably what you want to do is reconsider what you were doing in
the first place.

Tom Hawtin

[Followup-To: comp.lang.java.gui]
 
phillip.s.powell@gmail.com





PostPosted: 2007-3-22 3:16:00 Top

java-programmer >> How can JComboBox listener to MouseEvent? On Mar 19, 4:29 pm, RC <email***@***.com> wrote:
> Since JComboBox and JButton both are
> extends from JComponent.
> When I addMouseListener(this);
> for jButton, it listens to the MouseEvent.
> But jComboBox doesn't listen to the MouseEvent.
>
> public void mouseEntered(MouseEvent me) {
> System.out.println("mouse entered");
> jComboBox.setToolTipText("mouse entered"); // not work
> jButton.setToolTipText("mouse entered button"); // works fine
>
> }
>
> mouseEntered just move your mouse pointer on
> the object, no need to click (pressed and released).
>
> I just want to detect the mouse entered on JComboBox
> (i.e. popup a ToolTip) before I click it for select an item.
>
> Do I miss something? Do I need to setXXX and/or enableXXX
> JComboBox before I addMouseListener?
>
> Thank Q for your help!

I am not thinking that you'd want a MouseListener for the JComboBox,
but instead consider a PopupMenuListener instead, chances are that
will give you the results you want within the JComboBox.

Phil