aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Prevent Display of Drop List in JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Prevent Display of Drop List in JComboBox" Watch "Prevent Display of Drop List in JComboBox" New topic
Author

Prevent Display of Drop List in JComboBox

Gary Blomquist
Greenhorn

Joined: Dec 07, 2001
Posts: 14
When the user clicks the arrow to display the drop list of a JComboBox, I would like to check a condition and if true, prevent the drop list from displaying. I am aware of the PopupMenuListener interface. I would like to do something like the following:

public class ComboPopupMenuListener
implements PopupMenuListener {

public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
if ( combo.getItemCount() == 0 ) {
showMsg("Please create your phone book to populate combo box");

// Prevent Drop list from displaying !
}
}

// other interface methods omitted
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
here's another way, but, depending on what else you are doing,
it might be a bit too much of a hack

run the program, click the arrow button - no popup
click the add button to add an item, then reclick the arrow button - popup displays

Gary Blomquist
Greenhorn

Joined: Dec 07, 2001
Posts: 14
Thanks, Michael.

I tried to combine the two approaches so that I can display a message to the user when he/she clicks the arrow and the combo box is empty:




However, the arrow stays depressed until I click it. Shouldn't the popupMenuWillBecomeInvisible() method already be executing in the Event Dispatch Thread?
[ June 19, 2006: Message edited by: Gary Blomquist ]
Gary Blomquist
Greenhorn

Joined: Dec 07, 2001
Posts: 14
What is the correct syntax to delimit source code on this forum?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> What is the correct syntax to delimit source code on this forum?

underneath the window where you write/paste your post, there are several buttons, one is code.
Click this button and this will appear (almost, space added, so they appear)
[ code] [/ code]
paste your code between the two tags

you can go back to your earlier post, and click the 'paper&pencil' icon to
edit your post - all you do is highlight your pasted code, then click the code button
and click 'edit post' to post the edited message

for the error message, try this

Gary Blomquist
Greenhorn

Joined: Dec 07, 2001
Posts: 14
Thanks, Michael !
Gary Blomquist
Greenhorn

Joined: Dec 07, 2001
Posts: 14
The combo box has no border and the arrow is kind of faded. I know I can invoke setBorder() on the combo box. Is there some way set to the look and feel so that the arrow look correct?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try extending the metal ui (instead of the basic one)

class MyUI extends javax.swing.plaf.metal.MetalComboBoxUI
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Prevent Display of Drop List in JComboBox