How can JComboBox listener to MouseEvent?

R

RC

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!
 
T

Tom Hawtin

RC said:
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]
 
P

phillip.s.powell

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
 

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

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top