Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can I have JComboBox within JPopupMenu ?

2 views
Skip to first unread message

bobjan

unread,
Sep 7, 2005, 11:31:54 AM9/7/05
to
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

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.

Oliver Wong

unread,
Sep 7, 2005, 4:21:14 PM9/7/05
to
"bobjan" <bob...@eunet.yu> wrote in message
news:1126107114.3...@g49g2000cwa.googlegroups.com...

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


HGA0...@nifty.ne.jp

unread,
Sep 7, 2005, 6:21:56 PM9/7/05
to
JPopupMenu#add() is guranteed to work with Action, JMenuItem
and String. Try using javax.swing.Popup instead.

bobjan

unread,
Sep 9, 2005, 6:21:00 AM9/9/05
to
I don't want to use javax.swing.Popup, because it doesn't implement
automatic hide() after I click somewhere else.
With JPopupMenu, on WinXP and Java 1.4.2 the code I posted throws:
java.lang.NullPointerException
at
javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.grabContainer(BasicPopupMenuUI.java:383)
at
javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.requestAddGrab(BasicPopupMenuUI.java:267)
at
javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.stateChanged(BasicPopupMenuUI.java:323)
at
javax.swing.MenuSelectionManager.fireStateChanged(MenuSelectionManager.java:161)
at
javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:87)
at javax.swing.JPopupMenu.setVisible(JPopupMenu.java:751)
at javax.swing.JPopupMenu.show(JPopupMenu.java:927)
at
javax.swing.plaf.basic.BasicComboPopup.show(BasicComboPopup.java:177)
at
javax.swing.plaf.basic.BasicComboPopup.togglePopup(BasicComboPopup.java:982)
at
javax.swing.plaf.basic.BasicComboPopup$InvocationMouseHandler.mousePressed(BasicComboPopup.java:632)
at
java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5097)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:3195)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
.
.
.
.
Hope, this will give someone a clue.

Vova Reznik

unread,
Sep 9, 2005, 9:21:08 AM9/9/05
to
Please, notify forum if you will find decision.
I have the same problem running your example on 1.4.x.
Looks like problem is in finding parent window for combobox dropdown.

HGA0...@nifty.ne.jp

unread,
Sep 9, 2005, 8:01:22 PM9/9/05
to
> it doesn't implement
> automatic hide() after I click somewhere else.
You could implement that.

Roedy Green

unread,
Sep 14, 2005, 2:12:42 AM9/14/05
to
On Wed, 07 Sep 2005 20:21:14 GMT, "Oliver Wong" <ow...@castortech.com>
wrote or quoted :

>> 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.

0 new messages