In addition to specifying an explicit number of columns to span, you can
use the REMAINDER constant defined in
GridBagConstraints. This indicates that the component's
display area should begin with the column specified by the
gridx value and that it should fill all the remaining columns
to the right of that column. An example is shown overleaf:
This display was produced by the following code:
import java.awt.*;
import javax.swing.*;
public class Remainder {
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();
pane.add(new JButton("First row, first column"), gbc);
pane.add(new JButton("First row, second column"), gbc);
pane.add(new JButton("First row, third column"), gbc);
gbc.gridx = 0;
pane.add(new JButton("Second row"), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
pane.add(new JButton("Third row, gridwidth set to REMAINDER"), gbc);
f.setSize(600, 300);
f.setVisible(true);
}
}
You can also set a gridwidth value to RELATIVE,
which is similar to REMAINDER. However, RELATIVE
causes the component to span all remaining columns except the
last one in the grid. For example, you might make the modifications shown
below to the Remainder class defined earlier:
pane.add(new JButton("Second row"), gbc);
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbc.fill = GridBagConstraints.HORIZONTAL;
pane.add(new JButton("Third row, gridwidth set to RELATIVE"), gbc);
If you compile and execute the code, it will produce a display like this one:
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.