Hi all,
I wrote:
> mypopup = new PopupMenu();
> mypopup.add("Blah");
> // etc...
> mypopup.addActionListener(this);
>
> In my processMouseEvent method, I check for mouse clicks in a certain
> place, and then I try to pop up the menu by saying
>
> mypopup.show(this, X, Y);
>
> The result is this:
>
> Exception occurred during event dispatching:
> java.lang.NullPointerException: parent is null
OK, so you have to add the PopupMenu to some container. When Steve Byrne
pointed this
out, I almost kicked myself (I had gone through a similar long struggle
to get cascading
menus to work in Motif, also making some mistake with family
relationships... Wish I were
still programming a Mac, life seemed so much easier then!).
Anyway, here's what I ended up doing:
Replace
mypopup.show(this, X, Y);
with
if (mypopup.getParent() == null)
{
Container parent = getParent();
while (!(parent instanceof Window))
parent = parent.getParent();
parent.add(mypopup);
}
mypopup.show(this, X, Y);
Of course I can't do it in the constructor, since my component doesn't
have a parent then
yet, and since I feel my code should reflect my personality, I do the
add-to-Window bit
lazily.
Some people suggested adding the popup to the component itself --
haven't tried that,
I already had the above code working by the time I received those
replies, and according
to the Java Tutorial, PopupMenus can be used in any Window (doesn't say
anywhing about
other Containers, let alone Components), but I might give that a try
later. For the time
being, the above works fine in a standalone application, from
appletviewer, and from
Netscape (versions 1.1.6v4a and 4.5, respectively).
Many thanks to all who replied!
- Thomas