Calling a JMenuBar from another class

383 views Asked by At

I have the following code for a GUI with a button on it and code for a JMenuBar.

MenuBarTest.java

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

public class MenuBarTest extends JFrame implements ActionListener
{
    JFrame myMainWindow = new JFrame("This is my title");

    JPanel  firstPanel = new JPanel();

    JButton btnTest = new JButton("Refresh Results");

    GuiDMenuBar mbr;

    public void runGUI()
    {
        myMainWindow.setBounds(10, 10, 800, 800);
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        myMainWindow.setLayout(new GridLayout(1,1));

        createFirstPanel();

        myMainWindow.getContentPane().add(firstPanel);

        myMainWindow.setJMenuBar(mbr.GuiDMenuBar());

        myMainWindow.setVisible(true);
    }

    public void createFirstPanel()
    {
        firstPanel.setLayout(null);

        btnTest.setBounds(100, 100, 200, 50);
        btnTest.addActionListener(this);
        firstPanel.add(btnTest);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == btnTest)
        {
            System.out.println("Hi");
        }
    }

    public static void main(String[] args)
    {
        MenuBarTest mBT = new MenuBarTest();
        mBT.runGUI();
    }
}

GuiDMenuBar.java

package MenuBar;

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

public class GuiDMenuBar extends JMenuBar
{   
    JMenu m_PopUps;
    public JCheckBoxMenuItem m_p_AllPopUps,m_p_NamePopUps,m_p_TextPopUps,m_p_SizePopUps,m_p_ToolTipPopUps;

    public GuiDMenuBar()
    {
        setBorderPainted(true);
        makePopUpMenu();
    }

    void makePopUpMenu()
    {
        m_PopUps = new JMenu("Pop Ups");
        m_PopUps.setMnemonic('P');

        m_p_AllPopUps = new JCheckBoxMenuItem("Enable All Pop Ups");
        m_p_NamePopUps = new JCheckBoxMenuItem("Enable Name Pop Ups");
        m_p_SizePopUps = new JCheckBoxMenuItem("Enable Automatic Sizing Pop Ups");
        m_p_TextPopUps = new JCheckBoxMenuItem("Enable Add Contents Pop Ups");
        m_p_ToolTipPopUps = new JCheckBoxMenuItem("Enable Add ToolTips Pop Ups");

        m_p_AllPopUps.addItemListener(new ItemListener() 
        {
            public void itemStateChanged(ItemEvent e) 
            {
                if(m_p_AllPopUps.getState())
                {
                    m_p_NamePopUps.setState(true);
                    m_p_SizePopUps.setState(true);
                    m_p_TextPopUps.setState(true);
                    m_p_ToolTipPopUps.setState(true);
                }

                else
                {
                    m_p_NamePopUps.setState(false);
                    m_p_SizePopUps.setState(false);
                    m_p_TextPopUps.setState(false);
                    m_p_ToolTipPopUps.setState(false);
                }
            }
        });

        m_PopUps.add(m_p_AllPopUps);
        m_PopUps.addSeparator();
        m_PopUps.add(m_p_NamePopUps);
        m_PopUps.add(m_p_SizePopUps);
        m_PopUps.add(m_p_TextPopUps);
        m_PopUps.add(m_p_ToolTipPopUps);

        add(m_PopUps);
    }

    public void checkState()
    {
        System.out.println(m_p_AllPopUps.getState());
        System.out.println(m_p_NamePopUps.getState());
        System.out.println(m_p_SizePopUps.getState());
        System.out.println(m_p_TextPopUps.getState());
        System.out.println(m_p_ToolTipPopUps.getState());
    }
}

My aim is to add the JMenuBar to the JFrame. However when I compile I get an error saying that it can't find GuiDMenuBar(). I would greatly appreciate any help. Thanks

1

There are 1 answers

0
Hovercraft Full Of Eels On BEST ANSWER

You're trying to call a constructor as if it were a static method. Don't. Instead create your instance by calling the constructor as a constructor, assign the instance to your variable, and use it. Changes:

// GuiDMenuBar mbr;
GuiDMenuBar mbr = new GuiDMenuBar(); // create instance and assign

public void runGUI()
{
    // ....

    // myMainWindow.setJMenuBar(mbr.GuiDMenuBar());
    myMainWindow.setJMenuBar(mbr); // use instance

    myMainWindow.setVisible(true);
}

Next we'll talk about the evils of null layouts and setBounds(...)