wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JPopupmenu not working w/ JComboBox metal L&F Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPopupmenu not working w/ JComboBox metal L&F" Watch "JPopupmenu not working w/ JComboBox metal L&F" New topic
Author

JPopupmenu not working w/ JComboBox metal L&F

Herbert Kornfeld
Ranch Hand

Joined: Apr 16, 2003
Posts: 32
Hi,
I have a JPopupMenu added to a JComboBox in metal L&F and the popup will not show up. If I change the L&F to Windows I get acceptable behavior in that if I click the white part of the combo the popup will show up (if you right click the part with the arrow even in Windows L&F that does not work but I could live with that behaviour). Does anyone have a fix or work around for this - I want to use the metal L&F overall - if I could figure out a way to just use the windows L&F for just the combo that would be fine too but I don't know how to do that or if it is possible. Below is the standard code I am using. And I almost forgot the e.isPopupTrigger does not seem to be ever returning true - so I've done my testing by commenting out that if (e.isPopupTrigger())
test.

cmbLevelOne.addMouseListener(new MouseAdapter(){
//btnNew.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if (e.isPopupTrigger())
{
createPopup( e, e.getPoint() );
}
}
public void mousePressed(MouseEvent e){
// if (e.isPopupTrigger())
//{
createPopup( e, e.getPoint() );
System.out.println("called");
//}
}
});
public void createPopup(MouseEvent e, Point p)
{
popup = new JPopupMenu();
popup.add(mniNew);
popup.add(mniEdit);
popup.add(mniDelete);
popup.setInvoker (this);
popup.show(e.getComponent(), p.x, p.y);
}
Many Thanks!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

This code works in both the Metal and Windows L&F... though I would definitely consider this a "hack"... there must be some problem with the underlying ComponentUI for a Metal ComboBox.

Basically, I'm just adding the same listener to both the combo box and the first component inside the combo box.



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Herbert Kornfeld
Ranch Hand

Joined: Apr 16, 2003
Posts: 32
Groovy!
That does the trick.
Many Thanks!
Herbert Kornfeld
Ranch Hand

Joined: Apr 16, 2003
Posts: 32
Actually, I spoke to soon.
This does make the JPopupMenu work - but now I cannot view the original contents of the combo box - you can see it flicker white for a split second and then my popupmenu with actions(new, edit, delete) comes up. Now it seems to always return true for the isPopupTrigger because even if you left click on the combo my action popup will show up.
I also have an ItemListener registered against this JComboBox but I don't think that should have any bearing one way or the other.
Thanks
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

What do you mean by "now I cannot view the original contents of the combo box"... are you trying to launch the popup menu while the drop down part of the combo box is open? Or do you mean that the menu covers up the item selected in the combo box? If it's the last, just change the location the combo box gets launched. If it's the first, it would probably be better to use a JList component rather than a JComboBox.

I don't know why isPopupTrigger() is returning true for all mouse clicks... I can't get it to do that on my test code... Does it always happen, or does it only happen under certain conditions?
Herbert Kornfeld
Ranch Hand

Joined: Apr 16, 2003
Posts: 32
Well after further investigation - even when I add the listener to the first element in the combo box - the real problem is that the e.isPopupTrigger() never returns true when I right click the combo - hence the pop up never shows up.
The good news is this is not a pressing problem as my UI has changed but it is a curious problem - seems to me it is a Java bug that doesn't work in either 1.3 or 1.4 according to my tests. Or perhaps I have a faulty mouse but that is unlikely as it registers right clicks for other components.
At any rate, thanks very much for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPopupmenu not working w/ JComboBox metal L&F