Results 1 to 2 of 2

Thread: GridBagLayout

  1. #1
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default GridBagLayout

    How do I code a gridBagLayout to arrange some components. Say for example a button and a text area?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class GridBagDemo {
        private JPanel getContent() {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5,5,5,5);
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            panel.add(getPanel1(), gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(getPanel2(), gbc);
            gbc.gridwidth = 2;
            panel.add(getPanel3(), gbc);
            return panel;
        }
    
        private JPanel getPanel1() {
            JButton button = new JButton("button");
            JTextArea textArea = new JTextArea(4, 20);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets.bottom = 10;
            panel.add(button, gbc);
            gbc.insets.bottom = 0;
            panel.add(textArea, gbc);
            return panel;
        }
    
        private JPanel getPanel2() {
            JButton button = new JButton("button");
            JTextArea textArea = new JTextArea(4, 20);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            panel.add(textArea, gbc);
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.insets.left = 10;
            panel.add(button, gbc);
            return panel;
        }
    
        private JPanel getPanel3() {
            JButton button = new JButton("button");
            JTextArea textArea = new JTextArea(4, 20);
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(textArea, gbc);
            gbc.insets = new Insets(5,5,5,5);
            gbc.fill = GridBagConstraints.NONE;
            panel.add(button, gbc);
            return panel;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new GridBagDemo().getContent());
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 08:35 AM
  2. GridBagLayout
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 12:16 AM
  3. GridBagLayout...please help
    By newtojava7 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 01:16 AM
  4. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 08:03 PM
  5. Problem with GridBagLayout
    By Daniel in forum SWT / JFace
    Replies: 2
    Last Post: 07-01-2007, 06:57 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
  •