GridBagLayout Pane : GridBagLayout « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » GridBagLayoutScreenshots 
GridBagLayout Pane
GridBagLayout Pane

/*
 * Copyright (c) 2000 David Flanagan. All rights reserved. This code is from the
 * book Java Examples in a Nutshell, 2nd Edition. It is provided AS-IS, WITHOUT
 * ANY WARRANTY either expressed or implied. You may study, use, and modify it
 * for any non-commercial purpose. You may distribute it non-commercially as
 * long as you retain this notice. For a commercial use license, or to purchase
 * the book (recommended), visit http://www.davidflanagan.com/javaexamples2.
 */

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridBagLayoutPane extends JPanel {
  public GridBagLayoutPane() {
    // Create and specify a layout manager
    this.setLayout(new GridBagLayout());

    // Create a constraints object, and specify some default values
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH; // components grow in both dimensions
    c.insets = new Insets(5555)// 5-pixel margins on all sides

    // Create and add a bunch of buttons, specifying different grid
    // position, and size for each.
    // Give the first button a resize weight of 1.0 and all others
    // a weight of 0.0. The first button will get all extra space.
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 4;
    c.gridheight = 4;
    c.weightx = c.weighty = 1.0;
    this.add(new JButton("Button #1"), c);

    c.gridx = 4;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = c.weighty = 0.0;
    this.add(new JButton("Button #2"), c);

    c.gridx = 4;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #3"), c);

    c.gridx = 4;
    c.gridy = 2;
    c.gridwidth = 1;
    c.gridheight = 2;
    this.add(new JButton("Button #4"), c);

    c.gridx = 0;
    c.gridy = 4;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #5"), c);

    c.gridx = 2;
    c.gridy = 4;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #6"), c);

    c.gridx = 3;
    c.gridy = 4;
    c.gridwidth = 2;
    c.gridheight = 1;
    this.add(new JButton("Button #7"), c);

    c.gridx = 1;
    c.gridy = 5;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #8"), c);

    c.gridx = 3;
    c.gridy = 5;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #9"), c);
  }
  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    f.setContentPane(new GridBagLayoutPane());
    f.pack();
    f.setVisible(true);
  }
}

           
       
Related examples in the same category
1. GridBagLayout with weightx and weighty constraintsGridBagLayout with weightx and weighty constraints
2. GridBagLayout with constraintsGridBagLayout with constraints
3. Simplest gridbag layoutSimplest gridbag layout
4. GridBagLayout with weight constraintGridBagLayout with weight constraint
5. GridBagLayout with anchor constraintsGridBagLayout with anchor constraints
6. GridBagLayout with gridwidth and gridheight constraintsGridBagLayout with gridwidth and gridheight constraints
w_ww_.j___a___v_a__2s_.c_o___m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.