Example usage for java.awt GridBagConstraints GridBagConstraints

List of usage examples for java.awt GridBagConstraints GridBagConstraints

Introduction

In this page you can find the example usage for java.awt GridBagConstraints GridBagConstraints.

Prototype

public GridBagConstraints() 

Source Link

Document

Creates a GridBagConstraint object with all of its fields set to their default value.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();

    frame.setLayout(gbl);//from   w w w  .java  2s .  c om
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));

    gbl.layoutContainer(frame);

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.fill = GridBagConstraints.HORIZONTAL;

    gbl.setConstraints(component, gbc);
    frame.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();

    frame.setLayout(gbl);//from  www.  ja v  a  2  s . co m
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));

    gbl.layoutContainer(frame);

    GridBagConstraints gbc = new GridBagConstraints();

    // Make the component on stretchable
    gbc.fill = GridBagConstraints.NONE;

    gbl.setConstraints(component, gbc);
    frame.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new GridBagLayout());
    for (int i = 0; i < 25; i++) {
        JTextField field = new JTextField("Field " + i, 20);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridy = i;//from w  ww . j a  v  a 2  s.co  m
        panel.add(field, constraints);
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    JButton removeButton = new JButton("Remove Field");
    removeButton.addActionListener(e -> {
        if (panel.getComponentCount() >= 1) {
            panel.remove(panel.getComponentCount() - 1);
            scrollPane.revalidate();
            scrollPane.repaint();
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);
    f.setLocation(200, 200);
    f.getContentPane().add(scrollPane);
    f.getContentPane().add(removeButton, BorderLayout.SOUTH);
    f.setVisible(true);
}

From source file:TryGridBagLayout.java

public static void main(String[] args) {
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    aWindow.getContentPane().setLayout(gridbag);
    constraints.weightx = constraints.weighty = 10.0;
    constraints.fill = constraints.BOTH;
    addButton(" Press ", constraints, gridbag);
    constraints.gridwidth = constraints.REMAINDER;
    addButton("GO", constraints, gridbag);
    aWindow.setVisible(true);//www  .  ja  v  a  2  s .co m
}

From source file:GridBagWithWeight.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of fill constraints");
    JPanel p = new JPanel(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;/*from   w  w  w. ja  v  a  2 s. c om*/
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH; // Use both horizontal & vertical
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.fill = GridBagConstraints.HORIZONTAL; // Horizontal only
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE; // Remember to reset to none
    p.add(new JButton("and"), c);
    c.gridx = 2;
    c.fill = GridBagConstraints.VERTICAL; // Vertical only
    p.add(new JButton("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:GridBagWithGridWidthHeight.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of gridwidth, gridheight constraints");
    JPanel p = new JPanel(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;//from  w ww. j  ava2s .c o  m
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2; // span across 2 rows
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1; // set back to 1 row
    c.gridwidth = 2; // span across 2 columns
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1; // set back to 1 column
    p.add(new JButton("and"), c);
    c.gridx = 2;
    p.add(new JButton("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:GridBagWithWeight.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of weightx, weighty constraints");
    JPanel p = new JPanel();

    p.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    p.setLayout(new GridBagLayout());
    c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;//from   w  w  w. jav a 2  s.c om
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    p.add(new JButton("Source"), c);
    c.gridx = 0;
    c.gridy = 1;
    p.add(new JButton("and"), c);
    c.gridx = 1;
    p.add(new JButton("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:GridBagWithAnchor.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of anchor constraints");
    JPanel p = new JPanel(new GridBagLayout());

    p.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;/*from w  w w .j  a v  a  2  s . c  om*/
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2;
    c.anchor = GridBagConstraints.NORTH; // place component on the North
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.anchor = GridBagConstraints.SOUTHWEST;
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.CENTER; // remember to rest to center
    p.add(new JButton("and"), c);
    c.gridx = 2;
    p.add(new JButton("Support !!!"), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:GridBagWithContaints.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of gridx, gridy,ipadx, ipady and insets constraints");
    JPanel p = new JPanel();

    p.setLayout(new GridBagLayout());
    // creates a constraints object
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2); // insets for all components
    c.gridx = 0; // column 0
    c.gridy = 0; // row 0
    c.ipadx = 5; // increases components width by 10 pixels
    c.ipady = 5; // increases components height by 10 pixels
    p.add(new JButton("Java"), c); // constraints passed in
    c.gridx = 1; // column 1
    // c.gridy = 0; // comment out this for reusing the obj
    c.ipadx = 0; // resets the pad to 0
    c.ipady = 0;/*from  www .  j  a v a2 s .c o m*/
    p.add(new JButton("Source"), c);
    c.gridx = 0; // column 0
    c.gridy = 1; // row 1
    p.add(new JButton("and"), c);
    c.gridx = 1; // column 1
    p.add(new JButton("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:Main.java

public static void main(String... args) {
    JPanel contentPane;/* ww  w  .j a va2 s.  c om*/
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    contentPane = new JPanel();
    contentPane.setLayout(new GridBagLayout());

    JPanel centerPanel = new JPanel();
    centerPanel.setOpaque(true);
    centerPanel.setBackground(Color.CYAN);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    gbc.weightx = 1.0;
    gbc.weighty = 0.9;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.fill = GridBagConstraints.BOTH;

    contentPane.add(centerPanel, gbc);

    JButton leftButton = new JButton("Left");
    JButton rightButton = new JButton("Right");
    gbc.gridwidth = 1;
    gbc.gridy = 1;
    gbc.weightx = 0.3;
    gbc.weighty = 0.1;

    contentPane.add(leftButton, gbc);

    gbc.gridx = 1;
    gbc.weightx = 0.7;
    gbc.weighty = 0.1;

    contentPane.add(rightButton, gbc);

    frame.setContentPane(contentPane);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);

}