Swing component which is always on top - alternative to JPopupMenu - java

I've been trying to create a custom combo box component but I've been having difficulty triggering the visibility of the popup. What other component (other than JPopupMenu) can I use to display the combo box's items regardless of the size of the container of the combo box?
I've been trying to work my way through the source for JComboBox, but I can't seem to figure out how they show / hide the popup.
How can I do this?

Use a JWindow or undecorated JDialog.

Related

How to create an element

I want to create this element in swing:
As you can see the element is a small grid of buttons which appears when i click on another button. I've tried to use JComboBox to create this element. But as far as i know, JComboBox can just render an image of the some button, but it will not behave itself as a button. Also I couldn't set a GridLayout to JComboBox.
I also tried to create some JDialog, but I suppose that's bad idea.
So the question is: Which swing's component should I use to create mentioned element?
You could use dialog in the best way to achieve this.
JDialog dialog = new JDialog(owner);
dialog.setModalityType(Dialog.ModalityType.MODELESS);
dialog.setUndecorated(true);
You could set Modality type to Modeless to avoid parent frame lock and set undecorated true to make jdialog without close option. But note that you need to close the dialog from program.

Size JDialog so correct size when invisible components made visible

I have a JDialog that contains a large number of options and it was all working fine, however I have changed it so that by default some options are non visible until the unless the user clicks on the Show Advanced button.
When they they do this the options are displayed but because the dialog is not tall enough since it was sized based on those options being hidden a vertical scrollbar is added.
I want the dialog to be sized so large enough for when the advanced options are enabled. I have attempted this by creating the dialog with the advanced option displayed, calling pack() to fit based on advanced options being visible
this.pack();
showAdvancedAction.actionPerformed(null);
and then afterwards calling method to make advanced options invisble.
But still when diplayed the dialog is only large enough for when the options are not shown so when click on Show Advanced the dialog adds scroll bar again.
How can I resolve this.
this.pack();
showAdvancedAction.actionPerformed(null);
You have the order reversed. You need to pack the frame AFTER the components have been made visible.
showAdvancedAction.actionPerformed(null);
this.pack();
In java swing you can add buttons or components over the panel. but when you try to resize these then you need to use Grid Layout/ GridLayout class. Try using Grid Layouts and arrange your controls by providing X, Y co-ordinate values. It will definitely work.

Adding a JScrollPane to JPopupMenu

I'm trying to add a JScrollPane with a custom component in it to a JPopupMenu. It works perfectly, it also shows the vertical scroll bar when needed. But when I try to scroll using the mouse wheel, the popup menu disappears.
Anybody got an idea?
(If it's possible I want to use a JPopupmenu, because it is well integrated into the different OS. I know I can build a Window on my own which looks like a popup menu, but it is too much workaround..)
With MouseWheelEvent e, you should add the following:
e.consume();
to "consume this event so that it will not be processed in the default manner by the source which originated it."
It worked for me.

How to move JPopup up to Glasspane

I am putting a combobox component on the glasspane for users to select from a list of items. When the drop down list is clicked though the JPopupMenu is hidden behind other parts of the component on the glasspane since the popups are displayed on the LayeredPane.
I would like to find out how to make the popup display on the glasspane with the component. I have tried JPopupMenu.setDefaultLightWeightPopupEnabled(false) before the frame was initialized but it seems that makes the popup not display at all anywhere and I am not sure why.
Any advice on how to get the popup to display on the glasspane instead of the jlayeredpane would be helpful. I searched but most responses seem to related to pushing events down that are captured on the glasspane.
I am actually using a JideAutoCompletionComboBox which extends JComboBox.
Edit for question: I have a system wide (my app has a bunch of workspaces on tabs) popup type system. I would like to not use a Modal dialog for this and just use the glasspane. The component is basically for creating a message but one of the subcomponents is a combobox. Effectively you can think of the whole component like a popup though, but using the glasspane.
I don't like little floating windows that users can screw up by pushing around.
JDialog dialog = new JDialog(...);
dialog.setUndecorated(true);

Swing: Floating panel next to the selected item in a JComboBox

I've created an app with a small window (the size of a combo box). I need to create a floating panel that sits outside the window, next to the selected item in a JComboBox. (See attached image).
I've been reading about the JComboBox.setRenderer(customRenderer) etc. But was just wondering before I go down this path, whether it is at all possible to render something outside the window. I suspect it is, as the combobox itself manages to render it's popup list outside the window.
I'm very new to Swing, so any advice would be appreciated.
It's not possible with the custom renderer since Swing components are light weight. That is, Java is given a native window and all the component drawing takes place in that window. In your case, that is the JFrame containing the combo box.
What you can do though is create a new undecorated window and set it's location accordingly and draw whatever you want inside it.
EDIT: When Java needs to paint outside it's window bounds (like the case of pop up messages or combo boxes drop downs) if the component falls inside the bounds it uses the swing light weight mechanism. But if the component falls out side the bounds it is automatically substituted with a awt heavy weight component that has it's own native drawing surface outside the active window.
I've implemented similar idea using combobox renderers and tooltips on them. Content of every item's tooltip can be customized and rendered using HTML. Location of the tooltip can be set outside of the item itself thus creating design very similar to the one presented in your question.
Here is the starting point for you:
http://www.java2s.com/Code/Java/Swing-Components/ToolTipComboBoxExample.htm

Categories