JComboBox popup menu not appearing
I have a JComboBox inside a JPanel (which itself is nested within a few other JPanels). It's populated with members of an enum. I'm running into a problem where the popup menu doesn't appear when I click the expand button.
Here's the information I've gathered so far:
1) The first click on the expand button does nothing. The second click highlights the contents of the box, but the popup still doesn't appear.
2) Once I've clicked the button and given it focus, up/down keystrokes cycle through the entries correctly.
3) I've tried adding a PopupMenuListener to print out events, and the events fire in unexplained ways:
mouse down: popupMenuWillBecomeVisible fires
mouse up: nothing
mouse down: popupMenuWillBecomeInvisible fires
mouse up: nothing
Here's my initialization code for the JComboBox:
comboBox = new JComboBox();
comboBox.setPreferredSize(new Dimension(175, 30));
comboBox.setMaximumSize(new Dimension(175, 30));
comboBox.setAlignmentX(0.5f);
comboBox.addItem(Enum.Value1);
...
parentPanel = new JPanel();
parentPanel.setLayout(new BoxLayout(parentPanel, BoxLayout.X_AXIS));
parentPanel.setMaximumSize(new Dimension(37267, 50));
... add some other stuff to parentPanel ...
parentPanel.ad开发者_如何学Pythond(comboBox);
Does anyone have any idea why the popup menu might not be appearing?
Thanks for the help!
I've had the similair problem with Java (1.6) on mac (10.5+) and it was due to the dialog the jCombobox appeared in was modal.
In my case the dropdown actually DID appear but as it was not that large it was not obvious that it was drawn behind the dialog :) so if you suspect this to be the case verify it by making the dropdown be long enough to appear below the component hiding it.
- populate the dropdown with many items.
- set its Specification to return a large enough number on "getMaximumRowCount".
Hope it helps.
I had the same issue and was wondering what was wrong, because usually it works and that kind of bug would be very very critical.
So I deleted everything that I just had a main-method with an JDialog with just an JComboBox within it.
The moment I had deleted everything I realized what I've done wrong: I used getRootPane()
instead of getContentPane()
.
I usually use ESC for closing the dialog and add the KeyStroke to the rootPane. That was the reason for my careless mistake.
I hope that will help the next ones doing that mistake : )
I'm not sure if this is happening in your case, but a good portion of UI-related issues are due to the UI code not being run in the Event Dispatching Thread. Swing mandates that all code should be run in this thread or else you might suffer issues with GUIs not looking right or events not firing off properly, etc. If you are running all of this code in the Event Dispatching Thread then you can disregard this, otherwise check out this link:
http://download.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html
精彩评论