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

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



Menubar and Statusbar in GridLayout

Menubar and Statusbar in GridLayout Rate Topic: -----

#1 saychanh  Icon User is offline

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


Dream Kudos: 0

Posted 05 June 2009 - 10:44 AM

Hello,

I am making a GUI with 8 x 8 grid of buttons using the GridLayout, but I also want to add a menu bar and a status bar. Is there anyway to add these without having to set the layout to null and physically setting the size/location of all of the buttons?
Was This Post Helpful? 0
  • +
  • -


#2 nick2price  Icon User is online

  • D.I.C Lover
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,471
  • Joined: 23-November 07


Dream Kudos: 0

Posted 05 June 2009 - 12:50 PM

Yes. All you have to do is override the VerticalMenuBar method. Be simular for status bar. Are you chatting about not setting the layout to null for the components that are placed on these bars?
Was This Post Helpful? 0
  • +
  • -

#3 saychanh  Icon User is offline

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


Dream Kudos: 0

Posted 05 June 2009 - 02:24 PM

Where is the VerticalMenuBar method at?

Here is my current code with just the buttons:
	public class PlayerField extends JFrame
   {
	  private boolean clicked;
	  private int inputCoords[] = new int[2]; //[0] = x coord, [1] = y coord
	  private Button[][] buttons = new Button[8][8];

	   public PlayerField()
	  {
		 setTitle("Player Field");
		 setSize(300, 300);
		 setDefaultCloseOperation(EXIT_ON_CLOSE);
		 setLocationRelativeTo(null);
		  
		 JPanel panel = new JPanel();
		 panel.setBackground(Color.BLACK);
		 panel.setLayout(new GridLayout(8, 8, 2, 2));	  
		  
		 for (int x = 0; x < 8; x++)
		 {
			for (int y = 0; y < 8; y++)
			{
			   buttons[x][y] = new Button();
			   buttons[x][y].setActionCommand((x+1) + " " + (y+1));
			   buttons[x][y].setBackground(Color.CYAN);
			   panel.add(buttons[x][y]);
			}
		 }   			  
		 setContentPane(panel);		  
	  }


I would like to add a menu bar above the buttons and a status bar below it. I'm not talking about the components on these bars..or at least i don't think I am -_- haha I'm new to GUI.

Thanks for the help

This post has been edited by saychanh: 05 June 2009 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is online

  • D.I.C Lover
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,471
  • Joined: 23-November 07


Dream Kudos: 0

Posted 05 June 2009 - 02:58 PM

I would take that code out of the constructor and put it in its own method. I would then create a method for the Menu Bar and a method for the Status Bar. I would place each onto its on JPanel using whatever layout you find appropiate. I would then add each panel to the content pane using whatever layout you need to get them how you want, maybe BorderLayout. You dont need to worry about a layout being null. If you want the layout of your MenuBar to be a GridLayout, do somthing like

  private void menuBarPanel() {
	JPanel menuPanel = new JPanel();
	menuPanel .setLayout(new FlowLayout());
	
	JMenuBar menuBar = new JMenuBar();
	JMenu menu = new JMenu("My Menu");
	menu.setMnemonic(KeyEvent.VK_A);
	menu.getAccessibleContext().setAccessibleDescription("My Items");
	menuBar.add(menu);
	//add your items to the menu

	menuPanel.add(menuBar, 0);
  }
}


Do this for the other Panels too. Then you can create a mainPanel, like your content pane, and add your three panels to it
private void mainPanel(){
		JComponent mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout(5, 5));
		mainPanel.add(menuPanel, BorderLayout.NORTH);
		//then other panels
		add(mainPanel);
}


Initialise all your Panels in the constructor, so that they are created
public PlayerField()
	  {
		 setTitle("Player Field");
		 setSize(300, 300);
		 setDefaultCloseOperation(EXIT_ON_CLOSE);
		 menuBarPanel();
		 //other panels
		 mainPanel();
		}

Then you should be good to go. Need any advise just ask
Was This Post Helpful? 1
  • +
  • -

#5 saychanh  Icon User is offline

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


Dream Kudos: 0

Posted 05 June 2009 - 04:20 PM

Alright, I got it. Thank you so much!
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