public class TestApp extends JFrame{
public TestApp(String s) {
super(s);
}
public static void main(String args[]) {
final TestApp frame = new TestApp("JComboBox within
JPopupMenu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] val = {"first", "second", "thrid", "fourth"};
JComboBox box = new JComboBox(val);
final JPopupMenu popup = new JPopupMenu();
popup.add(box);
JButton button = new JButton("Show popup!");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
popup.show(frame, 50,50);
}
});
frame.setSize(300, 100);
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
it crashes! But when I change the line
popup.show(frame, 50,50);
to
popup.show(null, 50,50);
it works. I don't want to have null as the invoker, so I wonder what I
am supposed to do to prevent crash.
Thanks for posting an SSCCE. I ran the program, and it doesn't crash on
my side. I successfully got the pop up menu to appear, and could choose any
value from the dropdown box just fine. I'm running Windows XP, and Java 1.5.
Good luck with your debugging.
- Oliver
>> public class TestApp extends JFrame{
Works fine here JDK 1.5.0_04 with Win2K.
However with JDK 1.4.2_09 I get the same result as you do.
It looks like the problem is this piece of code in BasicPopupMenuUI
private void requestAddGrab(Component invoker) {
Window ancestor;
ancestor = getWindow(invoker);
MenuSelectionManager msm =
MenuSelectionManager.defaultManager();
MenuElement[] p = msm.getSelectedPath();
Component excluded = null;
for(int i = 0 ; excluded == null && i < p.length ; i++) {
if (p[i] instanceof JPopupMenu)
excluded = p[i].getComponent();
}
grabContainer(ancestor, excluded);
}
private static Window getWindow(Component c) {
Component w = c;
while(!(w instanceof Window) && (w!=null)) {
w = w.getParent();
}
return (Window)w;
}
ancestor is null.
In the JDK 1.5 version the equivalent code looks like this:
public static Window getWindowAncestor(Component c) {
for(Container p = c.getParent(); p != null; p = p.getParent())
{
if (p instanceof Window) {
return (Window)p;
}
}
return null;
}
I don't see though how that explains the problem.
Both should find your JFrame which is a JWindow and Window.
Perhaps the getParent chain does not work properly in 1.4.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.