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

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

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




Menubar and Statusbar in GridLayout

 

Menubar and Statusbar in GridLayout

saychanh

5 Jun, 2009 - 10:44 AM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2009
Posts: 6

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?

User is offlineProfile CardPM
+Quote Post


nick2price

RE: Menubar And Statusbar In GridLayout

5 Jun, 2009 - 12:50 PM
Post #2

D.I.C Lover
*****

Joined: 23 Nov, 2007
Posts: 1,296



Thanked: 119 times
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

saychanh

RE: Menubar And Statusbar In GridLayout

5 Jun, 2009 - 02:24 PM
Post #3

New D.I.C Head
*

Joined: 5 Mar, 2009
Posts: 6

Where is the VerticalMenuBar method at?

Here is my current code with just the buttons:
CODE

    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 sleep.gif haha I'm new to GUI.

Thanks for the help

This post has been edited by saychanh: 5 Jun, 2009 - 02:24 PM
User is offlineProfile CardPM
+Quote Post

nick2price

RE: Menubar And Statusbar In GridLayout

5 Jun, 2009 - 02:58 PM
Post #4

D.I.C Lover
*****

Joined: 23 Nov, 2007
Posts: 1,296



Thanked: 119 times
My Contributions
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

CODE
  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
CODE
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
CODE
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
User is offlineProfile CardPM
+Quote Post

saychanh

RE: Menubar And Statusbar In GridLayout

5 Jun, 2009 - 04:20 PM
Post #5

New D.I.C Head
*

Joined: 5 Mar, 2009
Posts: 6

Alright, I got it. Thank you so much!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 05:18PM

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