Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call a method from another class Java Swing?

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();
like image 765
JoJo Avatar asked Jul 13 '12 17:07

JoJo


People also ask

Can you call methods from a different class?

You can also use the instance of the class to call the public methods of other classes from another class. For example, the method FindMax belongs to the NumberManipulator class, and you can call it from another class Test.

What does pack () do in swing?

The pack() method is defined in Window class in Java and it sizes the frame so that all its contents are at or above their preferred sizes. An alternative to the pack() method is to establish a frame size explicitly by calling the setSize() or setBounds() methods.

How do you call a JFrame from another JFrame?

JFrame frame = new JFrame( "FrameDemo2" ); frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);


3 Answers

Check this modified code example :

import javax.swing.*;

import java.awt.*;

public class StickyNotes {

    private JMenuBar getMenuBar()
    {
        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);

        return menubar;
    }

    private void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("Java Sticky Notes");
        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
        frame.setJMenuBar(getMenuBar());

        // 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() {
                new StickyNotes().createAndShowGUI();
            }
        });
    }
}

Or you can modify your code a bit like this, if you really wanted to keep the JMenuBar set up in a different Class, where you can simply make an Object of the SwingMenu Class and call the method getMenuBar() by making an Object of this Class :

import javax.swing.*;

import java.awt.*;

public class StickyNotes {

    private void createAndShowGUI() {

        // Create and set up the window.
        JFrame frame = new JFrame("Java Sticky Notes");
        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 swingMenu = new SwingMenu();
        frame.setJMenuBar(swingMenu.getMenuBar());

        // 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() {
                new StickyNotes().createAndShowGUI();
            }
        });
    }
}

class SwingMenu {

    public JMenuBar getMenuBar() 
    {   
        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);

        return menubar;
    }
}
like image 79
nIcE cOw Avatar answered Oct 04 '22 22:10

nIcE cOw


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();
like image 34
Eng.Fouad Avatar answered Oct 04 '22 23:10

Eng.Fouad


Try this out:

This basically makes SwingMenu a manu bar which will help encapsulating the build of the menu bar.

public class SwingMenu extends JMenuBar{
    public SwingMenu() {
        JMenu filemenu = new JMenu("File");
        JMenu editmenu = new JMenu("Edit");

        // Build your file menu and edit menu here...

        add(filemenu);
        add(editmenu);
    }
}

Now, in your createAndShowGUI() just create a form and add the newly created menu bar to it.

private static void createAndShowGUI() {
     // Create and set up the window.
     JFrame frame = new JFrame("Java Sticky Notes");
     // other stuff...
     // Add Main Menu
     SwingMenu mainBar = new SwingMenu();
     frame.setJMenuBar(mainBar);
     // Display the window.
     frame.pack();
     frame.setVisible(true);
}
like image 28
GETah Avatar answered Oct 04 '22 21:10

GETah