Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prevent Display of Drop List in JComboBox

 
Gary Blomquist
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the correct syntax to delimit source code on this forum?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Michael !
 
Gary Blomquist
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try extending the metal ui (instead of the basic one)

class MyUI extends javax.swing.plaf.metal.MetalComboBoxUI
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic