• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPopupmenu not working w/ JComboBox metal L&F

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Herbert Kornfeld
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Groovy!
That does the trick.
Many Thanks!
 
Herbert Kornfeld
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic