Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

show PopupMenu in JMenuItem

Thread view: 
JessyCute - 06 Feb 2006 08:56 GMT
I want to show the popupmenu when users do the right click on the
menuitem to do something like the mozilla or internet explorer
Favorites menu which can be do right click to show the popup.
I tried this, but i always have the following problems:
1) I open the popupMenu with the popup.show( jmenuitem,
mouseEvent.getX(), mouseEvent.getY())
  I get the error : java.awt.IllegalComponentStateException: component
must be showing on the screen to determine its location

So, I do the menu stay show by overrided the doClick of the jmenuitem
to do nothing. And show the popup with the same way. I got this error.
java.lang.NullPointerException
    at
javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.grabContainer(Unknown
Source)

So, I try to do with the JMenu instead of the JMenuItem....

1.) when i open the popupMenu with
popup.show(jmenu,mouseEvent.getx(),mouseEvent.getY())

the popup is shown, but i can't access an Item in it. When I move the
mouse over the popup then the jmenu will disappears ! And the popup
never disappear!

2.) when i open the popupMenu with
popup.show(someComponent,mouseEvent.getx(),mouseEvent.getY())
the popup is shown, but the oldmenu disappears
in this case the items in the popup are accessible !

Below, this is the fragment of code.
=====================================================
                    //create popupmenu.
    popupMenu = new JPopupMenu();
    popupMenu.add( new JMenuItem("delete this link"));
    popupMenu.add( new JMenuItem("go to this link"));

    //create menubar.
    JMenuBar menuBar = new JMenuBar();

    //create favoritemenu.
    JMenu favoriteMenu = new JMenu("Favorite");
    JMenuItem link1 = new JMenuItem("http://java.sun.com");
    link1.addMouseListener(new MouseAdapter(){
    public void mouseReleased(MouseEvent e) {
           if( SwingUtilities.isRightMouseButton(e) ){ //right-click
         popupMenu.show(e.getComponent(), e.getX(), e.getY());
           } else {
              System.out.println( "left click=" + e.getSource() );
           }
                      }
    });

        favoriteMenu.add( link1 );
       
        menuBar.add( favoriteMenu );

thank's for help
Bart Cremers - 06 Feb 2006 10:36 GMT
You'll need to work in mousePressed instead of mouseReleased. That
solves the illegal component state. More work is needed to get it all
working smoothly, but with this you can continue.

Bart
JessyCute - 06 Feb 2006 11:21 GMT
Thanks, Yes. I try with mousePressed() then the illegal component state
error is gone. But the error java.lang.NullPointerException
    at
javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.grabContainer(Unknown
Source)

is come instead.
Bart Cremers - 06 Feb 2006 11:48 GMT
Using the snippet you've given me, I didn't get that exception. Build a
complete (compilable and runnable) example showing the problem and post
it here.

Bart
JessyCute - 07 Feb 2006 02:25 GMT
This is my test code, I can compile and run it. But the error occurs
when I do the right mouse click on the "http://java.sun.com" jmenuitem
of this code. Aims to show the popup window and this menuitem still
stay show. Thanks for you help.
==============================================================
public class TestSimplePopup extends JFrame{

    JPopupMenu popupMenu;

    public TestSimplePopup() {
        super();

        //create popupmenu.
       popupMenu = new JPopupMenu();
       popupMenu.add( new JMenuItem("delete this link"));
       popupMenu.add( new JMenuItem("go to this link"));

       //create menubar.
       JMenuBar menuBar = new JMenuBar();

       //create favoritemenu.
       JMenu favoriteMenu = new JMenu("Favorite");
       JMenuItem link1 = new JMenuItem("http://java.sun.com");
       link1.addMouseListener(new MouseAdapter(){
           public void mousePressed(MouseEvent e) {
                   if( SwingUtilities.isRightMouseButton(e) ){
//right-click
                       popupMenu.show(e.getComponent(), e.getX(), e.getY());
                   } else {
                       System.out.println( "left click=" + e.getSource() );
                   }
           }
       });

       favoriteMenu.add( link1 );

       menuBar.add( favoriteMenu );

       setJMenuBar( menuBar );

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       setSize(400, 400);

       show();
    }

    public static void main(String[] args) {
        new TestSimplePopup();
    }
}
==============================================================
JessyCute - 07 Feb 2006 03:20 GMT
The error occurs is :
java.lang.NullPointerException
    at
javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.grabContainer(Unknown
Source)
    at
javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.requestAddGrab(Unknown
Source)
    at
javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.stateChanged(Unknown
Source)
    at javax.swing.MenuSelectionManager.fireStateChanged(Unknown Source)
    at javax.swing.MenuSelectionManager.setSelectedPath(Unknown Source)
    at javax.swing.JPopupMenu.setVisible(Unknown Source)
    at javax.swing.JPopupMenu.show(Unknown Source)
    at
test.jessy.menu.TestSimplePopup$1.mousePressed(TestSimplePopup.java:34)
    at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown
Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
I think JPopupMenu cannot have the JMenuItem to be it's container,
Because I already read the sourcecode of the grabContainer() method in
JPopupMenu, it doesn't have the if condition for the JMenuItem. Thanks
for your help.
IchBin - 07 Feb 2006 04:19 GMT
> The error occurs is :
> java.lang.NullPointerException
[quoted text clipped - 37 lines]
> JPopupMenu, it doesn't have the if condition for the JMenuItem. Thanks
> for your help.

I am running you code with no error and looks like it is working as
designed. I am running under Eclipse 3.2., java version "1.5.0_06"

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

JessyCute - 07 Feb 2006 04:44 GMT
Thank IchBin, I try it with 1.5.0_06 now. It's work fine. That error
was gone. But the Favorite Menu item is disappear when I do right click
on this to show the popup.
I want the favorite menu item always stay show until the users click
the popup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.