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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:27:48+00:00 2026-06-14T17:27:48+00:00

I’m working on a simple gui. I can’t seem to spot the reason as

  • 0

I’m working on a simple gui.

I can’t seem to spot the reason as to why my JMenuBar Is not appearing. What am i missing?

Here is the code below.

    myMenuBar = new JMenuBar(); 
    myFileMenu = new JMenu("File"); 
    myRegisterItem = new JMenuItem("Register");
    myMenuBar.add(myFileMenu);
    myFileMenu.add(myRegisterItem);
    setJMenuBar(myMenuBar);

The full class:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.rmi.Naming;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class GUI extends JFrame{


private JTextArea recievedField;
private JButton sendButton;
private JTextField messageField; 
private JComboBox itemComboBox;
private JButton connectButton;
private JTextField userNameField;
private JPasswordField passwordField;
private JButton loginButton;
private JMenuBar myMenuBar;
private JMenu myFileMenu;
private JMenuItem myRegisterItem;


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

public GUI() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                } 

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new MainPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    });
}

public class MainPane extends JPanel {

    public MainPane() {


        myMenuBar = new JMenuBar(); 
        setJMenuBar(myMenuBar); 
        myFileMenu = new JMenu("File"); 
        myRegisterItem = new JMenuItem("Register");
        myMenuBar.add(myFileMenu);
        myFileMenu.add(myRegisterItem);


        setBorder(new EmptyBorder(4, 4, 4, 4));
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        add(new LoginPane(), gbc);
        gbc.gridy++;
        add(new ConnectPane(), gbc);
        gbc.gridy++;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add(new JScrollPane(recievedField = new JTextArea(5, 20)), gbc);

        gbc.gridwidth = 1;

        messageField = new JTextField(10);
        sendButton = new JButton("Send");

        gbc.gridy++;
        gbc.weighty = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(messageField, gbc);
        gbc.gridx++;
        gbc.weightx = 0;
        gbc.insets = new Insets(5,5,5,5);
        add(sendButton, gbc);          



    }

}

public class ConnectPane extends JPanel {

    public ConnectPane() {

        itemComboBox = new JComboBox();
        itemComboBox.addItem("Select an Item");
        connectButton = new JButton("Connect");

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5,5,5,5);
        add(itemComboBox, gbc);

        gbc.gridx++;
        gbc.weightx = 1;
        gbc.insets = new Insets(5,5,5,5);
        add(connectButton, gbc);
    }

}

public class LoginPane extends JPanel {

    public LoginPane() {

        userNameField = new JTextField(10);
        passwordField = new JPasswordField(10);
        loginButton = new JButton("Login");

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5,5,5,5);
        add(new JLabel("User name:"), gbc);

        gbc.gridx++;
        gbc.insets = new Insets(5,5,5,5);
        add(userNameField, gbc);

        gbc.gridx++;
        gbc.insets = new Insets(5,5,5,5);
        add(new JLabel("Password:"), gbc);

        gbc.gridx++;
        gbc.insets = new Insets(5,5,5,5);
        add(passwordField, gbc);

        gbc.gridx++;
        gbc.weightx = 1;
        add(loginButton, gbc);

    }

}
  • 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-14T17:27:49+00:00Added an answer on June 14, 2026 at 5:27 pm

    Your problem is you call setJMenuBar on JPanel instance this is wrong you should call:

    frame.setJMenuBar(myMenuBar);
    

    Also no need for:

    frame.setLayout(new BorderLayout());
    

    as JFrame contentPane Layout by default is BorderLayout

    i.e change your code to:

                JFrame frame = new JFrame("GUI");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPane());
                frame.setJMenuBar(myMenuBar);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
    

    Also do not use EventQueue rather SwingUtilities:

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
    
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                JFrame frame = new JFrame("GUI");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPane());
                frame.setJMenuBar(myMenuBar);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    

    Dont forget to comment out the setJmenuBar(..) in your MainPane class:

        public MainPane() {
            myMenuBar = new JMenuBar();
    
            //notice no call to setJMenuBar
    
            myFileMenu = new JMenu("File");
            myRegisterItem = new JMenuItem("Register");
            myMenuBar.add(myFileMenu);
    
            ...
         }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.