Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JMenuBar not visible....

843806Aug 27 2008 — edited Aug 27 2008
I'm trying to learn the GridBagLayout and I've come across a small problem. I've got a JFrame, and I want to add a tabbed pane to it and a couple of buttons.
For some reason, my JMenuBar doesn't appear on the screen..... If anyone could point me in the direction of what I'm doing wrong, that would be great.
Here's my code:

Set up the menu:
private void initComponents(){
        // Set up the menus
        menuBar = new JMenuBar();

        // File Menu
        JMenu menu = new JMenu("File");
        JMenuItem item = new JMenuItem("New");
        item.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

            }

        });
        menu.add(item);

        buildComponents();
}
build components
private void buildComponents(){
        //Build the Main Window

        int width=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int height=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        this.setSize(width,height);

        //this.setMenuBar(menuBar);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container mainContainer = this.getContentPane();
        mainContainer.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        this.setJMenuBar(menuBar);

        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        mainContainer.add(tabPanel,c);

        c.anchor = GridBagConstraints.PAGE_END;
        c.weightx = 0;
        c.weighty = 0;
        c.fill = GridBagConstraints.NONE;
        c.gridx = 1;
        c.gridy = 2;
        mainContainer.add(back,c);

        c.gridx = 2;
        mainContainer.add(next,c);

        this.setVisible(true);

    } 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 24 2008
Added on Aug 27 2008
3 comments
395 views