javajmenusplitpane

When I run this code only the Splitpane shows up but not the JMenu


When I run this code the interface will be split however the code I've used to make the JMenu doesn't show up and there are no errors. I've tried everything but nothing appears to be working.

public class Task1panel extends JFrame {
    public static JMenuBar setupMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu1 = new JMenu("menu");
        menuBar.add(menu1);
        JMenuItem menuItem1 = new JMenuItem("Item 1", KeyEvent.VK_1);

        menu1.add(menuItem1);

        menuItem1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });

        return menuBar;
    }

    public Task1panel () {
        setTitle("GUI");
        setSize(150, 150);

        //The code below creates the two panels of the UI And they are both labelled with names.
        JPanel jsp1 = new JPanel();
        JPanel jsp2 = new JPanel();
        JLabel j1 = new JLabel("Left Side");
        JLabel j2 = new JLabel("Right Side");

        //jsp1 and jsp2 work together with the code above to create the two panels of the interface by adding "J1" and "J2" this allows 
        //both sides to be labelled and appear on the UI        
        jsp1.add(j1);
        jsp2.add(j2);   

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, jsp1, jsp2);

        splitPane.setOneTouchExpandable(true);
        getContentPane().add(splitPane);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // build and show your GUI               
                Task1panel sp = new Task1panel ();
                sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                sp.setVisible(true);
            }
        });
    }
}

There are no errors in this code, everything should work, however nothing appears to be working.


Solution

  • It looks like you're missing a call to setJMenubar(setupMenu());Actually, setMenu() isn't called.

    At the end of your Task1panel constructor, try adding the call as I did in the example below:

    Task1panel () {
         //...
         setJMenubar(setupMenu());
    }