Sitemap

Amicuk Programming Answers

JPopupMenu showing only last JMenuItem

-0001-11-30   Views:0

Advertisement

Hello, I have a strange problem with JPopupMenu - only 1 JMenuItem (last one) is shown in the menu although there are 2 of them there. Menu is created by following sequence: JPopupMenu popup = new JPopupMenu; JMenuItem item1 = new JMenuItem(); item.s

Hello,
I have a strange problem with JPopupMenu - only 1 JMenuItem (last one) is shown in the menu although there are 2 of them there.
Menu is created by following sequence:
JPopupMenu popup = new JPopupMenu;
JMenuItem item1 = new JMenuItem();
item.setText( "ft" );
popup.add( item1 );
JMenuItem item2 = new JMenuItem();
item.setText( "m" );
popup.add( item2 );
Menu is activated (shown) after a button is pressed in the code called from button ActionListener:
// parameter is ActionEvent aEvent
popup.show( ( Component ) aEvent.getSource(), 0, 0 );
popup.pack(); // does not affect the behaviour, if omitted
// dubugging: trying to make sure menu has more than 1 element           
MenuElement[] elems = popup.getSubElements();
for( MenuElement elem: elems )
System.out.println( elem );
The code works, menu appears, but only with last item.
The debug printout shows, that menu has 2 elements:
javax.swing.JMenuItem[,0,0,0x0,layout=java.awt.BorderLayout,alignmentX=0.0,align
mentY=0.0,[email protected],flags
=264,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disable
dSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,ri
ght=2],paintBorder=true,paintFocus=false,pressedIcon=,rolloverEnabled=false,roll
overIcon=,rolloverSelectedIcon=,selectedIcon=,text=ft]
javax.swing.JMenuItem[,1,3,39x20,layout=java.awt.BorderLayout,alignmentX=0.0,ali
gnmentY=0.0,[email protected],fla
gs=264,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disab
ledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,
right=2],paintBorder=true,paintFocus=false,pressedIcon=,rolloverEnabled=false,ro
lloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=m]
Do you have an idea, why just last item is shown ?
When I changed
popup.show( ( Component ) aEvent.getSource(), 0, 0 );
to
popup.show( mainFrame, 0, 0 );
the menu appeared in the new location, but on change of behaviour ( my first idea was, that menu is clipped to the size of button).
Thanks.
Martin

The replay answer
Advertisement
Don't show us bits and pieces of code. Show us a demo program of how you are creating and attempting to show the popup menu. The code you post may not be related to the problem.
Also, don't forget to use the "formatting tags" so the code retains its original formatting.

Go to See the other 2 answers

JPopupMenu showing only last JMenuItem

Category:DefaultRelease time:-0001-11-30Views:130

Hello, I have a strange problem with JPopupMenu - only 1 JMenuItem (last one) is shown in the menu although there are 2 of them there. Menu is created by following sequence: JPopupMenu popup = new JPopupMenu; JMenuItem item1 = new JMenuItem(); item.s[More]

Disabled JMenuItem doesn show animated gif

Category:DefaultRelease time:2015-10-11Views:130

Hi there I would like to have a popup menu with actions that are enabled after they have finished with some background task taking some time. Basically this works fine for the enabling action on a shown popup. However, adding an animated gif as the i[More]

How to get the location of the JMenuItem on the screen in actionPerformed

Category:DefaultRelease time:2015-10-11Views:130

How can I get the location of the JMenuItem on the screen in actionPerformed. When I use the function getLocationOnScreen() on JMenuItem or the object retruned by getComponent() I get IllegalComponentStateException. I have figured out that inside act[More]

Change background color in a JPopupMenu?

Category:DefaultRelease time:-0001-11-30Views:130

Hello. I've been trying to change the background color of a JPopupMenu, so far with no success. I've tried: + invoke setBackground() along with setOpaque() + subclass JPopupMenu and provide the following method:      public void paintComponent(Graphi[More]

Show PopupMenu in JMenuItem

Category:DefaultRelease time:-0001-11-30Views:130

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: jav[More]

An array of JPopupMenu

Category:DefaultRelease time:-0001-11-30Views:130

Hi Guys, Is it possible to create an array of JPopupMenu using an array JMenuItem. If it can be used, is this right: - JPopupMenu ar[] = new JPopupMenu[4]; JMenuItem it[] = new JMenuItem[12]; How can I put the menu items in to popup menus. for instan[More]

JPopupMenu with JCheckBoxMenuItmes

Category:DefaultRelease time:-0001-11-30Views:130

Hi, I have a JPopupMenu with JCheckBoxMenuItems and JMenuItems. The default behaviour of the JPopupMenu is, that it closes on selection of a MenuItem. This behaviour is not really nice for the CheckBoxMenuItems ! Does anyone know a workaround? Thanks[More]

Java JPopupMenu doesn't work on OSX

Category:DefaultRelease time:-0001-11-30Views:130

Using OSX 10.6.4 and Java 1.6.0_22, the JPopupMenu doesn't appear to work. Simple code: public static void main(String args[]){ final JPopupMenu popup = new JPopupMenu(); popup.add(new JMenuItem("first")); JMenu sub = new JMenu("sub");[More]

Display 2 JPopupMenu at the same time

Category:DefaultRelease time:-0001-11-30Views:130

Hi, I need to display a JPopupMenu when I make a right click on an MenuItem from another JPopupMenu (and no I don't want a sub-menu, except if there's no item for this sub menu in the first menu). My problem is that when I display the 2nd popup menu[More]

JPopupMenu not closing after menu selection?

Category:DefaultRelease time:-0001-11-30Views:130

I have JPopupMenu with a JMenuItem added to it, and in the JMenuItem.mousePressed event I have this code: new NewTreeNodeDialog(this, foldersTree, treeModel,"Enter folder name",true).show();So I simply display a dialog where I want some input (t[More]

JPopupMenu or mouseReleased problem

Category:DefaultRelease time:-0001-11-30Views:130

Java rookie here so bear with me. Trying out a swing popupmenu by right clicking I get: java.lang.NullPointerException at PresentingLife$MouseWatcher.mouseReleased(PresentingLife.java:580) every time I release the mouse button in my jpanel and the po[More]

Hot
hi all, plz correct this revert back whan i executing every time it will show internal table itab is no long enough. plz rectify the errirs and send back it. ABLES:     zfm_kfz. type-pools: slis.                                 "ALV Declarations *Dat [More]
Hello everyone. I have to set up an Oracle application on AIX 5.3 which works only with Oracle 8.1.7.4, but Oracle doesn't support 8th version anymore. I need to test the performance of application on new hardware platform. Does anyone know there can [More]
Hello! I want to know if I can develop a 100% object oriented data base in Oracle 10g or 11? I have reed the "Oracle Database 11g The Complete Reference" chapter 41 "Advanced Object- Oriented Concepts" and I still have the doubt if I c [More]
Hi, I did archiving for PR and PO, but in retrieval I am not getting the detailed data such as Header Text (ie.in archived file). The requirement of the client is that they want all the data which we see in display mode of PR or PO.This is in version [More]
Hi! I'm having problems printing pdf-files to paper (printer HP LaserJet 2300). Using the fit-to-paper option the on-screen preview of the file looks like it should. On the paper that comes out of the printer it looks like the fit-to-paper command is [More]
We've just produced two new videos for Oracle's MySQL 5.6 Certifications: "Oracle MySQL 5.6 Certification: What's New for Database Administrators" (3:00) "Oracle MySQL 5.6 Certification: What's New for Developers" (2:54) Watch as MySQL [More]
My music collection is stored on an G4 (MDD), which has a 2005 era keyboard attached (looks like: Link). On my Macbook F7, F8 & F9 control iTunes, regardless of what else is open. Is there a way of assigning keys to control iTunes when it's not the f [More]
Hi, I got my new HS-250 Headset, but I don't see it in the Bluetooth discover. My Bold 9700 works perfectly and show me the HS-250 after device search. The Q10 not. My hands-free device in my car can be configured and is working after device search a [More]
I was able to change the color once via SP2013 color pallet but my company has decided to move everything to CSS due to the number of sites that we will have to create. Is there a way to change the image to the right of the search box?Hi Grady,  Shar [More]
Guys, I've just added a external server ip address to go through our vpn tunnel and then out the remote site internet connection. How can I check that this is the path the packet is taking? If I do a tracert then I can't see the path? ThanksWell, you [More]