Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:52:43+00:00 2026-06-16T04:52:43+00:00

I am trying to build a JMenu dynamically right when clicking in it (I’m

  • 0

I am trying to build a JMenu dynamically right when clicking in it (I’m only getting an empty menu), below is my code.

    final JMenu JMWindows = new JMenu("Opened Windows");                
    JMWindows.addActionListener(new ActionListener() {          
        @Override
        public void actionPerformed(ActionEvent e) {
            for(JInternalFrame ji : desktop.getAllFrames())
            {
                JMWindows.add(ji.getTitle());
            }               
        }
    });

I’ve realized that the actionperformed is never called, the JMenu is inside a JMenuBar. What could be the problem ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:52:44+00:00Added an answer on June 16, 2026 at 4:52 am
    • You add ActionListener to JMenu this cannot be done for JMenu use MenuListener and set it via JMenu#addMenuListener(..)

    • Also you need to call revalidate() and repaint() on JMenu instance after adding/removing components from it or the changes will not be reflected (we could also call this on the containers instance i.e JMenuBar or JFrame, but we know only 1 specific JMenu will change so no need IMO).

    • Please watch your variable naming schemes JMWindow should be jmWindow/jMWindow variables always begin with no caps and the 1st letter of every new word thereafter gets capitalized (besides for constants and enums).

    Here is an example using MenuListener:

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.SwingUtilities;
    import javax.swing.event.MenuEvent;
    import javax.swing.event.MenuListener;
    
    public class Test {
    
        private JDesktopPane jdpDesktop;
        private static int openFrameCount = 0;
        final JFrame frame = new JFrame("JInternalFrame Usage Demo");
    
        public Test() {
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // A specialized layered pane to be used with JInternalFrames
            jdpDesktop = new JDesktopPane() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(500, 500);
                }
            };
    
    
            for (int i = 0; i < 3; i++) {
                createFrame(); // Create first window
            }
    
            frame.setContentPane(jdpDesktop);
    
            frame.setJMenuBar(createMenuBar());
    
            // Make dragging faster by setting drag mode to Outline
            jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
    
            frame.pack();
            frame.setVisible(true);
        }
    
        protected JMenuBar createMenuBar() {
            JMenuBar menuBar = new JMenuBar();
            JMenu menu = new JMenu("Frame");
            menu.setMnemonic(KeyEvent.VK_N);
            JMenuItem menuItem = new JMenuItem("New IFrame");
            menuItem.setMnemonic(KeyEvent.VK_N);
            menuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    createFrame();
                }
            });
            menu.add(menuItem);
            menuBar.add(menu);
    
    
            final JMenu jmWindows = new JMenu("Opened Windows");
            jmWindows.addMenuListener(new MenuListener() {
                @Override
                public void menuSelected(MenuEvent me) {
                    jmWindows.removeAll();//remove previous opened window jmenuitems
                    for (JInternalFrame ji : jdpDesktop.getAllFrames()) {
                        JMenuItem menuItem = new JMenuItem(ji.getTitle());
                        jmWindows.add(menuItem);
                    }
    
                    jmWindows.revalidate();
                    jmWindows.repaint();
                    jmWindows.doClick();
    
                }
    
                @Override
                public void menuDeselected(MenuEvent me) {
                }
    
                @Override
                public void menuCanceled(MenuEvent me) {
                }
            });
            menuBar.add(jmWindows);
    
    
            return menuBar;
        }
    
        protected void createFrame() {
            Test.MyInternalFrame frame = new Test.MyInternalFrame();
            frame.setVisible(true);
            // Every JInternalFrame must be added to content pane using JDesktopPane
            jdpDesktop.add(frame);
            try {
                frame.setSelected(true);
            } catch (java.beans.PropertyVetoException e) {
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Test();
                }
            });
        }
    
        class MyInternalFrame extends JInternalFrame {
    
            static final int xPosition = 30, yPosition = 30;
    
            public MyInternalFrame() {
                super("IFrame #" + (++openFrameCount), true, // resizable
                        true, // closable
                        true, // maximizable
                        true);// iconifiable
                setSize(300, 300);
                // Set the window's location.
                setLocation(xPosition * openFrameCount, yPosition
                        * openFrameCount);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to build Xuggler under Windows. Xuggler is core native code functions wrapped into
Trying to build a simple helloWorld android/java application with jni c code. I am
I'm trying to build a particular JMenu . I want a JMenuItem with JMenu
I am trying build an AST for the below grammer using ANTLR condition_in :
trying to build a query from a few tables here, and getting stumped on
I am trying build a jQuery EasyUI datagrid or treegrid out of a large
I was trying Build For Archiving application (from Titanium Mobile) with xCode 4.4, but
I'm trying build a method which returns the shortest path from one node to
I'm trying build an App Engine connected Android application and am having some problems
I'm trying build a MVC framework, but I'm confused about manage themes. Well... I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.