examples
Class Example13
java.lang.Object
examples.Example13
public class Example13
- extends java.lang.Object
This is another example of using GridRows
to create a grid. It uses
setRowMargins
to set
left and right margins.
The example has been taken from a tutorial for
FormLayout.
// A utility function to create a textfield with
// specified number of columns and a bevel border.
public static JTextField createTextField(int n)
{
JTextField ed=new JTextField(n);
ed.setBorder(BorderFactory.createLoweredBevelBorder());
return ed;
}
// A utility function to create a row of a
// separator of fixed height with a leading label.
public static Row createNamedSeparator(String text)
{
Row row=new Row(Cell.JUSTIFIED,Cell.CENTER);
row.add(new JLabel(text));
JSeparator sep=new JSeparator();
row.add(sep);
Dimension d=sep.getPreferredSize();
sep.setMaximumSize(new Dimension(Cell.MAX,d.height));
return row;
}
// A utility function to add a label
// and an editor to a GridRow object.
public static void addLabelAndEditor(
GridRow row, String text, int n)
{
row.add(Cell.RIGHT,Cell.CENTER,new JLabel(text));
row.add(Cell.NO_ALIGNMENT,Cell.CENTER,createTextField(n));
}
public static void createGUI()
{
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container container=frame.getContentPane();
// GridRows object
GridRows rows=new GridRows();
// Row 1 The top separator.
// The Row (cell) containing the label
// and the separator spans all the four columns of the grid.
rows.newRow().add(createNamedSeparator("General")).span(4);
// Row 2 The 'Company' label and editor.
GridRow row=rows.newRow();
addLabelAndEditor(row,"Company",20);
// The editor spans three columns of the grid.
row.span(3);
// Row 3 The 'Contact' label and editor
row=rows.newRow();
addLabelAndEditor(row,"Contact",20);
// The editor spans three columns of the grid.
row.span(3);
// Row 4 An empty row for vertical spacing.
// It is enough to add a vertical gap in
// the first column. The trailing blanks in
// the row are automaticall created.
rows.newRow().add(new Gap(10));
// Row 5 The 'Propeller' separator.
// The Row (cell) containing the label
// and the separator spans all the four columns of the grid.
rows.newRow().add(createNamedSeparator("Propeller")).span(4);
// Row 6 The 'PTI' and 'Power' labels and editors in the same row.
row=rows.newRow();
addLabelAndEditor(row,"PTI[kW]",10);
addLabelAndEditor(row," Power[kW]",10);
// Row 7 The 'R' and 'D' labels and editors in the same row.
row=rows.newRow();
addLabelAndEditor(row,"R[mm]",10);
addLabelAndEditor(row," D[mm]",10);
CellGrid cellgrid=rows.createCellGrid();
// Left row margins for the rows with the editors.
// Sets Left Margin Size = 20 for rows 1,2,5, and 6.
cellgrid.setRowMargins(Cell.LEFT,20,1, 2, 5, 6);
// Right row margins for the rows with the editors.
// Sets Right Margin Size = 20 for rows 1,2,5, and 6.
cellgrid.setRowMargins(Cell.RIGHT,20,1, 2, 5, 6);
cellgrid.createLayout(container);
frame.pack();
frame.setSize(frame.getPreferredSize());
frame.show();
}
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Example13
public Example13()
createTextField
public static javax.swing.JTextField createTextField(int n)
createNamedSeparator
public static Row createNamedSeparator(java.lang.String text)
addLabelAndEditor
public static void addLabelAndEditor(GridRow row,
java.lang.String text,
int n)
createGUI
public static void createGUI()
main
public static void main(java.lang.String[] args)
run
public static void run()