Search code examples
javaswingjmenuitemjmenubar

Same menu opened in two different JFrames works only on the last one


I'm trying to write a simple Paint application in which the user will be able to open new JFrames (in new Threads) with Drawing Panels, and each of those frames will have a JMenuBar on top, however, only the last open frame has a functional menu bar, all the remaining (open) frames show menu but the menu doesn't work (nothing happens when I click). Does anyone know how to fix this?

I've simplified the code to leave only the sections regarding JMenuBar.

The code consists of the following classes:

Main.java

package sample;

public class Main {

    Main() {

        MainFrameThread.getMainFrameThread().run();

    }//end of Main()

    public static void main(String[] args) {
        new Main();
    }

}//end of Main class

TopMenu.java

package sample;

import javax.swing.*;

public class TopMenu extends JMenuBar {

    private JMenu menu_File;
    private static JMenuItem menu_New;

    public static JMenuItem getMenu_New() {
        return menu_New;
    }

    public TopMenu() {

        menu_File = new JMenu("File");
        menu_New = new JMenuItem("New");
        this.add(menu_File);
        menu_File.add(menu_New);

    }//end of TopMenu()

}//end of TopMenu extends JMenuBar

MainFrameThread.java

package sample;

public class MainFrameThread extends Thread {

    private static MainFrameThread mainFrameThread = new MainFrameThread();

    public static MainFrameThread getMainFrameThread() {
        return mainFrameThread;
    }

    public MainFrameThread() {}

    @Override
    public void run() {

        MainFrame mainFrame = new MainFrame();

    }//end of public void run()

}//end of public class FrameSizeDialogThread

ActionController.java

package sample;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ActionController {

    private static ActionController actionController = new ActionController();
    private ListenForMenu listenForMenu = new ListenForMenu();

    public static ActionController getActionController() {
        return actionController;
    }

    public ActionController() {}

    public void clickOnMenu(TopMenu topMenu) {
        TopMenu.getMenu_New().addActionListener(listenForMenu);
    }

    //listener for menu
    public class ListenForMenu implements ActionListener {
        public void actionPerformed(ActionEvent ev) {

            if(ev.getSource() == TopMenu.getMenu_New()) {
                MainFrame newMainFrame = new MainFrame();
            }//end of if(ev.getSource() == TopMenu.getMenu_New())

        }//end of public void actionPerformed(ActionEvent ev)
    }//end of public class ListenForMenu

}//end of ActionController class

and MainFrame.java

package sample;

import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame {

    public MainFrame() {

        JFrame frame = new JFrame("Paint Application");

        //creating menu
        TopMenu topMenu = new TopMenu();
        ActionController.getActionController().clickOnMenu(topMenu);
        frame.setJMenuBar(topMenu);

        //frame properties
        frame.setSize(800, 600);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }//end of public MainFrame()

}//end of public class MainFrame

I'm stuck, nothing works, regardless where I initialise the MainFrame.java. Does anyone see the mistake???


Solution

  • however, only the last open frame has a functional menu bar

    Swing components can't be shared. A Swing component can only have a single parent. So for every child window you will need to create a new JMenuBar and JMenu and JMenuItem.

    However, the Action used by the JMenuItem can be shared.

    private static JMenuItem menu_New;
    
    public static JMenuItem getMenu_New() {
        return menu_New;
    }
    

    None of the variable or methods related to the menus should be static. Again, you need to create a unique instance of each.