gridwidth
This constraint identifies the number of columns that the component spans,
and its default value is 1. For example, in the screenshot below, the
button in the third row spans both columns:
The code to create this display is as follows:
import java.awt.*;
import javax.swing.*;
public class ColumnSpan {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = f.getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = GridBagConstraints.RELATIVE;
pane.add(new JButton("First row, first column"), gbc);
pane.add(new JButton("Second row"), gbc);
gbc.gridwidth = 2;
pane.add(new JButton("Third row, spans two columns"), gbc);
gbc.gridwidth = 1;
gbc.gridx = GridBagConstraints.RELATIVE;
pane.add(new JButton("First row, second column"), gbc);
f.setSize(400, 300);
f.setVisible(true);
}
}
In this case, the button's size is set to its preferred width, and the
button is centered horizontally within its display area. However, it can be
made to fill both columns by setting the fill value:
import java.awt.*;
import javax.swing.*;
public class ColumnSpan {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = f.getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = GridBagConstraints.RELATIVE;
pane.add(new JButton("First row, first column"), gbc);
pane.add(new JButton("Second row"), gbc);
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
pane.add(new JButton("Third row, spans two columns"), gbc);
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = GridBagConstraints.RELATIVE;
pane.add(new JButton("First row, second column"), gbc);
f.setSize(400, 300);
f.setVisible(true);
}
}
With these alterations, the second being to reset the fill
value, the display now becomes:
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|