Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,813 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,696 people online right now. Registration is fast and FREE... Join Now!




JMenuBar Issues

 

JMenuBar Issues

leitbug06

16 Feb, 2009 - 02:26 PM
Post #1

New D.I.C Head
*

Joined: 5 Jan, 2009
Posts: 16


My Contributions
I am creating a product database for a flooring company. I am working on the first screen right now, it will have a menu bar, a panel with a text area, and a panel with two buttons. When I run the code, everything is good, except the menu bar does not show and I cannot figure out why. Any help would be greatly appreciated.

CODE
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ProductDatabase extends JFrame
{    
    private JFrame frame;
    private JPanel textPanel;
    private JPanel buttonPanel;
    private JButton searchButton;
    private JButton editButton;
    private JTextArea textArea;
    private JMenuBar menuBar;
    private JMenu file;
    private JMenu help;
    private JMenuItem quit;
    private JMenuItem about;
  
  public ProductDatabase()
  {
          frame = new JFrame("Product Database");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
          
          textPanel = new JPanel();
          textArea = new JTextArea("Floor Coverings International \n Virginia Beach");
          textPanel.add(textArea);
          frame.add(textPanel);
          
          buttonPanel = new JPanel();
          searchButton = new JButton("Search");
          
          searchButton.setVerticalTextPosition(AbstractButton.CENTER);
      searchButton.setHorizontalTextPosition(AbstractButton.LEADING);
          editButton = new JButton("Edit");
          editButton.setVerticalTextPosition(AbstractButton.CENTER);
      editButton.setHorizontalTextPosition(AbstractButton.TRAILING);
          buttonPanel.add(searchButton);
          buttonPanel.add(editButton);
          frame.add(buttonPanel);
          
          menuBar = new JMenuBar();
          file = new JMenu("File");
          menuBar.add(file);
          quit = new JMenuItem("Quit");
          file.add(quit);
          help = new JMenu("Help");
          menuBar.add(help);
          about = new JMenuItem("About");
          help.add(about);
          frame.add(menuBar);

          frame.setSize(400,400);
          frame.setVisible(true);
  }

  private static void createAndShowGUI()
  {
          JFrame.setDefaultLookAndFeelDecorated(true);
          
          ProductDatabase frame = new ProductDatabase();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
          frame.setVisible(true);
  }
      
  public static void main(String[] args)
  {
      javax.swing.SwingUtilities.invokeLater(new Runnable()
          {
              public void run()
              {
                  createAndShowGUI();
              }
          });
  }    
}


User is offlineProfile CardPM
+Quote Post


markhazlett9

RE: JMenuBar Issues

16 Feb, 2009 - 02:58 PM
Post #2

Coding is a lifestyle
Group Icon

Joined: 12 Jul, 2008
Posts: 1,463



Thanked: 48 times
Dream Kudos: 25
My Contributions
Your layouts for your main frame and the panels are set to null... I would try a border layout and put the menu bar in the NORTH position. So your main panel should look something like this when you declare it...

JPanel panel = new JPanel(new BorderLayout());

This allows the layout to be set in the main frame. That way when you add components to the panel then you will be adding them to a specific location and nothing will overlap... Like this...

panel.add(menuBar, BorderLayout.NORTH);

Hope this helps. Cheers
User is offlineProfile CardPM
+Quote Post

pbl

RE: JMenuBar Issues

16 Feb, 2009 - 03:19 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,962



Thanked: 1187 times
Dream Kudos: 450
My Contributions
Your ProductDatabase is already a JFrame no need to add another one
You are not correctly using the BoxLayout this is not the way it should be used ... use another one it you be easier
JMenuBar can be added anywhere but if you want it in the op you have to use setJMenuBar
Your CreaeANdSHwGui is misleading... do not use a static method to create an instance of a GUI

CODE

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ProductDatabase extends JFrame
{    
    private JPanel textPanel;
    private JPanel buttonPanel;
    private JButton searchButton;
    private JButton editButton;
    private JTextArea textArea;
    private JMenuBar menuBar;
    private JMenu file;
    private JMenu help;
    private JMenuItem quit;
    private JMenuItem about;

    public ProductDatabase()
    {
        super("Product Database");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        textPanel = new JPanel();
        textArea = new JTextArea("Floor Coverings International \n Virginia Beach");
        textPanel.add(textArea);
        add(textPanel);

        buttonPanel = new JPanel();
        searchButton = new JButton("Search");

        searchButton.setVerticalTextPosition(AbstractButton.CENTER);
        searchButton.setHorizontalTextPosition(AbstractButton.LEADING);
        editButton = new JButton("Edit");
        editButton.setVerticalTextPosition(AbstractButton.CENTER);
        editButton.setHorizontalTextPosition(AbstractButton.TRAILING);
        buttonPanel.add(searchButton);
        buttonPanel.add(editButton);
        add(buttonPanel);

        menuBar = new JMenuBar();
        file = new JMenu("File");
        menuBar.add(file);
        quit = new JMenuItem("Quit");
        file.add(quit);
        help = new JMenu("Help");
        menuBar.add(help);
        about = new JMenuItem("About");
        help.add(about);
        setJMenuBar(menuBar);

        setSize(400,400);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                ProductDatabase pd = new ProductDatabase();
            }
        });
    }    

}


Good luck
User is online!Profile CardPM
+Quote Post

leitbug06

RE: JMenuBar Issues

19 Feb, 2009 - 03:25 PM
Post #4

New D.I.C Head
*

Joined: 5 Jan, 2009
Posts: 16


My Contributions
I got it to work. Thank you both for the help and advice!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 09:56PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month