Question

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);

    }

}
Was it helpful?

Solution

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);

        ...
     }

OTHER TIPS

Its because you have not set Your JMenuBar Visible.

Add this to your code

    frame.setJMenuBar(myMenuBar); // This line to be added
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top