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, We have a created a table view using the BI query as source. the table view has say 4 fields (Serial NO., Plant key, Material and Quantity) initially before submitting the parameters, once we enter some parameters and press submit button some [More]
Hi, I am currently using 2LIS_04_P_MATNR to get details of production order. This data source gives me the details of total quantity produced on the final posting date (ie. the date on which total quantity produced is confirmed). eg. If quantity requ [More]
iTunes 10 stops working as soon as I plug in my iPod Shuffle. It will work when the iPod Shuffle isn't plugged in, but as soon as I plug it in, It sais "iTunes has stopped working" And the only thing I can do is close down iTunes. Help?when I lo [More]
When I create a web service for an o/b interface using the wizard, I need to give the URL of the pattern http://<host:<port>/XISOAPAdapter/MessageServlet?channel=<party>:<service>:<channel> Looking at the URL, I think there is s [More]
I am loading a swf into an AIR app created in CS3. In the AIR app I use HTMLLoader.createRootWindow() which works fine.. but I would like to call this function from the SWF that was loaded into the AIR app. I cannot call it directly from the swf so I [More]
Or more specifically, an indexer for my media files that doesn't include extras (such as a player - I have enough of those). I'm looking for a simple tool that will provide a searchable indexer, preferably using metadata as well as file names. Using [More]
Hi I compiled VIs in LabVIEW 6.0i to be DLL file and used it in VB. I used it smoothly until I installed LabVIEW 6.1. When I run my VB application which called that DLL, I got error that is "DDT_Cmd requires a version 6.0 (or compatible) LabVIEW Run- [More]
Hi All, My client requirement to bank Gl Account Data in my customize (bank position) report. Address of required text data is : 1. Run Tcode  FS00. 2. Select GL code from left side Bar 3. In Right side, select  Information C/A tab. 4. In G/L Text Ch [More]
I am trying out a version of iWork '09 but can see no way of saving a copy of documents in iWork'08 so that someone can read the documents who has not upgraded can read the document.You can read one of the many threads about Save As- no longer in Lio [More]
Anonymous UUID:       7F4336E4-C6DA-0238-7240-8EEE20983BFD Thu Aug  7 16:08:47 2014 panic(cpu 1 caller 0xffffff80082dc24e): Kernel trap at 0xffffff7f8a16f9f1, type 14=page fault, registers: CR0: 0x000000008001003b, CR2: 0xffffef811ed3bf00, CR3: 0x000 [More]