• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPopupMenu: add JComboBox, close via JButtons

 
Peter Kolb
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am working on a GUI for which I wanted to have "roll menus", i.e. the user clicks on an area and a dialog opens in which the user can do stuff. If finished, registered listeners should be informed if anything interesting has happened.
I have written an editor class that extends JButton. Mouse events are successfully captured, i.e. no event is fired but a dialog is opened instead. If the user wants submit the changes, an action event is generated "manually" and fired. So far so good.
If have first used a modal JDialog. That worked, but I didn�t like the (superfluous) title bar. Since I couldn�t find any hint how to get rid of that or at least make it smaller programmatically, I then tried a JPopupMenu.
That looks good but doesn�t work.
When I run the code below, firstly I find that the JPopupMenu vanishes if I open the JComboBox. Secondly, if the "acccept" or "cancel" buttons are clicked, they freeze and the JPopupMenu is not closed.
If anybody can reproduce this behaviour or even better has a solution to hide the JPopupMenu, I would be verys grateful.
Here is the code. Enjoy.
Regards and thanks, Paradoxoff
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> If have first used a modal JDialog. That worked, but I didn�t like the
> (superfluous) title bar. Since I couldn�t find any hint how to get rid of that...

couple of ways - with/without border

 
Peter Kolb
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks, Michael.
I had tried the JDialog.setUndecorated(true) already but on a dialog that was created from a JOptionPane.createDialog() which finally ended up in an IllegalComponentStateException.
I will probably construct a custom undecorated JDialog. Since the dialogs for some editors will be quite small I will omit the usual "OK" and "Cancel" buttons and replace them with JButtons containing small appropriate icons.
I would like to make these icons a part of the JDialog by placing them in the NORTH of the JDialog�s BorderLayout and thus "fake" some kind of title bar with buttons. What I would like to know is whether there is a way to overwrite the component residing at d.getRootPane().getComponent(1) with a custom one.
BTW, concerning the strange JPopupMenu behaviour: I have switched from a JPopupMeu to a Popup created from the PopupFactory. However, in both cases it turned out that the ActionEvents fired if the accpet or cancel buttons in the JPopupMenu or Popup are pressed do not make it through to the listener (i.e. the BaseEditor). I have also found that I am not able to navigate by keyboard through a JList that I placed in the popup.
Overall it seems that the JPopupMenu and the Popup from the PopupFactory capture events in a way that I have not understood. Any hints/thoughts about this?
Thanks and regards, Peter
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> What I would like to know is whether there is a way to overwrite the
> component residing at d.getRootPane().getComponent(1) with a custom one.

overwrite would be out, the MetalTitlePane class is private, and if you used
Windows L&F, well, enough said...

you could always build your own titlePane look-a-like and simply add it to
the undecorated dialog's BorderLayout.NORTH
 
Peter Kolb
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Work finished. It turned out that a plain JPanel w/o any extra color settings looks by far best (at least IMHO). Just put an EtchedBorder around it and that�s it. Any further work (like setting its foreground to SystemColor.activeCaption) made it look less java-ish.
The only thing that could happen is that something else gets placed in the contentPane�s BorderLayout NORTH but thats theory. For now it looks and works perfectly fine!
Thanks a lot!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> The only thing that could happen is that something else gets placed in the
> contentPane�s BorderLayout NORTH but thats theory.

if you want to get a little fancy with it, you could override the panel's
add(component,constraint) and check if the constraint is borderlayout.NORTH,
and if so, throw an error etc
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!