-
Bug
-
Resolution: Fixed
-
P3
-
1.3.0
-
beta2
-
sparc
-
solaris_7
Name: ea65 Date: 03/23/2000
PopupMenus need to be heavyweight when they extend off the edge
of the their containing windows. The calulation for this is
wrong and considers a window's frame decorations to be part
of the window, when Swing can't write on the frame decorations.
This is most serious when a popup menu is positioned with its
y-origin under the title bar of its parent's frame.
Included is a test program that demonstrates the problem.
Compile and run it. It has textfields that set the x- and
y-coords of a popup menu. Hitting the Test button causes the
popup menu to appear. By default, the coors are (100,-15)
which causes most of the first menu item to be hidden under
the frame's title bar. Another interesting value is (-4, -23)
as this will obscure both the menu's left edge and its first
item.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SwingPopupMenuBug extends JFrame {
private JPanel topPanel;
private JTextField xTF;
private JTextField yTF;
public static void main(String args[]) {
SwingPopupMenuBug x = new SwingPopupMenuBug();
}
public SwingPopupMenuBug() {
super("Swing Popup Menu Bug");
topPanel = new JPanel();
JLabel xLabel = new JLabel("X: ");
JLabel yLabel = new JLabel("Y: ");
xTF = new JTextField("100");
xTF.setColumns(10);
yTF = new JTextField("-15");
yTF.setColumns(10);
JButton testBtn = new JButton("Test");
testBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showPopup();
}
});
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
southPanel.add(xLabel, c);
c.gridx = 1;
c.gridy = 0;
southPanel.add(xTF, c);
c.gridx = 0;
c.gridy = 1;
southPanel.add(yLabel, c);
c.gridx = 1;
c.gridy = 1;
southPanel.add(yTF, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
southPanel.add(testBtn, c);
topPanel.setBorder(BorderFactory.createEmptyBorder(100, 50, 10, 50));
topPanel.add(BorderLayout.SOUTH, southPanel);
getContentPane().add(topPanel);
pack();
// setSize(400, 400);
setVisible(true);
}
public void showPopup() {
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("One"));
popup.add(new JMenuItem("Two"));
popup.add(new JMenuItem("Three"));
try {
int x = Integer.parseInt(xTF.getText());
int y = Integer.parseInt(yTF.getText());
popup.show(topPanel, x, y);
} catch (Exception e) {
xTF.setText("100");
yTF.setText("-15");
}
}
}
======================================================================