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 Friends Earlier we used FTP concept, so I received XML file and length frpm FTP function modules, but now instead of FTP I am going to use the following function modules SCMS_BINARY_TO_XSTRING SMUM_XML_PARSE for that I need to pass the lenght of t [More]
Hi experts, I need your help, when i run the package LegalConsolidation, it doesnt work and reports this error: SPRunConso Version 7.0.113.01 ERROR CSD-150 Problem extracting Data : C_REPART  ERROR CSD-160 Problem extracting Data : C_CONSO  0 Rows Ca [More]
hai Abrami, I saw in JDK\bin\demo\jfc\Tableexample. but it contans example only for populating the Table with the values from database which I finished alraedy. But I treid of storing the Table values in database using VArray. but it contradicts with [More]
Hi , I am trying to load XML document in a Dw .Is it possible to do in OWB ? if Yes .Can someone please help me . Thanks, katetAs far as my understanding goes, there is no native mechanism avialble in OWB to handle XML sources. The only possible way [More]
34.05 auto updated this am. Now when I try to sign in on my or my wife's lottery account, i simply revert to the sign in page. All works well on IE. Have downloaded and reinstalled 34.05 to a new folder, still can't sign in to the lotteryGood Evening [More]
I recently purchased a Nikon D5200 and I could not open the raw files in Elements 9.  I downloaded all updates for Camera Raw and it still did not work.  I found a question/answer with the same problem and it recommended updating the DNG Converter or [More]
Hi I live in Iran and I forgot the answer to the security question please help me thanksHow to reset your Apple ID security questions. Go to appleid.apple.com, click on the blue button that says 'Manage Your Apple ID'. Log in with your Apple ID and p [More]
Hello, i have a bold (orange unlocked), but i use the vodafone network (sfr france). can i install the BlackBerry Handheld Software for vodafone to update my phone ? is there a original rom without any operator specification ? thanks in advance.  Sol [More]
I am advised by ntl/Virgin (my isp) they don't support imap and I've certainly had some intermittent but not total failure email service since switching from PC and Outlook Express (which was pop/smtp) to my new macbook pro just a week or so ago. Adv [More]
Everytime I try to stream something from netflix I get plugin failure...My Netflix is basically worthless now. Never had this problem on Snow Leopard. It ****** me off. I have updated silverlight and still no luck! Can anyone help me.I've tried unins [More]