how to call a method from another class Java Swing?

JoJo picture JoJo · Jul 13, 2012 · Viewed 7.5k times · Source

I have the following SwingMenu class. package base;

import javax.swing.*;

public class SwingMenu {
    public static void main(String[] args) {
        SwingMenu s = new SwingMenu();
    }

    public SwingMenu() {
        JFrame frame = new JFrame(
                "Creating a JMenuBar, JMenu, JMenuItem and seprator Component");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menubar = new JMenuBar();
        JMenu filemenu = new JMenu("File");
        filemenu.add(new JSeparator());
        JMenu editmenu = new JMenu("Edit");
        editmenu.add(new JSeparator());
        JMenuItem fileItem1 = new JMenuItem("New");
        JMenuItem fileItem2 = new JMenuItem("Open");
        JMenuItem fileItem3 = new JMenuItem("Close");
        fileItem3.add(new JSeparator());
        JMenuItem fileItem4 = new JMenuItem("Save");
        JMenuItem editItem1 = new JMenuItem("Cut");
        JMenuItem editItem2 = new JMenuItem("Copy");
        editItem2.add(new JSeparator());
        JMenuItem editItem3 = new JMenuItem("Paste");
        JMenuItem editItem4 = new JMenuItem("Insert");
        filemenu.add(fileItem1);
        filemenu.add(fileItem2);
        filemenu.add(fileItem3);
        filemenu.add(fileItem4);
        editmenu.add(editItem1);
        editmenu.add(editItem2);
        editmenu.add(editItem3);
        editmenu.add(editItem4);
        menubar.add(filemenu);
        menubar.add(editmenu);
        frame.setJMenuBar(menubar);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

And I want to display the Menu by calling it from the this main class.

package base;

import javax.swing.*;

import java.awt.*;
import base.SwingMenu;

public class StickyNotes {

    private static void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("Java Sticky Notes");

        frame.setPreferredSize(new Dimension(5000, 5000));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        // Add Label
        JLabel label = new JLabel("Type Below");
        frame.getContentPane().add(label);

        // Add Main Menu
        SwingMenu mainBar = new SwingMenu();
        //frame.setJMenuBar(mainBar);
        //frame.getContentPane().add(mainBar);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public Container createContentPane() {
        // Create the content-pane-to-be.
        JPanel jplContentPane = new JPanel(new BorderLayout());
        jplContentPane.setLayout(new BorderLayout());
        jplContentPane.setOpaque(true);
        return jplContentPane;
    }

    public static void main(String[] args) {
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

I just cannot figure it out all morning :) How do I get the Menu Bar to show up in Java Swing?

    // Add Main Menu
    SwingMenu mainBar = new SwingMenu();

Answer

Eng.Fouad picture Eng.Fouad · Jul 13, 2012

You are creating 2 different JFrames. After creating the JFrame:

JFrame frame = new JFrame("Java Sticky Notes");

Create the menu bar and assign it to the JFrame:

JMenuBar menubar = new JMenuBar();

// ...

frame.setJMenuBar(menubar);

No need for:

SwingMenu mainBar = new SwingMenu();