Skip to main content

Adding a MouseListener to a JComboBox

3 replies [Last post]
joecole
Offline
Joined: 2003-06-10
Points: 0

Hi,

I would like to listen for mouse events on the JComboBox. I have searched the source, and also the plaf, listcellrenderer and comboboxeditor sources, and still cannot find the right component to listen for mouse events.

I am guessing it should be in comboboxbutton, but since these are implementation specific, how would I listen for mouse events on it?

I think JComboBox should have an overridden addMouseListener(); that allows listening to events from components further down the hierarchy.

Can anyone help me out?

Joe Cole

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zander
Offline
Joined: 2003-06-13
Points: 0

> Hi,
>
> I would like to listen for mouse events on the
> JComboBox. I have searched the source, and also the
> plaf, listcellrenderer and comboboxeditor sources,
> and still cannot find the right component to listen
> for mouse events.

It would be nice to know why you want that, I can't imagine a reason for doing so..

joecole
Offline
Joined: 2003-06-10
Points: 0

Zander,

My users have been wanting right click "What's This?" functionality on all components in simple dialogs (most anyway).

I gave it to them.

Joe

joecole
Offline
Joined: 2003-06-10
Points: 0

I found the answer.

Not very nice, but works.

JComboBox box = (JComboBox) element;

for (int i = 0; i < box.getComponentCount(); i++) {
Component component = box.getComponent(i);
if (component instanceof AbstractButton) {
component.addMouseListener(this);
}
}

So there is the answer.

But - still remains the question - why doesnt the addMouseListener in JComboBox delegate to the components?

Joe Cole