1 year ago
#362915
user2864650
Java Swing Menu, Popup, Combobox GUI Problem
When I click on any popup menu that extends outside the bounds of my frame or dialog, whether it's from a combobox or a menu it doesn't matter, my entire gui goes funky. It starts replicating panels but only visually. Everything remains usable just hidden behind a mess. I've reverted my program to bare bones, 1 frame, 2 buttons, a menu, and 1 dialog. I shrink the frame so when I click the menu the popup extends down past the bottom of the frame, click the menuitem which brings up a dialog with a couple of buttons. The first time is fine but after the second time things go very weird. Everything works perfectly fine if the popup stays within the frame. Please tell me other people experience this. Any info would be appreciated. Google knows nothing about this btw :)
I removed all unnecessary imports, I made sure all gui creation/editing is on EDT, I broke down extended classes and use only base classes, I tried hiding and reusing dialogs, I tried disposing, I found this UIManager.put("PopupMenu.consumeEventOnClose", false) which didn't make much sense but I tried it anyway, nothing worked.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test implements ActionListener
{
JFrame frame;
JDialog dialog;
public Test()
{
int width = 336, height = 225;
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.frame = new JFrame();
this.frame.setTitle("Frame");
this.frame.setLocation((dim.width - width) / 2, (dim.height - height) / 2);
this.frame.setSize(width, height);
JMenuBar menuBar = new JMenuBar();
JMenu tools = new JMenu("Tools");
JMenuItem settings = new JMenuItem("Settings");
settings.addActionListener(this);
tools.add(settings);
menuBar.add(tools);
this.frame.setJMenuBar(menuBar);
JPanel root = (JPanel)this.frame.getContentPane();
root.setLayout(new BorderLayout());
JPanel flow = new JPanel();
JPanel center = new JPanel(new GridLayout(1, 2));
JLabel label = new JLabel("Username: ");
center.add(label);
JTextField field = new JTextField("", 10);
center.add(field);
flow.add(center);
root.add(flow);
JPanel buttonPanel = new JPanel();
JButton cancel = new JButton(" Cancel ");
cancel.addActionListener(this);
buttonPanel.add(cancel);
root.add(buttonPanel, BorderLayout.SOUTH);
this.frame.setVisible(true);
}
public static void main ( String[] args )
{
new Test();
}
@Override
public void actionPerformed(ActionEvent e)
{
switch(e.getActionCommand())
{
case " Cancel ":
{
this.frame.dispose();
System.exit(0);
}
case "Settings":
{
if (this.dialog == null)
{
this.dialog = new JDialog(this.frame, true);
this.dialog.setTitle("Settings");
this.dialog.setSize(400, 300);
this.dialog.setLocationRelativeTo(this.frame);
this.dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
JPanel sRoot = (JPanel)this.dialog.getContentPane();
sRoot.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
JButton close = new JButton(" Close ");
close.addActionListener(this);
buttonPanel.add(close);
sRoot.add(buttonPanel, BorderLayout.SOUTH);
}
this.dialog.setVisible(true);
break;
}
case " Close ":
{
this.dialog.setVisible(false);
break;
}
default:
{
break;
}
}
}
}
Just shrink the main frame so the settings menu expands past the bottom of the frame (might have to open and close the settings dialog twice)
Then close the settings dialog, expand the main frame and hover over the button and/or the textfield.
It happens to me without fail.
java
swing
user-interface
popup
popupmenu
0 Answers
Your Answer