School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,029 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,096 people online right now. Registration is fast and FREE... Join Now!



JMenuBar Issues

JMenuBar Issues Rate Topic: -----

#1 leitbug06  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 16
  • Joined: 05-January 09


Dream Kudos: 0

Posted 16 February 2009 - 02:26 PM

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.

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

Was This Post Helpful? 0
  • +
  • -


#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • Icon
  • View blog
  • Group: Greeters
  • Posts: 1,659
  • Joined: 12-July 08


Dream Kudos: 25

Posted 16 February 2009 - 02:58 PM

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
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 16 February 2009 - 03:19 PM

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

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
Was This Post Helpful? 0
  • +
  • -

#4 leitbug06  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 16
  • Joined: 05-January 09


Dream Kudos: 0

Posted 19 February 2009 - 03:25 PM

I got it to work. Thank you both for the help and advice!
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month