Results 1 to 3 of 3
  1. #1
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default Problem with GridBagLayout

    Hi, Im trying to make a GUI for a docking control program. It comprises of 4 text boxes at the top, then 8 buttons under these.
    The text boxes have to be done using a BoxLayout, and the buttons using a GridBagLayout. They must then both be put in a JPanel.
    I have got the text boxes sorted, and have been doing a lot of reading on how to make GridBagLayouts. I think the code I have should have the layout made. I belive the problem is that the GridBagLayout is hiding somwhere behind the panel I wish to put it on.
    I have tried a few ways of putting the GridBagLayout on the panel, but it isn't having any of it!
    Java Code:
     -----------------------------------------------------
     | Text Box 1                                        |
     |---------------------------------------------------|
     | Text Box 2                                        |
     |---------------------------------------------------|
     | Text Box 3                                        |
     |---------------------------------------------------|
     | Text Box 4                                        |
     |---------------------------------------------------|
     | Button             | Button  | Button  | Button   |
     |---------------------------------------------------|
     | Button             | Button  | Button             |
     |------------------------------|                    |
     | Button                       |                    |
     |---------------------------------------------------|
    Java Code:
    import javax.swing.*;
     import java.awt.*;
     import java.awt.event.*;
     import java.awt.color.*;
     import java.applet.Applet;
     
     public class GUI extends Applet
     {
         JFrame frame;
         
         // TEXT FIELDS
         JTextField seaShipsID;
         JTextField queueShipsID;
         JTextField dockShipsID;
         JTextField shipMessages;
         
         // BUTTONS
         JButton dockFromSea;
         JButton dockFromQueue;
         JButton setSail;
         JButton queue;
         JButton admit;
         JButton dequeue;
         JButton refuse;
         JButton bye;
         
         // PANELS
         JPanel textFieldPanel; 
         JPanel mainPanel;
         
         // GRID BAG LAYOUT
         GridBagLayout buttonLayout;
         GridBagConstraints buttonConstraints;
         
         public static void main(String[] args)
         {
             GUI gui = new GUI();
             gui.go();
         }
           
         public void go()
         {
             frame = new JFrame();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
             
             buttonLayout = new GridBagLayout();
             buttonConstraints = new GridBagConstraints();
             
             textFieldPanel = new JPanel();
             textFieldPanel.setBackground(Color.lightGray);
             textFieldPanel.setLayout(new BoxLayout(textFieldPanel, BoxLayout.Y_AXIS));       
             
             mainPanel = new JPanel();
             mainPanel.setBackground(Color.lightGray);
             mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));       
             
             // BUTTONS                      
             dockFromSea = new JButton("Dock From Sea");
             dockFromQueue = new JButton("Dock From Queue");
             setSail = new JButton("Set Sail");
             queue = new JButton("Queue");
             admit = new JButton("Admit");
             dequeue = new JButton("Dequeue");
             refuse = new JButton("Refuse");
             bye = new JButton("Bye");
             
             // BUTTON LAYOUT
             buttonConstraints.fill = GridBagConstraints.BOTH;
             addComponent(dockFromSea, 0,0,2,1);
             
             buttonConstraints.fill = GridBagConstraints.BOTH;
             addComponent(queue, 0,2,1,1);
             
             buttonConstraints.fill = GridBagConstraints.BOTH;
             addComponent(admit, 0,3,1,1);
            
             buttonConstraints.fill = GridBagConstraints.HORIZONTAL;
             addComponent(admit, 0,4,1,1);
             
             buttonConstraints.fill = GridBagConstraints.BOTH;
             addComponent(dockFromQueue, 1,0,2,1);
             
             buttonConstraints.fill = GridBagConstraints.BOTH;
             addComponent(dequeue, 1,2,1,1);
             
             buttonConstraints.fill = GridBagConstraints.BOTH;
             addComponent(refuse, 1,3,2,2);
             
             buttonConstraints.fill = GridBagConstraints.BOTH;
             addComponent(setSail, 2,1,3,1);
                
             
             // TEXT FIELDS
             seaShipsID = new JTextField("ID of boats at sea");
             queueShipsID = new JTextField("ID of boats in queue to dock");
             dockShipsID = new JTextField("ID of the boats in dock");
             shipMessages = new JTextField("No Message");
             
             // TEXT FIELD PANEL IMPLEMENTATION
             frame.getContentPane().add(BorderLayout.NORTH, textFieldPanel);
             textFieldPanel.add(seaShipsID);
             textFieldPanel.add(queueShipsID);
             textFieldPanel.add(dockShipsID);
             textFieldPanel.add(shipMessages);
                          
             // MAIN PANEL IMPLEMENTATION
             frame.getContentPane().add(BorderLayout.NORTH, mainPanel);
             mainPanel.add(textFieldPanel);
            
             frame.setSize(300,200);
             frame.setVisible(true);
         }
                     
             private void addComponent(Component component, int row, int column, int width, int height)
             {
                 buttonConstraints.gridx = column; 
                 buttonConstraints.gridy = row;
                 buttonConstraints.gridwidth = width;
                 buttonConstraints.gridheight = height;
                 buttonLayout.setConstraints(component, buttonConstraints);
                 add(component);
             }
         
     }
    Thanks.

    Daniel:o

  2. #2
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default

    I'm not sure if that is the problem but by implementing ActionListener you'll need to have an implementation for that method.

    Greetings.

    Albert:rolleyes:

  3. #3
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    I would recommend adding an ActionListener to each individual button, thereby deleting the need for the memory check and cleaning up your code.

    Greetings.

    Felissa:p

Similar Threads

  1. GridBagLayout
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:16 AM
  2. GridBagLayout...please help
    By newtojava7 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 02:16 AM
  3. Replies: 1
    Last Post: 02-17-2008, 12:43 AM
  4. Need a idea on GridBagLayout
    By ddsuresh in forum AWT / Swing
    Replies: 1
    Last Post: 01-29-2008, 02:32 AM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 09:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •