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 8234505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:30:09+00:00 2026-06-07T18:30:09+00:00

I have the following SwingMenu class. package base; import javax.swing.*; public class SwingMenu {

  • 0

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();
  • 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-07T18:30:11+00:00Added an answer on June 7, 2026 at 6:30 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code: public class reader extends Activity { WebView mWebView; String mFilename;
Have following kernel function: private static String programSource = __kernel void sampleKernel(__global float *Y,
I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
Have following code: public interface ITest { string St1 { get; } } public
I have following structure of an HTML document: <body> <div class=main> <div class=left></div> <div
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
I have following files and directories in my project root directory main.py bar.py foo
I have following class which reads and writes an array of objects from/to a
I have following example string that needs to be filtered 0173556677 (Alice), 017545454 (Bob)
Have following String built SQL query: StringBuilder querySelect = new StringBuilder(select * from messages

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.