Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JComboBox and MouseListener

749 views
Skip to first unread message

Jan Geldof

unread,
Oct 2, 1998, 3:00:00 AM10/2/98
to
I can't get a JComboBox to listen to Mouse-events. All other JComponents
work fine with it, only this !@%$##@ JComboBox!

Has anyone managed to do this? :

// +++++++++++++

JComboBox jComboBox = new JComboBox();
jComboBox.addItem(new String ("on"));
jComboBox.addItem(new String ("off"));
jComboBox.addMouseListener( new MouseListener() {

public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked"+e.getClickCount());
if (e.isAltDown()) {
// Middle mouse key
System.out.println("Middle mouse key clicked.");
}
if (e.isMetaDown()) {
// Right mouse key
System.out.println("Right mouse key clicked.");
}
}

public void mousePressed(MouseEvent e) {
System.out.println("Mouse pressed");
if (e.isMetaDown()) {
// Right mouse key
System.out.println("Right mouse key pressed.");
}
}

public void mouseReleased(MouseEvent e) {
System.out.println("Mouse released");
}

public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered");
}

public void mouseExited(MouseEvent e) {
System.out.println("Mouse exited");
}

});

// ----------

==========================================================
Jan Geldof
ECMWF (European Centre for Medium-Range Weather Forecasts)
Shinfield Park
Reading
UK
tel. 0118-9499252
e-mail: jan.g...@ecmwf.int

James Tan

unread,
Oct 3, 1998, 3:00:00 AM10/3/98
to
I'm having the same problem.
I believe there is already an entry in the bug list.

Like you, I need to detect mouse events so that I can overlay a 'tooltip'
over it if the contents has been truncated with elipses.

This bug has been around a long time including Swing 1.1beta2.

Apparently, if you set any border of a particular size on the combobox,
that border registers the mouse event but not the combobox!

Arrrggghhh!!!

-----------
James.

Jan Geldof wrote:

> I can't get a JComboBox to listen to Mouse-events. All other JComponents
> work fine with it, only this !@%$##@ JComboBox!
>

> *snip*

bruce_...@my-dejanews.com

unread,
Oct 4, 1998, 3:00:00 AM10/4/98
to
In article <3614D124...@ecmwf.int>,

Jan Geldof <jan.g...@ecmwf.int> wrote:
> I can't get a JComboBox to listen to Mouse-events.

In another thread I've posted this:
Well, I think I now know how to add a mouseListener to a
JComboBox.
You cannot just:

JComboBox mcb = new JComboBox();
mcb.addMouseListener(new MyMouseListener());

as you can with non-compound objects. This was reported as
a bug some time ago,
but it's closed and is not considered a bug now. The
suggestion is to:

mcb.getComponent(0).addMouseListener(new
MyMouseListener());

where getComponent(0) returns the first sub-component of
the comboBox, which in
the Metal L&F is the entry field and button. Presumably it
already has its own
MouseListener to control the dropdown list. You add another
to look for other
mouse events ... in my case, PopupTriggers.

And in your MouseListener class you can get the ComboBox
reference back from the
MouseEvent using getComponent().getParent() as in :

class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
Component jc = e.getComponent().getParent();
// other stuff
}
}
}

So far all this is working nicely.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

0 new messages