JComboBox is deaf

i'm a newbie to java GUI programming, and i'm getting stumped on something that should be trivial. my JComboBox doesn't hear mouse events, even though i've added a mouselistener that works with other controls. here' the full test story.

i've got a tabbed pane in a JPanel. the tabbed pane has 2 JComboBoxes.

i have a listener class that implements both ActionListener and MouseListener. the listener is added to the first combo and works. it's used to add items to the second combo. the mouse listener is supposed to allow a user to delete items from the second combo by double-clicking or right-clicking, but when it's added to the second combo the mouse events aren't being heard at all.

i've tested it without the delete function and just an addItem function that adds to the 2nd combo the getSource().toString() of the mouse event (so i can see where it's coming from).

when i add the mouse listener to the tabpane instead of the combo, it works and adds the items to the combo if i click in the pane. but if i add it to the combo, either in addition to or instead of the tabbed pane, clicks on the combo box do not register.

can someone tell me what i'm doing wrong?

cheers<br />PR

Peter Robinson
Greenhorn

Joined: Jun 19, 2004
Posts: 3

posted Jun 20, 2004 02:15:00

0

hi again by searching old posts i've found something out. you have to attach the listener to each componeent of the combo, by looping through the return from getComponents(). that means i can now catch a mouse-click somewhere on the combo, but it doesn't tell me where in the combo the click was (so i can delete the item clicked). i'm presently just deletiing the item selected, but that's a kludge. a doubkle-click or right-click anywhere on the combo will delete the selcted item, which is tolerable, but pretty crappy. also, it doesn't seem to catch clicks on the drop-down menu, only in the main row.

Well, part of your problem is the inherent nature of a JComboBox. It's items aren't meant to be double clicked. JComboBox's items are really meant to be selected (clicked once) and an action occurs on said event.

Unless relestate is an issue I think a JList would fit your app a lot better than a JComboBox.

the reason i didn't use a list (that was my first thought) is that it isn't scrollable, and creating a scrollable pane, adding a list to it, and then adding the whole lot to a tab which is then added to a panel seemed like a heavy way to create such a simple concept. the panel has several other controls so i was worried that it would slow up the program - not on its own, but cumulatively.

Originally posted by Peter Robinson: thanks for reply gregg. i'll look into it more.

the reason i didn't use a list (that was my first thought) is that it isn't scrollable, and creating a scrollable pane, adding a list to it, and then adding the whole lot to a tab which is then added to a panel seemed like a heavy way to create such a simple concept. the panel has several other controls so i was worried that it would slow up the program - not on its own, but cumulatively.

cheers Peter

Hmmm...

I don't see the "heaviness" of this and it is my opinion that using a JList for what you are trying to do will save you a ton of time.

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com