This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JComboBox is deaf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox is deaf" Watch "JComboBox is deaf" New topic
Author

JComboBox is deaf

Peter Robinson
Greenhorn

Joined: Jun 19, 2004
Posts: 3
hi all

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
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.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Peter Robinson
Greenhorn

Joined: Jun 19, 2004
Posts: 3
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
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JComboBox is deaf
 
Similar Threads
disabling mouse effects on swing forms
image in glassPane
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
How do I capture a mouseClicked event from a JTable within a JScrollPane
Problems using JTree and JTabbedPane