Java/Swing Components/FormLayout
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 Build a panel with a leading indent column using the DefaultFormBuilder
- 2 Build button stacks using the ButtonStackBuilder
- 3 Build panels component orientation: left-to-right vs. right-to-left
- 4 Columns and rows are specified before the panel is filled with components 1
- 5 Compares approaches how to append a custom area at the end of a panel built with the DefaultFormBuilder
- 6 Create and configure a layout, create a builder, add components
- 7 Demonstrates a frequent pitfall when specifying a growing row
- 8 Demonstrates a pure use of the FormLayout
- 9 Demonstrates how a JTextArea"s preferred size grows with the container if no columns and rows are set
- 10 Demonstrates how to build button bars using a ButtonBarBuilder
- 11 Demonstrates how to build button bars with a fixed button order
- 12 Demonstrates how to find bugs in the layout using
- 13 Demonstrates sizes: constant, minimum, preferred
- 14 Demonstrates the basic FormLayout sizes: constant, minimum, preferred
- 15 Demonstrates the different FormLayout alignments
- 16 FormLayout: Basic Example 1
- 17 FormLayout: Bounds Example 6
- 18 FormLayout: Button Bar Builder Example
- 19 FormLayout: Button Bar Builder Example 2
- 20 FormLayout: Button Bar Builder Example 3
- 21 FormLayout: Button Stack Builder Example 1
- 22 FormLayout: Default Alignment Example 2
- 23 FormLayout: Default Form Builder Example 1
- 24 FormLayout: Explicit Alignment Example 3
- 25 FormLayout: Grouping Example 10
- 26 FormLayout: Growable Example 8
- 27 FormLayout growing options: none, default, weighted
- 28 FormLayout: No Grouping Example 9
- 29 FormLayout: Size Specification Example 4
- 30 FormLayout: Spacing Example 5
- 31 FormLayout: Spanning Example 7
- 32 How columns and rows can be grouped in FormLayout
- 33 How components can span multiple columns and rows
- 34 How FormLayout applies the default column and
- 35 Panel Builder Example 1
- 36 Panel Builder Example 2
- 37 Shows three approaches how to add custom rows to a form that is built using a DefaultFormBuilder
- 38 The different sizing units provided by the FormLayout
- 39 The use of Factories as provided by the Forms framework
- 40 Three FormLayout component sizes: minimum, default and preferred
- 41 Uses the FormLayout and the DefaultFormBuilder
Build a panel with a leading indent column using the DefaultFormBuilder
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates how to efficiently build a panel with a leading
* indent column using the DefaultFormBuilder.<p>
*
* The default FocusTraversalPolicy will lead to a poor focus traversal,
* where non-editable fields are included in the focus cycle.
* Anyway, this tutorial is about layout, not focus, and so I favor
* a lean example over a fully functional.
*
* @author Karsten Lentzsch
* @version $Revision: 1.9 $
*
* @see DefaultFormBuilder
*/
public class IndentColumnExample {
private JTextField fileNumberField;
private JTextField rfqNumberField;
private JTextField blNumberField;
private JTextField mblNumberField;
private JTextField customerKeyField;
private JTextField customerAddressField;
private JTextField shipperKeyField;
private JTextField shipperAddressField;
private JTextField consigneeKeyField;
private JTextField consigneeAddressField;
private JTextField departureCodeField;
private JTextField departurePortField;
private JTextField destinationCodeField;
private JTextField destinationPortField;
private JTextField deliveryDateField;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Indent Column");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new FormDebugExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
// Component Creation and Initialization **********************************
/**
* Creates and intializes the UI components.
*/
private void initComponents() {
fileNumberField = new JTextField();
rfqNumberField = new JTextField();
blNumberField = new JTextField();
mblNumberField = new JTextField();
customerKeyField = new JTextField();
customerAddressField = new JTextField();
customerAddressField.setEditable(false);
shipperKeyField = new JTextField();
shipperAddressField = new JTextField();
shipperAddressField.setEditable(false);
consigneeKeyField = new JTextField();
consigneeAddressField = new JTextField();
consigneeAddressField.setEditable(false);
departureCodeField = new JTextField();
departurePortField = new JTextField();
departurePortField.setEditable(false);
destinationCodeField = new JTextField();
destinationPortField = new JTextField();
destinationPortField.setEditable(false);
deliveryDateField = new JTextField();
}
// Building *************************************************************
/**
* Builds the pane.
*
* @return the built panel
*/
public JComponent buildPanel() {
initComponents();
FormLayout layout = new FormLayout(
"12dlu, pref, 3dlu, max(45dlu;min), 2dlu, min, 2dlu, min, 2dlu, min, ",
"");
layout.setColumnGroups(new int[][] { { 4, 6, 8, 10 } });
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();
builder.setLeadingColumnOffset(1);
builder.appendSeparator("General");
builder.append("File Number", fileNumberField, 7);
builder.append("RFQ Number", rfqNumberField, 7);
builder.append("BL/MBL", blNumberField, mblNumberField); builder.nextLine();
builder.appendSeparator("Addresses");
builder.append("Customer", customerKeyField, customerAddressField, 5);
builder.append("Shipper", shipperKeyField, shipperAddressField, 5);
builder.append("Consignee", consigneeKeyField, consigneeAddressField, 5);
builder.appendSeparator("Transport");
builder.append("Departure", departureCodeField, departurePortField, 5);
builder.append("Destination", destinationCodeField, destinationPortField, 5);
builder.append("Delivery Date", deliveryDateField); builder.nextLine();
return builder.getPanel();
}
}
Build button stacks using the ButtonStackBuilder
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.ruponent;
import java.awt.Insets;
import javax.swing.*;
import com.jgoodies.forms.builder.ButtonStackBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates how to build button stacks using the ButtonStackBuilder.
*
* @author Karsten Lentzsch
* @version $Revision: 1.12 $
*
* @see ButtonStackBuilder
*/
public class ButtonStacksExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Button Stacks");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new ButtonStacksExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add(buildButtonStackNoBuilder(), "No Builder");
tabbedPane.add(buildButtonStackWithBuilder(), "Builder");
tabbedPane.add(buildButtonStackRelated(), "Related");
tabbedPane.add(buildButtonStackUnrelated(), "Unrelated ");
tabbedPane.add(buildButtonStackMixedDefault(), "Mix");
tabbedPane.add(buildButtonStackMixedNarrow(), "Mix Narrow");
return tabbedPane;
}
private Component buildButtonStackNoBuilder() {
JPanel buttonStack = new JPanel(
new FormLayout("p", "p, 4px, p"));
buttonStack.add(new JButton("Yes"), "1, 1");
buttonStack.add(new JButton("No"), "1, 3");
return wrap(buttonStack,
"This stack has been built without a ButtonStackBuilder.\n" +
" o The buttons have no minimum width and\n" +
" o The gaps use pixel sizes and do not scale with the font\n" +
" o The gaps may become inconsisten in a team.");
}
private Component buildButtonStackWithBuilder() {
ButtonStackBuilder builder = new ButtonStackBuilder();
builder.addGridded(new JButton("Yes"));
builder.addRelatedGap();
builder.addGridded(new JButton("No"));
return wrap(builder.getPanel(),
"This stack has been built with a ButtonStackBuilder.\n" +
" o The buttons have a minimum width and\n" +
" o The gap uses a logical size that follows a style guide.");
}
private Component buildButtonStackRelated() {
ButtonStackBuilder builder = new ButtonStackBuilder();
builder.addGridded(new JButton("Related"));
builder.addRelatedGap();
builder.addGridded(new JButton("Related"));
builder.addRelatedGap();
builder.addGridded(new JButton("Related"));
return wrap(builder.getPanel(),
"This stack uses the logical gap for related buttons.\n");
}
private Component buildButtonStackUnrelated() {
ButtonStackBuilder builder = new ButtonStackBuilder();
builder.addGridded(new JButton("Unrelated"));
builder.addUnrelatedGap();
builder.addGridded(new JButton("Unrelated"));
builder.addUnrelatedGap();
builder.addGridded(new JButton("Unrelated"));
return wrap(builder.getPanel(),
"This stack uses the logical gap for unrelated buttons.\n");
}
private Component buildButtonStackMixedDefault() {
ButtonStackBuilder builder = new ButtonStackBuilder();
builder.addGridded(new JButton("OK"));
builder.addRelatedGap();
builder.addGridded(new JButton("Cancel"));
builder.addUnrelatedGap();
builder.addGridded(new JButton("Help"));
builder.addUnrelatedGap();
builder.addGlue();
builder.addFixed(new JButton("Copy to Clipboard"));
return wrap(builder.getPanel(),
"Demonstrates a glue (between Help and Copy),\n" +
"has related and unrelated buttons and\n" +
"a button with long label with the default margin.");
}
private Component buildButtonStackMixedNarrow() {
ButtonStackBuilder builder = new ButtonStackBuilder();
builder.addGridded(new JButton("OK"));
builder.addRelatedGap();
builder.addGridded(new JButton("Cancel"));
builder.addUnrelatedGap();
builder.addGridded(new JButton("Help"));
builder.addUnrelatedGap();
builder.addGlue();
builder.addGridded(new JButton("Copy to Clipboard"));
return wrap(builder.getPanel(),
"Demonstrates a glue (between Help and Copy),\n" +
"has related and unrelated buttons and\n" +
"a button with long label with a narrow margin.\n\n"+
"Note that some look&feels do not support\n" +
"the narrow margin feature, and conversely,\n" +
"others have only narrow margins.");
}
// Helper Code ************************************************************
private static Component wrap(Component buttonStack, String text) {
JTextArea textArea = new JTextArea(text);
textArea.setMargin(new Insets(6, 10, 4, 6));
Component textPane = new JScrollPane(textArea);
FormLayout layout = new FormLayout(
"fill:100dlu:grow, 6dlu, p",
"fill:56dlu:grow");
JPanel panel = new JPanel(layout);
CellConstraints cc = new CellConstraints();
panel.setBorder(Borders.DIALOG_BORDER);
panel.add(textPane, cc.xy(1, 1));
panel.add(buttonStack, cc.xy(3, 1));
return panel;
}
}
Build panels component orientation: left-to-right vs. right-to-left
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.ruponent;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.FormSpec.DefaultAlignment;
/**
* Demonstrates how to build panels that honor or ignore the current
* component orientation: left-to-right vs. right-to-left.<p>
*
* This example uses a utility class that may be moved to the extras or
* to the Forms core in a future version. The tricky part is the abstract
* definition of column specifications and cell constraints.<p>
*
* The example below utilizes the <code>OrientationUtils</code> to flip
* column specification defaul alignments and to reverse the order of
* column specifications. Cell constraints need to be adjusted too; this
* example avoids the problem by using a builder that creates <em>all</em>
* cell constraints.<p>
*
* You can find information about the latest additions regarding the
* Forms support for different component orientations in the comments for
*
== Columns and rows are specified before the panel is filled with components ==
<!-- start source code -->
<source lang="java">
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.FormLayout;
/**
* Combines the <code>FormLayout</code> with the <code>PanelBuilder</code>.
* Columns and rows are specified before the panel is filled
* with components. The builder"s cursor is used to determine the location
* of the next component. And the builder"s convenience methods are used
* to add labels and separators.<p>
*
* This panel building style is intended for learning purposes only.
* The recommended style is demonstrated in the {@link DefaultFormBuilderExample}.
*
* @author Karsten Lentzsch
* @version $Revision: 1.8 $
*
* @see PlainExample
* @see RowCounterExample
* @see DefaultFormBuilderExample
*/
public class DynamicRowsExample {
private JTextField identifierField;
private JTextField ptiField;
private JTextField powerField;
private JTextField lenField;
private JTextField daField;
private JTextField diField;
private JTextField da2Field;
private JTextField di2Field;
private JTextField rField;
private JTextField dField;
private JComboBox locationCombo;
private JTextField kFactorField;
private JCheckBox holesCheckBox;
private JCheckBox slotsCheckBox;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Dynamic Rows");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new DynamicRowsExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
// Component Creation and Initialization **********************************
/**
* Creates and intializes the UI components.
*/
private void initComponents() {
identifierField = new JTextField();
ptiField = new JTextField();
powerField = new JTextField();
lenField = new JTextField();
daField = new JTextField();
diField = new JTextField();
da2Field = new JTextField();
di2Field = new JTextField();
rField = new JTextField();
dField = new JTextField();
locationCombo = createLocationComboBox();
kFactorField = new JTextField();
holesCheckBox = new JCheckBox("Has radial holes", true);
slotsCheckBox = new JCheckBox("Has longitudinal slots");
}
/**
* Creates and returns a combo box for the locations.
*
* @return a combo box for three locations
*/
private JComboBox createLocationComboBox() {
return new JComboBox(
new String[] {
"Propeller nut thread",
"Stern tube front area",
"Shaft taper" });
}
// Building *************************************************************
/**
* Builds the pane.
*
* @return the built panel
*/
public JComponent buildPanel() {
initComponents();
FormLayout layout = new FormLayout(
"right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
+ "right:max(40dlu;pref), 3dlu, 70dlu",
"p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, "
+ "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, "
+ "p, 3dlu, p, 3dlu, p, 3dlu, p");
PanelBuilder builder = new PanelBuilder(layout);
builder.setDefaultDialogBorder();
builder.addSeparator("Segment");
builder.nextLine(2);
builder.addLabel("Identifier"); builder.nextColumn(2);
builder.add(identifierField);
builder.nextLine(2);
builder.addLabel("PTI [kW]"); builder.nextColumn(2);
builder.add(ptiField); builder.nextColumn(2);
builder.addLabel("Power [kW]"); builder.nextColumn(2);
builder.add(powerField);
builder.nextLine(2);
builder.addLabel("len [mm]"); builder.nextColumn(2);
builder.add(lenField);
builder.nextLine(2);
builder.addSeparator("Diameters");
builder.nextLine(2);
builder.addLabel("da [mm]"); builder.nextColumn(2);
builder.add(daField); builder.nextColumn(2);
builder.addLabel("di [mm]"); builder.nextColumn(2);
builder.add(diField);
builder.nextLine(2);
builder.addLabel("da2 [mm]"); builder.nextColumn(2);
builder.add(da2Field); builder.nextColumn(2);
builder.addLabel("di2 [mm]"); builder.nextColumn(2);
builder.add(di2Field);
builder.nextLine(2);
builder.addLabel("R [mm]"); builder.nextColumn(2);
builder.add(rField); builder.nextColumn(2);
builder.addLabel("D [mm]"); builder.nextColumn(2);
builder.add(dField);
builder.nextLine(2);
builder.addSeparator("Criteria");
builder.nextLine(2);
builder.addLabel("Location"); builder.nextColumn(2);
builder.add(locationCombo); builder.nextColumn(2);
builder.addLabel("k-factor"); builder.nextColumn(2);
builder.add(kFactorField);
builder.nextLine(2);
builder.addLabel("Holes"); builder.nextColumn(2);
builder.setColumnSpan(5);
builder.add(holesCheckBox);
builder.setColumnSpan(1);
builder.nextLine(2);
builder.addLabel("Slots"); builder.nextColumn(2);
builder.setColumnSpan(5);
builder.add(slotsCheckBox);
builder.setColumnSpan(1);
return builder.getPanel();
}
}
Columns and rows are specified before the panel is filled with components 1
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates a typical use of the FormLayout.
* Columns and rows are specified before the panel is filled with
* components, and the panel is filled with a PanelBuilder.<p>
*
* Unlike the PlainExample, this implementation can delegate
* the component creation for text labels and titled separators
* to the builder.<p>
*
* This panel building style is recommended for panels with
* a medium number of rows and components. If the panel has more rows,
* you may consider using a row variable to address the current row.
*
* @author Karsten Lentzsch
* @version $Revision: 1.9 $
*
* @see PanelBuilder
* @see RowCounterExample
* @see DynamicRowsExample
* @see DefaultFormBuilderExample
*/
public class PanelBuilderExample {
private JTextField companyNameField;
private JTextField contactPersonField;
private JTextField orderNoField;
private JTextField inspectorField;
private JTextField referenceNoField;
private JComboBox approvalStatusComboBox;
private JTextField shipYardField;
private JTextField registerNoField;
private JTextField hullNumbersField;
private JComboBox projectTypeComboBox;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: PanelBuilder");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new PanelBuilderExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
// Component Creation and Initialization **********************************
/**
* Creates and intializes the UI components.
*/
private void initComponents() {
companyNameField = new JTextField();
contactPersonField = new JTextField();
orderNoField = new JTextField();
inspectorField = new JTextField();
referenceNoField = new JTextField();
approvalStatusComboBox = createApprovalStatusComboBox();
shipYardField = new JTextField();
registerNoField = new JTextField();
hullNumbersField = new JTextField();
projectTypeComboBox = createProjectTypeComboBox();
}
/**
* Creates and returns a combo box for the approval states.
*
* @return a combo box for the approval status
*/
private JComboBox createApprovalStatusComboBox() {
return new JComboBox(
new String[] { "In Progress", "Finished", "Released" });
}
/**
* Creates and returns a combo box for the project types.
*
* @return a combo box for the project type
*/
private JComboBox createProjectTypeComboBox() {
return new JComboBox(
new String[] { "New Building", "Conversion", "Repair" });
}
// Building *************************************************************
/**
* Builds the pane.
*
* @return the built panel
*/
public JComponent buildPanel() {
initComponents();
FormLayout layout = new FormLayout(
"right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
+ "right:max(40dlu;pref), 3dlu, 70dlu",
"p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
"p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
"p, 3dlu, p, 3dlu, p, 3dlu, p");
PanelBuilder builder = new PanelBuilder(layout);
builder.setDefaultDialogBorder();
// Fill the table with labels and components.
CellConstraints cc = new CellConstraints();
builder.addSeparator("Manufacturer", cc.xyw(1, 1, 7));
builder.addLabel("Company", cc.xy (1, 3));
builder.add(companyNameField, cc.xyw(3, 3, 5));
builder.addLabel("Contact", cc.xy (1, 5));
builder.add(contactPersonField, cc.xyw(3, 5, 5));
builder.addLabel("Order No", cc.xy (1, 7));
builder.add(orderNoField, cc.xy (3, 7));
builder.addSeparator("Inspector", cc.xyw(1, 9, 7));
builder.addLabel("Name", cc.xy (1, 11));
builder.add(inspectorField, cc.xyw(3, 11, 5));
builder.addLabel("Reference No", cc.xy (1, 13));
builder.add(referenceNoField, cc.xy (3, 13));
builder.addLabel("Status", cc.xy (1, 15));
builder.add(approvalStatusComboBox, cc.xy (3, 15));
builder.addSeparator("Ship", cc.xyw(1, 17, 7));
builder.addLabel("Shipyard", cc.xy (1, 19));
builder.add(shipYardField, cc.xyw(3, 19, 5));
builder.addLabel("Register No", cc.xy (1, 21));
builder.add(registerNoField, cc.xy (3, 21));
builder.addLabel("Hull No", cc.xy (5, 21));
builder.add(hullNumbersField, cc.xy (7, 21));
builder.addLabel("Project Type", cc.xy (1, 23));
builder.add(projectTypeComboBox, cc.xy (3, 23));
return builder.getPanel();
}
}
Compares approaches how to append a custom area at the end of a panel built with the DefaultFormBuilder
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
/**
* Compares approaches how to append a custom area at the end of
* a panel built with the DefaultFormBuilder:<ol>
* <li> using two custom rows to align the leading label,
* <li> using a single custom row with label on top,
* <li> using a separator.
* </ol>
* These differ in the position of the leading "Feedback" label,
* and in turn in the alignment of font baselines between label
* and the text area.
*
* @author Karsten Lentzsch
* @version $Revision: 1.8 $
*
* @see DefaultFormBuilder
* @see DefaultFormWithCustomRowsExample
*/
public class DefaultFormWithCustomAreasExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Custom Areas");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new DefaultFormWithCustomAreasExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
// Building ***************************************************************
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add(buildCustomAreaWithAlignedLabelPanel(), "Aligned label");
tabbedPane.add(buildCustomAreaWithTopLabelPanel(), "Top label");
tabbedPane.add(buildCustomAreaWithSeparatorPanel(), "Separator");
return tabbedPane;
}
private DefaultFormBuilder buildPanelHeader() {
FormLayout layout = new FormLayout(
"right:pref, 3dlu, min:grow",
"");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();
builder.setRowGroupingEnabled(true);
builder.appendSeparator("Customer Data");
builder.append("Last Name", new JTextField());
builder.append("First Name", new JTextField());
builder.append("Street", new JTextField());
builder.append("Email", new JTextField());
return builder;
}
/**
* Demonstrates how to append a larger custom area at the end of
* a panel that is build with a {@link DefaultFormBuilder}.<p>
*
* We add a gap and a single custom row that grows and that
* is filled vertically (where the default is center vertically).
* The area uses a standard leading label.
*
* @return the custom area panel with aligned labels
*/
private JComponent buildCustomAreaWithAlignedLabelPanel() {
DefaultFormBuilder builder = buildPanelHeader();
CellConstraints cc = new CellConstraints();
builder.append("Feedback");
builder.appendRow(new RowSpec("0:grow"));
builder.add(new JScrollPane(new JTextArea("Feedback - font baselines shall be aligned")),
cc.xywh(builder.getColumn(), builder.getRow(), 1, 2, "fill, fill"));
return builder.getPanel();
}
/**
* Demonstrates how to append two custom areas at the end of
* a panel that is build with a DefaultFormBuilder.
*
* @return the custom area panel with label in the top
*/
private JComponent buildCustomAreaWithTopLabelPanel() {
DefaultFormBuilder builder = buildPanelHeader();
CellConstraints cc = new CellConstraints();
builder.appendRow(builder.getLineGapSpec());
builder.appendRow(new RowSpec("top:28dlu:grow"));
builder.nextLine(2);
builder.append("Feedback");
builder.add(new JScrollPane(new JTextArea("Feedback - likely the baselines are not aligned")),
cc.xy(builder.getColumn(), builder.getRow(), "fill, fill"));
return builder.getPanel();
}
/**
* Demonstrates how to append a larger custom area at the end of
* a panel that is build with a DefaultFormBuilder.<p>
*
* We add a gap and a single custom row that grows and that
* is filled vertically (where the default is center vertically).
* The area is separated by a titled separator and it is indented
* using an empty leading label.
*
* @return the custom area panel with separators
*/
private JComponent buildCustomAreaWithSeparatorPanel() {
DefaultFormBuilder builder = buildPanelHeader();
builder.appendSeparator("Customer Feedback");
builder.appendRow(builder.getLineGapSpec());
builder.appendRow(new RowSpec("fill:28dlu:grow"));
builder.nextLine(2);
builder.append("", new JScrollPane(new JTextArea()));
return builder.getPanel();
}
}
Create and configure a layout, create a builder, add components
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Quickly introduces the most important features of the FormLayout:
* create and configure a layout, create a builder, add components.<p>
*
* Note that this class is not a JPanel subclass;
* it justs uses a JPanel as layout container that will be returned
* by <code>#buildPanel()</code>.
*
* @author Karsten Lentzsch
* @version $Revision: 1.8 $
*/
public class QuickStartExample {
private JTextField companyField;
private JTextField contactField;
private JTextField ptiField;
private JTextField powerField;
private JTextField radiusField;
private JTextField diameterField;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Quick Start");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new QuickStartExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
// Component Creation and Initialization **********************************
/**
* Creates, intializes and configures the UI components.
* Real applications may further bind the components to underlying models.
*/
private void initComponents() {
companyField = new JTextField();
contactField = new JTextField();
ptiField = new JTextField(6);
powerField = new JTextField(10);
radiusField = new JTextField(8);
diameterField = new JTextField(8);
}
// Building *************************************************************
/**
* Builds the panel. Initializes and configures components first,
* then creates a FormLayout, configures the layout, creates a builder,
* sets a border, and finally adds the components.
*
* @return the built panel
*/
public JComponent buildPanel() {
// Separating the component initialization and configuration
// from the layout code makes both parts easier to read.
initComponents();
// Create a FormLayout instance on the given column and row specs.
// For almost all forms you specify the columns; sometimes rows are
// created dynamically. In this case the labels are right aligned.
FormLayout layout = new FormLayout(
"right:pref, 3dlu, pref, 7dlu, right:pref, 3dlu, pref", // cols
"p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, p, 3dlu, p"); // rows
// Specify that columns 1 & 5 as well as 3 & 7 have equal widths.
layout.setColumnGroups(new int[][]{{1, 5}, {3, 7}});
// Create a builder that assists in adding components to the container.
// Wrap the panel with a standardized border.
PanelBuilder builder = new PanelBuilder(layout);
builder.setDefaultDialogBorder();
// Obtain a reusable constraints object to place components in the grid.
CellConstraints cc = new CellConstraints();
// Fill the grid with components; the builder offers to create
// frequently used components, e.g. separators and labels.
// Add a titled separator to cell (1, 1) that spans 7 columns.
builder.addSeparator("General", cc.xyw(1, 1, 7));
builder.addLabel("Company", cc.xy (1, 3));
builder.add(companyField, cc.xyw(3, 3, 5));
builder.addLabel("Contact", cc.xy (1, 5));
builder.add(contactField, cc.xyw(3, 5, 5));
builder.addSeparator("Propeller", cc.xyw(1, 7, 7));
builder.addLabel("PTI [kW]", cc.xy (1, 9));
builder.add(ptiField, cc.xy (3, 9));
builder.addLabel("Power [kW]", cc.xy (5, 9));
builder.add(powerField, cc.xy (7, 9));
builder.addLabel("R [mm]", cc.xy (1, 11));
builder.add(radiusField, cc.xy (3, 11));
builder.addLabel("D [mm]", cc.xy (5, 11));
builder.add(diameterField, cc.xy (7, 11));
// The builder holds the layout container that we now return.
return builder.getPanel();
}
}
Demonstrates a frequent pitfall when specifying a growing row
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import javax.swing.text.JTextComponent;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.debug.FormDebugUtils;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates a frequent pitfall when specifying a growing row.
* In this layout a row grows, but the text area in that row is centered
* and doesn"t "grow". In other words, the area doesn"t fill
* the available vertical space.
*
* @author Karsten Lentzsch
* @version $Revision: 1.2 $
*/
public class VerticalGrowthExample {
// UI Components **********************************************************
private JTextComponent notesArea;
// Self Launch ************************************************************
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Vertical Growth");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JComponent panel = new VerticalGrowthExample().buildPanel();
frame.getContentPane().add(panel);
frame.setSize(500, 400);
frame.show();
}
// Component Initialization ***********************************************
/**
* Creates and configures the UI components.
*/
private void initComponents() {
notesArea = new JTextArea(
"This text area doesn"t consume the available vertical space.\n\n"
+ "The row is specified as "pref:grow", and so the row grows.\n"
+ "It"s just that the text area doesn"t fill the row"s vertical space.\n\n"
+ "Since the row"s alignment is not explicitly defined,\n"
+ "it uses the "center" alignment as default. But in this case\n"
+ "we want to "fill". The row spec should read: "fill:pref:grow"."
);
}
// Building ***************************************************************
/**
* Builds and returns a panel with a title and scrollable text area.<p>
*
* The FormDebugUtils dumps
*
* @return the built panel
*/
public JComponent buildPanel() {
initComponents();
FormLayout layout = new FormLayout(
"pref:grow",
"pref, 3dlu, pref:grow" // Correct: "pref, 3dlu, fill:pref:grow"
);
PanelBuilder builder = new PanelBuilder(layout);
builder.setDefaultDialogBorder();
CellConstraints cc = new CellConstraints();
builder.addTitle("An Example for FAQ #3.3", cc.xy(1, 1));
builder.add(new JScrollPane(notesArea), cc.xy(1, 3));
FormDebugUtils.dumpRowSpecs(layout);
FormDebugUtils.dumpConstraints(builder.getPanel());
return builder.getPanel();
}
}
Demonstrates a pure use of the FormLayout
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.ruponent;
import javax.swing.*;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates a <i>pure</i> use of the FormLayout.
* Columns and rows are specified before the panel is filled with
* components. And the panel is filled without a builder.<p>
*
* This panel building style is simple but not recommended. Other panel
* building styles use a builder to fill the panel and/or create
* form rows dynamically. See the {@link PanelBuilderExample} for
* a slightly better panel building style that can use the builder
* to create text labels and separators.
*
* @author Karsten Lentzsch
* @version $Revision: 1.8 $
*
* @see PanelBuilderExample
* @see RowCounterExample
* @see DynamicRowsExample
* @see DefaultFormBuilderExample
*/
public class PlainExample {
private JTextField companyNameField;
private JTextField contactPersonField;
private JTextField orderNoField;
private JTextField inspectorField;
private JTextField referenceNoField;
private JComboBox approvalStatusComboBox;
private JTextField shipYardField;
private JTextField registerNoField;
private JTextField hullNumbersField;
private JComboBox projectTypeComboBox;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Plain Building");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new PlainExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
// Component Creation and Initialization **********************************
/**
* Creates and intializes the UI components.
*/
private void initComponents() {
companyNameField = new JTextField();
contactPersonField = new JTextField();
orderNoField = new JTextField();
inspectorField = new JTextField();
referenceNoField = new JTextField();
approvalStatusComboBox = createApprovalStatusComboBox();
shipYardField = new JTextField();
registerNoField = new JTextField();
hullNumbersField = new JTextField();
projectTypeComboBox = createProjectTypeComboBox();
}
/**
* Creates and returns a combo box for the approval states.
*
* @return a combo box for the approval status
*/
private JComboBox createApprovalStatusComboBox() {
return new JComboBox(
new String[] { "In Progress", "Finished", "Released" });
}
/**
* Creates and returns a combo box for the project types.
*
* @return a combo box for the project type
*/
private JComboBox createProjectTypeComboBox() {
return new JComboBox(
new String[] { "New Building", "Conversion", "Repair" });
}
// Building *************************************************************
/**
* Builds the pane.
*
* @return the built panel
*/
public JComponent buildPanel() {
initComponents();
FormLayout layout = new FormLayout(
"right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
+ "right:max(40dlu;pref), 3dlu, 70dlu",
"p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
"p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
"p, 3dlu, p, 3dlu, p, 3dlu, p");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
// Fill the table with labels and components.
CellConstraints cc = new CellConstraints();
panel.add(createSeparator("Manufacturer"), cc.xyw(1, 1, 7));
panel.add(new JLabel("Company"), cc.xy (1, 3));
panel.add(companyNameField, cc.xyw(3, 3, 5));
panel.add(new JLabel("Contact"), cc.xy (1, 5));
panel.add(contactPersonField, cc.xyw(3, 5, 5));
panel.add(new JLabel("Order No"), cc.xy (1, 7));
panel.add(orderNoField, cc.xy (3, 7));
panel.add(createSeparator("Inspector"), cc.xyw(1, 9, 7));
panel.add(new JLabel("Name"), cc.xy (1, 11));
panel.add(inspectorField, cc.xyw(3, 11, 5));
panel.add(new JLabel("Reference No"), cc.xy (1, 13));
panel.add(referenceNoField, cc.xy (3, 13));
panel.add(new JLabel("Status"), cc.xy (1, 15));
panel.add(approvalStatusComboBox, cc.xy (3, 15));
panel.add(createSeparator("Ship"), cc.xyw(1, 17, 7));
panel.add(new JLabel("Shipyard"), cc.xy (1, 19));
panel.add(shipYardField, cc.xyw(3, 19, 5));
panel.add(new JLabel("Register No"), cc.xy (1, 21));
panel.add(registerNoField, cc.xy (3, 21));
panel.add(new JLabel("Hull No"), cc.xy (5, 21));
panel.add(hullNumbersField, cc.xy (7, 21));
panel.add(new JLabel("Project Type"), cc.xy (1, 23));
panel.add(projectTypeComboBox, cc.xy (3, 23));
return panel;
}
/**
* Creates and answer a separator with a label in the left hand side.
*
* @param text the label"s text
* @return a separator with label in the left hand side
*/
private Component createSeparator(String text) {
return DefaultComponentFactory.getInstance().createSeparator(text);
}
}
Demonstrates how a JTextArea"s preferred size grows with the container if no columns and rows are set
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ruponentAdapter;
import java.awt.event.ruponentEvent;
import javax.swing.*;
import javax.swing.border.rupoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates how a JTextArea"s preferred size grows with the container
* if no columns and rows are set.
*
* @author Karsten Lentzsch
* @version $Revision: 1.3 $
*/
public class GrowingTextAreaExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Growing Text Area");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new GrowingTextAreaExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
String example1Text =
"Here the layout uses a fixed initial size of 200 dlu. "
+ "The area"s minimum and preferred sizes will be ignored. "
+ "And so, the area will grow and shrink.";
tabbedPane.add("1",
buildTab("Fixed Size (Good)",
"fill:200dlu:grow",
createArea(example1Text, true, 0, null)));
String example2Text =
"This text area has line wrapping disabled\n"
+ "and uses a hand-wrapped text (using "\\n").\n\n"
+ "Its minimum and preferred sizes are constant and so,\n"
+ "the area will grow but shrink down to its minimum size.";
tabbedPane.add("2",
buildTab("Pref Size, Line Wrap Disabled (Good)",
"fill:pref:grow",
createArea(example2Text, false, 0, null)));
String example3Text =
"This text area grows horizontally and will never shrink again. "
+ "Since line wrapping is enabled, "
+ "the area"s preferred size is defined by its size. "
+ "(See BasicTextAreaUI#getPreferredSize(Component).\n\n"
+ "If the layout container grows, the layout manager "
+ "sets a larger size, and hence, the preferred size grows."
+ "The FormLayout honors the area"s preferred size to compute "
+ "the new column width, and so the area won"t shrink again.\n\n"
+ "Even if you use a "default" width the column won"t shrink.";
tabbedPane.add("3",
buildTab("Pref Size, Line Wrap Enabled (Never Shrinks)",
"fill:pref:grow",
createArea(example3Text, true, 0, null)));
String example4Text =
"This text area grows but never shrinks. "
+ "Since line wrapping is enabled, the area"s "
+ "minimum and preferred sizes are defined by its size.\n\n"
+ "If the layout container grows, the layout manager "
+ "sets a larger size, and hence, the minimum and preferred sizes grow. "
+ "If the layout container shrinks, the layout manager can shrink "
+ "the column width down to the area"s minimum width. "
+ "But the minimum size is like the preferred size determined "
+ "by the size previously set, and so, the column won"t shrink.\n\n"
+ "A solution to this problem is to set a custom minimum size.";
tabbedPane.add("4",
buildTab("Default Size, Line Wrap Enabled (Never Shrinks)",
"fill:default:grow",
createArea(example4Text, true, 0, null)));
String example5Text =
"This text area has uses a column width of 30 characters. "
+ "But that just affects the initial preferred and minimum size."
+ "The area grows and won"t shrink again - just as in tabs 3 and 4.";
tabbedPane.add("5",
buildTab("Default Size, Line Wrap Enabled, Columns Set (Never Shrinks)",
"fill:default:grow",
createArea(example5Text, true, 30, null)));
String example6Text =
"This text area grows and shrinks. "
+ "Since line wrapping is enabled, "
+ "the area"s preferred size is defined by its size. "
+ "Here a custom minimum size (100, 32) has been set.\n\n"
+ "If the layout container grows, the layout manager "
+ "sets a larger size, and hence, the preferred size grows. "
+ "However, if the layout container shrinks, the layout can "
+ "shrink the column down to the area"s minimum width, which is 100; "
+ "the minimum size is independent from the size previously set.";
tabbedPane.add("6",
buildTab("Default Size, Line Wrap Enabled, Min Size Set (Good)",
"fill:default:grow",
createArea(example6Text, true, 0, new Dimension(100, 32))));
return tabbedPane;
}
private JTextArea createArea(
String text,
boolean lineWrap,
int columns,
Dimension minimumSize) {
JTextArea area = new JTextArea(text);
area.setBorder(new CompoundBorder(
new LineBorder(Color.GRAY),
new EmptyBorder(1, 3, 1, 1)));
area.setLineWrap(lineWrap);
area.setWrapStyleWord(true);
area.setColumns(columns);
if (minimumSize != null) {
area.setMinimumSize(new Dimension(100, 32));
}
return area;
}
private JComponent buildTab(String title, String columnSpec, JTextArea area) {
JLabel columnSpecLabel = new JLabel(columnSpec);
columnSpecLabel.setHorizontalAlignment(JLabel.CENTER);
FormLayout layout = new FormLayout(
columnSpec,
"pref, 9dlu, pref, 3dlu, fill:default:grow, 9dlu, pref");
PanelBuilder builder = new PanelBuilder(layout);
builder.setDefaultDialogBorder();
CellConstraints cc = new CellConstraints();
builder.addTitle(title, cc.xy(1, 1));
builder.add(columnSpecLabel, cc.xy(1, 3));
builder.add(area, cc.xy(1, 5));
builder.add(buildInfoPanel(area), cc.xy(1, 7));
return builder.getPanel();
}
private JComponent buildInfoPanel(JTextArea area) {
JLabel sizeLabel = new JLabel();
JLabel minSizeLabel = new JLabel();
JLabel prefSizeLabel = new JLabel();
JLabel lineWrapLabel = new JLabel(area.getLineWrap() ? "enabled" : "disabled");
JLabel customMinLabel = new JLabel(area.isMinimumSizeSet() ? "set" : "computed");
JLabel columnsLabel = new JLabel(area.getColumns() == 0
? "not specified"
: String.valueOf(area.getColumns()));
area.addComponentListener(new SizeChangeHandler(
area, sizeLabel, minSizeLabel, prefSizeLabel));
FormLayout layout = new FormLayout("pref, 4dlu, pref, 21dlu, pref, 4dlu, pref");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.append("Size:", sizeLabel);
builder.append("Line wrap:", lineWrapLabel);
builder.append("Min size:", minSizeLabel);
builder.append("Min size:", customMinLabel);
builder.append("Pref size:", prefSizeLabel);
builder.append("Columns:", columnsLabel);
return builder.getPanel();
}
// Listens to area size changes and writes the formatted sizes to the given labels.
private static class SizeChangeHandler extends ComponentAdapter {
private final JTextArea area;
private final JLabel sizeLabel;
private final JLabel minSizeLabel;
private final JLabel prefSizeLabel;
private SizeChangeHandler(
JTextArea area,
JLabel sizeLabel,
JLabel minSizeLabel,
JLabel prefSizeLabel) {
this.area = area;
this.sizeLabel = sizeLabel;
this.minSizeLabel = minSizeLabel;
this.prefSizeLabel = prefSizeLabel;
}
public void componentResized(ComponentEvent evt) {
sizeLabel.setText(format(area.getSize()));
minSizeLabel.setText(format(area.getMinimumSize()));
prefSizeLabel.setText(format(area.getPreferredSize()));
}
private String format(Dimension d) {
return String.valueOf(d.width) + ", " + d.height;
}
}
}
Demonstrates how to build button bars using a ButtonBarBuilder
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.ruponent;
import java.awt.Insets;
import javax.swing.*;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates how to build button bars using a ButtonBarBuilder.
*
* @author Karsten Lentzsch
* @version $Revision: 1.16 $
*
* @see ButtonBarBuilder
* @see com.jgoodies.forms.factories.ButtonBarFactory
*/
public class ButtonBarsExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Button Bars");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new ButtonBarsExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add(buildButtonBar1Panel(), "No Builder");
tabbedPane.add(buildButtonBar2Panel(), "Builder");
tabbedPane.add(buildButtonBar3Panel(), "Related");
tabbedPane.add(buildButtonBar4Panel(), "Unrelated ");
tabbedPane.add(buildButtonMixedBar1Panel(), "Mix");
tabbedPane.add(buildButtonMixedBar2Panel(), "Mix Narrow");
return tabbedPane;
}
private Component buildButtonBar1Panel() {
JPanel buttonBar = new JPanel(
new FormLayout("0:grow, p, 4px, p", "p"));
buttonBar.add(new JButton("Yes"), "2, 1");
buttonBar.add(new JButton("No"), "4, 1");
return wrap(buttonBar,
"This bar has been built without a ButtonBarBuilder:\n" + " o buttons have no minimum widths,\n" +
" o the button order is fixed left-to-right,\n" + " o gaps may be inconsistent between team members.");
}
private Component buildButtonBar2Panel() {
ButtonBarBuilder builder = new ButtonBarBuilder();
builder.addGlue();
builder.addGriddedButtons(new JButton[] {
new JButton("Yes"),
new JButton("No")
});
return wrap(builder.getPanel(),
"This bar has been built with a ButtonBarBuilder:\n" +
" o buttons have a minimum widths,\n" +
" o the button order honors the platform default,\n" +
" o the button gap is a logical size that follows a style guide.");
}
private Component buildButtonBar3Panel() {
ButtonBarBuilder builder = new ButtonBarBuilder();
builder.addGlue();
builder.addGriddedButtons(new JButton[] {
new JButton("One"),
new JButton("Two"),
new JButton("Three")
});
return wrap(builder.getPanel(),
"This bar uses the logical gap for related buttons.\n");
}
private Component buildButtonBar4Panel() {
ButtonBarBuilder builder = new ButtonBarBuilder();
builder.addGlue();
builder.addGridded(new JButton("One"));
builder.addUnrelatedGap();
builder.addGridded(new JButton("Two"));
builder.addUnrelatedGap();
builder.addGridded(new JButton("Three"));
return wrap(builder.getPanel(),
"This bar uses the logical gap for unrelated buttons.\n" +
"It is a little bit wider than the related gap.");
}
private Component buildButtonMixedBar1Panel() {
ButtonBarBuilder builder = new ButtonBarBuilder();
builder.addGridded(new JButton("Help"));
builder.addGlue();
builder.addUnrelatedGap();
builder.addFixed(new JButton("Copy to Clipboard"));
builder.addUnrelatedGap();
builder.addGriddedButtons(new JButton[] {
new JButton("OK"),
new JButton("Cancel")
});
return wrap(builder.getPanel(),
"Demonstrates a glue (between Help and the rest),\n" +
"has related and unrelated buttons and an ungridded button\n" +
"with a default margin (Copy to Clipboard).");
}
private Component buildButtonMixedBar2Panel() {
ButtonBarBuilder builder = new ButtonBarBuilder();
builder.addGridded(new JButton("Help"));
builder.addGlue();
builder.addUnrelatedGap();
builder.addFixedNarrow(new JButton("Copy to Clipboard"));
builder.addUnrelatedGap();
builder.addGriddedButtons(new JButton[] {
new JButton("OK"),
new JButton("Cancel")
});
return wrap(builder.getPanel(),
"Demonstrates a glue (between Help and the rest),\n" +
"has related and unrelated buttons and an ungridded button\n" +
"with a narrow margin (Copy to Clipboard).\n\n"+
"Note that some look&feels do not support the narrow margin\n" +
"feature, and conversely, others have only narrow margins.");
}
// Helper Code ************************************************************
private static Component wrap(Component buttonBar, String text) {
JTextArea textArea = new JTextArea(text);
textArea.setMargin(new Insets(6, 10, 4, 6));
Component textPane = new JScrollPane(textArea);
FormLayout layout = new FormLayout(
"fill:100dlu:grow",
"fill:56dlu:grow, 4dlu, p");
JPanel panel = new JPanel(layout);
CellConstraints cc = new CellConstraints();
panel.setBorder(Borders.DIALOG_BORDER);
panel.add(textPane, cc.xy(1, 1));
panel.add(buttonBar, cc.xy(1, 3));
return panel;
}
}
Demonstrates how to build button bars with a fixed button order
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.ruponent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates how to build button bars with a fixed button order
* or with a button order that honors the platform"s style.
*
* @author Karsten Lentzsch
* @version $Revision: 1.4 $
*
* @see ButtonBarBuilder
* @see com.jgoodies.forms.factories.ButtonBarFactory
*/
public class ButtonOrderExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Button Order");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new ButtonOrderExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
/**
* Builds and returns a panel that consists of three paragraphs
* that demonstrate different button orders. Each paragraph contains
* a button bar that is built from a button sequence, and another
* bar that is built from individual buttons.
*
* @return a panel that demonstrates button order
*/
public JComponent buildPanel() {
FormLayout layout = new FormLayout("right:pref:grow, 4dlu, pref");
DefaultFormBuilder rowBuilder = new DefaultFormBuilder(layout);
rowBuilder.setDefaultDialogBorder();
rowBuilder.appendSeparator("Left to Right");
rowBuilder.append("Ordered", buildButtonSequence(ButtonBarBuilder.createLeftToRightBuilder()));
rowBuilder.append("Fixed", buildIndividualButtons(ButtonBarBuilder.createLeftToRightBuilder()));
rowBuilder.appendSeparator("Right to Left");
rowBuilder.append("Ordered", buildButtonSequence(createRightToLeftBuilder()));
rowBuilder.append("Fixed", buildIndividualButtons(createRightToLeftBuilder()));
rowBuilder.appendSeparator("Platform Default Order");
rowBuilder.append("Ordered", buildButtonSequence(new ButtonBarBuilder()));
rowBuilder.append("Fixed", buildIndividualButtons(new ButtonBarBuilder()));
return rowBuilder.getPanel();
}
/**
* Builds and returns a button bar honoring the builder"s button order.
*
* @param builder the builder used to build the bar
* @return a button bar that honors the builder"s button order
*/
private Component buildButtonSequence(ButtonBarBuilder builder) {
builder.addGriddedButtons(new JButton[] {
new JButton("One"),
new JButton("Two"),
new JButton("Three")
});
return builder.getPanel();
}
/**
* Builds and returns a button bar ignoring the builder"s button order.
* Instead a fixed left to right order is used.
*
* @param builder the builder used to build the bar
* @return a button bar with a fixed left to right button order
*/
private Component buildIndividualButtons(ButtonBarBuilder builder) {
builder.addGridded(new JButton("One"));
builder.addRelatedGap();
builder.addGridded(new JButton("Two"));
builder.addRelatedGap();
builder.addGridded(new JButton("Three"));
return builder.getPanel();
}
/**
* Creates and returns a button bar builder with a fixed
* right-to-left button order. Unlike the factory method
* {@link ButtonBarBuilder#createLeftToRightBuilder()}
* this method is useful for demonstration purposes only.
*
* @return a ButtonBarBuilder with right-to-left button order
*/
private static ButtonBarBuilder createRightToLeftBuilder() {
ButtonBarBuilder builder = new ButtonBarBuilder();
builder.setLeftToRightButtonOrder(false);
return builder;
}
}
Demonstrates how to find bugs in the layout using
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.debug.FormDebugUtils;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates how to find bugs in the layout using
* the {@link FormDebugPanel} and the {@link FormDebugUtils}.<p>
*
* The example also demonstrates efficient panel building with
* the DefaultFormBuilder. The builder has been configured
* to use a leading indent column.
*
* @author Karsten Lentzsch
* @version $Revision: 1.12 $
*/
public class FormDebugExample {
private JTextField fileNumberField;
private JTextField rfqNumberField;
private JTextField blNumberField;
private JTextField mblNumberField;
private JTextField customerKeyField;
private JTextField customerAddressField;
private JTextField shipperKeyField;
private JTextField shipperAddressField;
private JTextField consigneeKeyField;
private JTextField consigneeAddressField;
private JTextField departureCodeField;
private JTextField departurePortField;
private JTextField destinationCodeField;
private JTextField destinationPortField;
private JTextField deliveryDateField;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Debug a Form");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new FormDebugExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
// Component Creation and Initialization **********************************
/**
* Creates and intializes the UI components.
*/
private void initComponents() {
fileNumberField = new JTextField();
rfqNumberField = new JTextField();
blNumberField = new JTextField();
mblNumberField = new JTextField();
customerKeyField = new JTextField();
customerAddressField = new JTextField();
customerAddressField.setEditable(false);
shipperKeyField = new JTextField();
shipperAddressField = new JTextField();
shipperAddressField.setEditable(false);
consigneeKeyField = new JTextField();
consigneeAddressField = new JTextField();
consigneeAddressField.setEditable(false);
departureCodeField = new JTextField();
departurePortField = new JTextField();
departurePortField.setEditable(false);
destinationCodeField = new JTextField();
destinationPortField = new JTextField();
destinationPortField.setEditable(false);
deliveryDateField = new JTextField();
}
// Building *************************************************************
/**
* Builds the panel.
*
* @return the built panel
*/
public JComponent buildPanel() {
initComponents();
FormLayout layout = new FormLayout(
"12dlu, pref, 3dlu, max(45dlu;min), 2dlu, min, 2dlu, min, 2dlu, min");
layout.setColumnGroups(new int[][] { { 4, 6, 8, 10 } });
DefaultFormBuilder builder =
new DefaultFormBuilder(layout, new FormDebugPanel());
builder.setDefaultDialogBorder();
builder.setLeadingColumnOffset(1);
builder.appendSeparator("General");
builder.append("File Number", fileNumberField, 7);
builder.append("RFQ Number", rfqNumberField, 7);
builder.append("BL/MBL", blNumberField, mblNumberField); builder.nextLine();
builder.appendSeparator("Addresses");
builder.append("Customer", customerKeyField, customerAddressField, 5);
builder.append("Shipper", shipperKeyField, shipperAddressField, 5);
builder.append("Consignee", consigneeKeyField, consigneeAddressField, 5);
builder.appendSeparator("Transport");
builder.append("Departure", departureCodeField, departurePortField, 5);
builder.append("Destination", destinationCodeField, destinationPortField, 5);
builder.append("Delivery Date", deliveryDateField); builder.nextLine();
FormDebugUtils.dumpAll(builder.getPanel());
return builder.getPanel();
}
}
Demonstrates sizes: constant, minimum, preferred
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates the basic FormLayout sizes: constant, minimum, preferred.
*
* @author Karsten Lentzsch
* @version $Revision: 1.9 $
*/
public class BasicSizesExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Basic Sizes");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new BasicSizesExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add("Horizontal", buildHorizontalSizesPanel());
tabbedPane.add("Vertical", buildVerticalSizesPanel());
return tabbedPane;
}
private JComponent buildHorizontalSizesPanel() {
FormLayout layout = new FormLayout(
"pref, 12px, " + "75px, 25px, min, 25px, pref",
"pref, 12px, pref");
// Create a panel that uses the layout.
JPanel panel = new JPanel(layout);
// Set a default border.
panel.setBorder(Borders.DIALOG_BORDER);
// Create a reusable CellConstraints instance.
CellConstraints cc = new CellConstraints();
// Add components to the panel.
panel.add(new JLabel("75px"), cc.xy(3, 1));
panel.add(new JLabel("Min"), cc.xy(5, 1));
panel.add(new JLabel("Pref"), cc.xy(7, 1));
panel.add(new JLabel("new JTextField(15)"), cc.xy(1, 3));
panel.add(new JTextField(15), cc.xy(3, 3));
panel.add(new JTextField(15), cc.xy(5, 3));
panel.add(new JTextField(15), cc.xy(7, 3));
return panel;
}
private JComponent buildVerticalSizesPanel() {
FormLayout layout = new FormLayout(
"pref, 12px, pref",
"pref, 12px, 45px, 12px, min, 12px, pref");
// Create a panel that uses the layout.
JPanel panel = new JPanel(layout);
// Set a default border.
panel.setBorder(Borders.DIALOG_BORDER);
// Create a reusable CellConstraints instance.
CellConstraints cc = new CellConstraints();
// Add components to the panel.
panel.add(new JLabel("new JTextArea(10, 40)"), cc.xy(3, 1));
panel.add(new JLabel("45px"), cc.xy(1, 3));
panel.add(new JLabel("Min"), cc.xy(1, 5));
panel.add(new JLabel("Pref"), cc.xy(1, 7));
panel.add(createTextArea(10, 40), cc.xy(3, 3));
panel.add(createTextArea(10, 40), cc.xy(3, 5));
panel.add(createTextArea(10, 40), cc.xy(3, 7));
return panel;
}
private JComponent createTextArea(int rows, int cols) {
return new JScrollPane(new JTextArea(rows, cols),
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
}
// Helper Classes *********************************************************
/**
* Creates and returns a button that can have predefined minimum
* and preferred sizes. In the constructor you can specify or omit
* the minimum width, height and preferred width/height.
*/
// private static class SpecialSizeButton extends JButton {
//
// private final Dimension fixedMinimumSize;
// private final Dimension fixedPreferredSize;
//
// private SpecialSizeButton(
// String text,
// Dimension fixedMinimumSize,
// Dimension fixedPreferredSize) {
// super(text);
// this.fixedMinimumSize = fixedMinimumSize;
// this.fixedPreferredSize = fixedPreferredSize;
// //putClientProperty("jgoodies.isNarrow", Boolean.TRUE);
// }
//
// public Dimension getMinimumSize() {
// Dimension d = super.getMinimumSize();
// return new Dimension(
// fixedMinimumSize.width == -1
// ? d.width
// : fixedMinimumSize.width,
// fixedMinimumSize.height == -1
// ? d.height
// : fixedMinimumSize.height);
// }
//
// public Dimension getPreferredSize() {
// Dimension d = super.getPreferredSize();
// return new Dimension(
// fixedPreferredSize.width == -1
// ? d.width
// : fixedPreferredSize.width,
// fixedPreferredSize.height == -1
// ? d.height
// : fixedPreferredSize.height);
// }
// }
}
Demonstrates the basic FormLayout sizes: constant, minimum, preferred
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates the basic FormLayout sizes: constant, minimum, preferred.
*
* @author Karsten Lentzsch
* @version $Revision: 1.10 $
*/
public class BoundedSizesExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Bounded Sizes");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new BoundedSizesExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add("Jumping 1", buildJumping1Panel());
tabbedPane.add("Jumping 2", buildJumping2Panel());
tabbedPane.add("Stable 1", buildStable1Panel());
tabbedPane.add("Stable 2", buildStable2Panel());
return tabbedPane;
}
private JComponent buildJumping1Panel() {
FormLayout layout = new FormLayout(
"right:pref, 4dlu, max(35dlu;min), 2dlu, min, 2dlu, min, 2dlu, min, ",
EDITOR_ROW_SPEC);
return buildEditorGeneralPanel(layout);
}
private JComponent buildJumping2Panel() {
FormLayout layout = new FormLayout(
"right:pref, 4dlu, max(35dlu;min), 2dlu, min, 2dlu, min, 2dlu, min, ",
EDITOR_ROW_SPEC);
return buildEditorTransportPanel(layout);
}
private JComponent buildStable1Panel() {
FormLayout layout = new FormLayout(
"right:max(50dlu;pref), 4dlu, max(35dlu;min), 2dlu, min, 2dlu, min, 2dlu, min, ",
EDITOR_ROW_SPEC);
return buildEditorGeneralPanel(layout);
}
private JComponent buildStable2Panel() {
FormLayout layout = new FormLayout(
"right:max(50dlu;pref), 4dlu, max(35dlu;min), 2dlu, min, 2dlu, min, 2dlu, min, ",
EDITOR_ROW_SPEC);
return buildEditorTransportPanel(layout);
}
private static final String EDITOR_ROW_SPEC =
"p, 3dlu, p, 3dlu, p, 3dlu, p";
/**
* Builds and answer the editor"s general tab for the given layout.
*
* @param layout the layout to be used
* @return the editor"s general panel
*/
private JComponent buildEditorGeneralPanel(FormLayout layout) {
layout.setColumnGroups(new int[][] { { 3, 5, 7, 9 } });
PanelBuilder builder = new PanelBuilder(layout);
builder.setDefaultDialogBorder();
CellConstraints cc = new CellConstraints();
builder.addLabel("File number:", cc.xy (1, 1));
builder.add(new JTextField(), cc.xyw(3, 1, 7));
builder.addLabel("RFQ number:", cc.xy (1, 3));
builder.add(new JTextField(), cc.xyw(3, 3, 7));
builder.addLabel("Entry date:", cc.xy (1, 5));
builder.add(new JTextField(), cc.xy (3, 5));
builder.addLabel("Sales Person:", cc.xy (1, 7));
builder.add(new JTextField(), cc.xyw(3, 7, 7));
return builder.getPanel();
}
/**
* Builds and answer the editor"s transport tab for the given layout.
*
* @param layout the layout to be used
* @return the editor"s transport panel
*/
private JComponent buildEditorTransportPanel(FormLayout layout) {
layout.setColumnGroups(new int[][] { { 3, 5, 7, 9 } });
PanelBuilder builder = new PanelBuilder(layout);
builder.setDefaultDialogBorder();
CellConstraints cc = new CellConstraints();
builder.addLabel("Shipper:", cc.xy (1, 1));
builder.add(new JTextField(), cc.xy (3, 1));
builder.add(new JTextField(), cc.xyw(5, 1, 5));
builder.addLabel("Consignee:", cc.xy (1, 3));
builder.add(new JTextField(), cc.xy (3, 3));
builder.add(new JTextField(), cc.xyw(5, 3, 5));
builder.addLabel("Departure:", cc.xy (1, 5));
builder.add(new JTextField(), cc.xy (3, 5));
builder.add(new JTextField(), cc.xyw(5, 5, 5));
builder.addLabel("Destination:", cc.xy (1, 7));
builder.add(new JTextField(), cc.xy (3, 7));
builder.add(new JTextField(), cc.xyw(5, 7, 5));
return builder.getPanel();
}
}
Demonstrates the different FormLayout alignments
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates the different FormLayout alignments.
*
* @author Karsten Lentzsch
* @version $Revision: 1.9 $
*/
public class AlignmentExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Alignments");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new AlignmentExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add("Horizontal", buildHorizontalButtons());
tabbedPane.add("Vertical", buildVerticalButtons());
return tabbedPane;
}
private JComponent buildHorizontalButtons() {
FormLayout layout = new FormLayout(
"left:pref, 15px, center:pref, 15px, right:pref, 15px, fill:pref, 15px, pref",
"pref, 12px, pref, 4px, pref, 4px, pref, 4px, pref, 4px, pref");
// Create a panel that uses the layout.
JPanel panel = new JPanel(layout);
// Set a default border.
panel.setBorder(Borders.DIALOG_BORDER);
// Create a reusable CellConstraints instance.
CellConstraints cc = new CellConstraints();
// Add components to the panel.
panel.add(new JLabel("Left"), cc.xy(1, 1));
panel.add(new JButton("Name"), cc.xy(1, 3));
panel.add(new JButton("Phone"), cc.xy(1, 5));
panel.add(new JButton("Fax"), cc.xy(1, 7));
panel.add(new JButton("Email"), cc.xy(1, 9));
panel.add(new JButton("Address"), cc.xy(1, 11));
panel.add(new JLabel("Center"), cc.xy(3, 1));
panel.add(new JButton("Name"), cc.xy(3, 3));
panel.add(new JButton("Phone"), cc.xy(3, 5));
panel.add(new JButton("Fax"), cc.xy(3, 7));
panel.add(new JButton("Email"), cc.xy(3, 9));
panel.add(new JButton("Address"), cc.xy(3, 11));
panel.add(new JLabel("Right"), cc.xy(5, 1));
panel.add(new JButton("Name"), cc.xy(5, 3));
panel.add(new JButton("Phone"), cc.xy(5, 5));
panel.add(new JButton("Fax"), cc.xy(5, 7));
panel.add(new JButton("Email"), cc.xy(5, 9));
panel.add(new JButton("Address"), cc.xy(5, 11));
panel.add(new JLabel("Fill"), cc.xy(7, 1, "center, center"));
panel.add(new JButton("Name"), cc.xy(7, 3));
panel.add(new JButton("Phone"), cc.xy(7, 5));
panel.add(new JButton("Fax"), cc.xy(7, 7));
panel.add(new JButton("Email"), cc.xy(7, 9));
panel.add(new JButton("Address"), cc.xy(7, 11));
panel.add(new JLabel("Default"), cc.xy(9, 1, "center, center"));
panel.add(new JButton("Name"), cc.xy(9, 3));
panel.add(new JButton("Phone"), cc.xy(9, 5));
panel.add(new JButton("Fax"), cc.xy(9, 7));
panel.add(new JButton("Email"), cc.xy(9, 9));
panel.add(new JButton("Address"), cc.xy(9, 11));
return panel;
}
private JComponent buildVerticalButtons() {
FormLayout layout = new FormLayout(
"pref, 8dlu, pref, 4dlu, pref",
"top:pref, 9dlu, center:pref, 9dlu, bottom:pref, 9dlu, fill:pref, 9dlu, pref");
// Create a panel that uses the layout.
JPanel panel = new JPanel(layout);
// Set a default border.
panel.setBorder(Borders.DIALOG_BORDER);
// Create a reusable CellConstraints instance.
CellConstraints cc = new CellConstraints();
// Add components to the panel.
panel.add(new JLabel("Top"), cc.xy(1, 1));
panel.add(createSmallButton(), cc.xy(3, 1));
panel.add(createMediumButton(), cc.xy(5, 1));
panel.add(new JLabel("Center"), cc.xy(1, 3));
panel.add(createSmallButton(), cc.xy(3, 3));
panel.add(createMediumButton(), cc.xy(5, 3));
panel.add(new JLabel("Bottom"), cc.xy(1, 5));
panel.add(createSmallButton(), cc.xy(3, 5));
panel.add(createMediumButton(), cc.xy(5, 5));
panel.add(new JLabel("Fill"), cc.xy(1, 7));
panel.add(createSmallButton(), cc.xy(3, 7));
panel.add(createMediumButton(), cc.xy(5, 7));
panel.add(new JLabel("Default"), cc.xy(1, 9));
panel.add(createSmallButton(), cc.xy(3, 9));
panel.add(createMediumButton(), cc.xy(5, 9));
return panel;
}
private JButton createSmallButton() {
return new JButton("<html>One</html>");
}
private JButton createMediumButton() {
return new JButton("<html>One<br>Two</html>");
}
}
FormLayout: Basic Example 1
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import javax.swing.*;
public class FormLayoutExample1 extends JPanel {
public FormLayoutExample1() {
FormLayout formLayout = new FormLayout("pref", "pref, pref");
setLayout(formLayout);
CellConstraints c = new CellConstraints();
add(new JLabel("Component 1"), c.xy(1, 1));
add(new JLabel("Component 2"), c.xy(1, 2));
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Basic Example 1");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample1());
f.pack();
f.setVisible(true);
}
}
FormLayout: Bounds Example 6
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class FormLayoutExample6 extends JPanel {
public FormLayoutExample6() {
super(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
String labelWidth = "50dlu";
FormLayout formLayout = new FormLayout("max(" + labelWidth + ";pref), 2dlu, 100dlu", "default, default, default");
JPanel tabPanel1 = new FormDebugPanel();
tabPanel1.setLayout(formLayout);
CellConstraints c = new CellConstraints();
tabPanel1.add(createLabel("Small 1"), c.xy(1, 1));
tabPanel1.add(createLabel("Larger Label"), c.xy(1, 2));
tabPanel1.add(createLabel("Med. Label"), c.xy(1, 3));
tabPanel1.add(new JTextField(), c.xy(3, 1));
tabPanel1.add(new JTextField(), c.xy(3, 2));
tabPanel1.add(new JTextField(), c.xy(3, 3));
tabbedPane.add("Tab1", tabPanel1);
formLayout = new FormLayout("max(" + labelWidth + ";pref), 2dlu, 100dlu", "default, default, default");
JPanel tabPanel2 = new FormDebugPanel();
tabPanel2.setLayout(formLayout);
tabPanel2.add(createLabel("Small 2"), c.xy(1, 1));
tabPanel2.add(createLabel("Small 3"), c.xy(1, 2));
tabPanel2.add(createLabel("Small 4"), c.xy(1, 3));
tabPanel2.add(new JTextField(), c.xy(3, 1));
tabPanel2.add(new JTextField(), c.xy(3, 2));
tabPanel2.add(new JTextField(), c.xy(3, 3));
tabbedPane.add("Tab2", tabPanel2);
formLayout = new FormLayout("pref, 2dlu, 100dlu", "default, default, default");
JPanel tabPanel3 = new FormDebugPanel();
tabPanel3.setLayout(formLayout);
tabPanel3.add(createLabel("Small 5"), c.xy(1, 1));
tabPanel3.add(createLabel("Small 6"), c.xy(1, 2));
tabPanel3.add(createLabel("Small 7"), c.xy(1, 3));
tabPanel3.add(new JTextField(), c.xy(3, 1));
tabPanel3.add(new JTextField(), c.xy(3, 2));
tabPanel3.add(new JTextField(), c.xy(3, 3));
tabbedPane.add("Tab3", tabPanel3);
JPanel panel = new JPanel(new BorderLayout());
panel.add(tabbedPane, BorderLayout.CENTER);
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Bounds Example 6");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample7());
f.pack();
f.setVisible(true);
}
}
FormLayout: Button Bar Builder Example
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
public class ButtonBarBuilderExample1 extends JPanel {
public ButtonBarBuilderExample1() {
super(new BorderLayout());
ButtonBarBuilder builder = new ButtonBarBuilder(new FormDebugPanel());
builder.addGridded(new JButton("Help"));
builder.addRelatedGap();
builder.addGlue();
builder.addGriddedButtons(new JButton[]{new JButton("Ok"), new JButton("Cancel"), new JButton("Apply")});
add(builder.getPanel());
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Button Bar Builder Example");
f.setDefaultCloseOperation(2);
f.add(new ButtonBarBuilderExample1());
f.pack();
f.setVisible(true);
}
}
FormLayout: Button Bar Builder Example 2
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
public class ButtonBarBuilderExample2 extends JPanel {
public ButtonBarBuilderExample2() {
super(new BorderLayout());
ButtonBarBuilder builder = new ButtonBarBuilder(new FormDebugPanel());
builder.setLeftToRightButtonOrder(true);
builder.addGridded(new JButton("Help"));
builder.addRelatedGap();
builder.addGlue();
builder.addGriddedButtons(new JButton[]{new JButton("Ok"), new JButton("Cancel"), new JButton("Apply")});
add(builder.getPanel());
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Button Bar Builder Example 2");
f.setDefaultCloseOperation(2);
f.add(new ButtonBarBuilderExample2());
f.pack();
f.setVisible(true);
}
}
FormLayout: Button Bar Builder Example 3
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
public class ButtonBarBuilderExample3 extends JPanel {
public ButtonBarBuilderExample3() {
super(new BorderLayout());
ButtonBarBuilder builder = new ButtonBarBuilder(new FormDebugPanel());
builder.addGridded(new JButton("Help"));
builder.addRelatedGap();
builder.addGlue();
builder.addFixed(new JButton("Ok"));
builder.addFixed(new JButton("Cancel"));
builder.addFixed(new JButton("Apply"));
add(builder.getPanel());
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Button Bar Builder Example 3");
f.setDefaultCloseOperation(2);
f.add(new ButtonBarBuilderExample3());
f.pack();
f.setVisible(true);
}
}
FormLayout: Button Stack Builder Example 1
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jgoodies.forms.builder.ButtonStackBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
public class ButtonStackBuilderExample1 extends JPanel {
public ButtonStackBuilderExample1() {
super(new BorderLayout());
ButtonStackBuilder builder = new ButtonStackBuilder(new FormDebugPanel());
builder.addGridded(new JButton("Button1"));
builder.addGridded(new JButton("Button2"));
JButton bigGriddedButton = new JButton("Button3");
bigGriddedButton.setFont(bigGriddedButton.getFont().deriveFont(30.0f));
builder.addGridded(bigGriddedButton);
builder.addUnrelatedGap();
builder.addFixed(new JButton("Button4"));
JButton bigFixedButton = new JButton("Button5");
bigFixedButton.setFont(bigFixedButton.getFont().deriveFont(24.0f));
builder.addFixed(bigFixedButton);
builder.addUnrelatedGap();
builder.addButtons(new JButton[]{new JButton("Button6"), new JButton("Button7")});
add(builder.getPanel());
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Button Stack Builder Example 1");
f.setDefaultCloseOperation(2);
f.add(new ButtonStackBuilderExample1());
f.pack();
f.setVisible(true);
}
}
FormLayout: Default Alignment Example 2
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import javax.swing.*;
public class FormLayoutExample2 extends JPanel {
public FormLayoutExample2() {
super(new BorderLayout());
FormDebugPanel panel = new FormDebugPanel();
FormLayout formLayout =
new FormLayout("left:100px, right:100px, center:100px, fill:100px", "top:100px, bottom:100px, center:100px, fill:100px");
panel.setLayout(formLayout);
CellConstraints c = new CellConstraints();
panel.add(createLabel("L/T", 50, 50), c.xy(1, 1));
panel.add(createLabel("L/B", 50, 50), c.xy(1, 2));
panel.add(createLabel("L/C", 50, 50), c.xy(1, 3));
panel.add(createLabel("L/F", 50, 50), c.xy(1, 4));
panel.add(createLabel("R/T", 50, 50), c.xy(2, 1));
panel.add(createLabel("R/B", 50, 50), c.xy(2, 2));
panel.add(createLabel("R/C", 50, 50), c.xy(2, 3));
panel.add(createLabel("R/F", 50, 50), c.xy(2, 4));
panel.add(createLabel("C/T", 50, 50), c.xy(3, 1));
panel.add(createLabel("C/B", 50, 50), c.xy(3, 2));
panel.add(createLabel("C/C", 50, 50), c.xy(3, 3));
panel.add(createLabel("C/F", 50, 50), c.xy(3, 4));
panel.add(createLabel("F/T", 50, 50), c.xy(4, 1));
panel.add(createLabel("F/B", 50, 50), c.xy(4, 2));
panel.add(createLabel("F/C", 50, 50), c.xy(4, 3));
panel.add(createLabel("F/F", 50, 50), c.xy(4, 4));
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Default Alignment Example 2");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample2());
f.pack();
f.setVisible(true);
}
}
FormLayout: Default Form Builder Example 1
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
public class DefaultFormBuilderExample1 extends JPanel {
public DefaultFormBuilderExample1() {
super(new BorderLayout());
DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout(""));
builder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
builder.appendColumn("right:pref");
builder.appendColumn("3dlu");
builder.appendColumn("fill:max(pref; 100px)");
builder.appendColumn("5dlu");
builder.appendColumn("right:pref");
builder.appendColumn("3dlu");
builder.appendColumn("fill:max(pref; 100px)");
builder.append("First:", new JTextField());
builder.append("Last:", new JTextField());
builder.nextLine();
builder.append("Married:", new JCheckBox());
builder.nextLine();
builder.append("Phone:", new JTextField());
builder.nextLine();
builder.append("Fax:", new JTextField());
builder.nextLine();
builder.append("Email:", new JTextField());
builder.nextLine();
builder.appendSeparator("Work");
builder.append("Company:", new JTextField());
builder.nextLine();
builder.append("Phone:", new JTextField());
builder.nextLine();
builder.append("Fax:", new JTextField());
builder.nextLine();
add(builder.getPanel());
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Default Form Builder Example 1");
f.setDefaultCloseOperation(2);
f.add(new DefaultFormBuilderExample1());
f.pack();
f.setVisible(true);
}
}
FormLayout: Explicit Alignment Example 3
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import javax.swing.*;
public class FormLayoutExample3 extends JPanel {
public FormLayoutExample3() {
super(new BorderLayout());
FormDebugPanel panel = new FormDebugPanel();
FormLayout formLayout =
new FormLayout("100px, 100px, 100px, 100px", "100px, 100px, 100px, 100px");
panel.setLayout(formLayout);
CellConstraints c = new CellConstraints();
panel.add(createLabel("L/T", 50, 50), c.xy(1, 1, CellConstraints.LEFT, CellConstraints.TOP));
panel.add(createLabel("L/B", 50, 50), c.xy(1, 2, CellConstraints.LEFT, CellConstraints.BOTTOM));
panel.add(createLabel("L/C", 50, 50), c.xy(1, 3, CellConstraints.LEFT, CellConstraints.CENTER));
panel.add(createLabel("L/F", 50, 50), c.xy(1, 4, CellConstraints.LEFT, CellConstraints.FILL));
panel.add(createLabel("R/T", 50, 50), c.xy(2, 1, CellConstraints.RIGHT, CellConstraints.TOP));
panel.add(createLabel("R/B", 50, 50), c.xy(2, 2, CellConstraints.RIGHT, CellConstraints.BOTTOM));
panel.add(createLabel("R/C", 50, 50), c.xy(2, 3, CellConstraints.RIGHT, CellConstraints.CENTER));
panel.add(createLabel("R/F", 50, 50), c.xy(2, 4, CellConstraints.RIGHT, CellConstraints.FILL));
panel.add(createLabel("C/T", 50, 50), c.xy(3, 1, CellConstraints.CENTER, CellConstraints.TOP));
panel.add(createLabel("C/B", 50, 50), c.xy(3, 2, CellConstraints.CENTER, CellConstraints.BOTTOM));
panel.add(createLabel("C/C", 50, 50), c.xy(3, 3, CellConstraints.CENTER, CellConstraints.CENTER));
panel.add(createLabel("C/F", 50, 50), c.xy(3, 4, CellConstraints.CENTER, CellConstraints.FILL));
panel.add(createLabel("F/T", 50, 50), c.xy(4, 1, CellConstraints.FILL, CellConstraints.TOP));
panel.add(createLabel("F/B", 50, 50), c.xy(4, 2, CellConstraints.FILL, CellConstraints.BOTTOM));
panel.add(createLabel("F/C", 50, 50), c.xy(4, 3, CellConstraints.FILL, CellConstraints.CENTER));
panel.add(createLabel("F/F", 50, 50), c.xy(4, 4, CellConstraints.FILL, CellConstraints.FILL));
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Explicit Alignment Example 3");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample3());
f.pack();
f.setVisible(true);
}
}
FormLayout: Grouping Example 10
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class FormLayoutExample10 extends JPanel {
public FormLayoutExample10() {
super(new BorderLayout());
FormDebugPanel panel = new FormDebugPanel();
FormLayout formLayout =
new FormLayout("p, p, p, p", "p, p");
panel.setLayout(formLayout);
CellConstraints c = new CellConstraints();
panel.add(createLabel("C1:", 30, 30), c.xy(1, 1));
panel.add(createLabel("C2:", 30, 40), c.xy(2, 1));
panel.add(createLabel("C3:", 30, 50), c.xy(3, 1));
panel.add(createLabel("C4:", 30, 60), c.xy(4, 1));
panel.add(createLabel("C5:", 30, 30), c.xy(1, 2));
panel.add(createLabel("C6:", 40, 30), c.xy(2, 2));
panel.add(createLabel("C7:", 50, 30), c.xy(3, 2));
panel.add(createLabel("C8:", 60, 30), c.xy(4, 2));
formLayout.setRowGroups(new int[][]{{1, 2}});
formLayout.setColumnGroups(new int[][]{{1, 3}});
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Grouping Example 10");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample10());
f.pack();
f.setVisible(true);
}
}
FormLayout: Growable Example 8
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class FormLayoutExample8 extends JPanel {
public FormLayoutExample8() {
super(new BorderLayout());
FormDebugPanel panel = new FormDebugPanel();
FormLayout formLayout =
new FormLayout("fill:50px:grow(.25), fill:50px:grow, fill:50px:grow(.25)", "fill:50px");
panel.setLayout(formLayout);
CellConstraints c = new CellConstraints();
panel.add(createLabel("Comp1"), c.xy(1, 1));
panel.add(createLabel("Comp2"), c.xy(2, 1));
panel.add(createLabel("Comp3"), c.xy(3, 1));
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Growable Example 8");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample8());
f.pack();
f.setVisible(true);
}
}
FormLayout growing options: none, default, weighted
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates the FormLayout growing options: none, default, weighted.
*
* @author Karsten Lentzsch
* @version $Revision: 1.9 $
*/
public class GrowingExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Growing");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new GrowingExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add("All", buildHorizontalAllExtraSpacePanel());
tabbedPane.add("Half", buildHorizontalHalfAndHalfPanel());
tabbedPane.add("Percent", buildHorizontalPercentMixedPanel());
tabbedPane.add("Percent 2", buildHorizontalPercentPanel());
tabbedPane.add("Vertical 1", buildVerticalGrowing1Panel());
tabbedPane.add("Vertical 2", buildVerticalGrowing2Panel());
return tabbedPane;
}
private JComponent buildHorizontalAllExtraSpacePanel() {
FormLayout layout = new FormLayout(
"pref, 6px, pref:grow",
"pref, 12px, pref");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("Fixed"), cc.xy(1, 1));
panel.add(new JLabel("Gets all extra space"), cc.xy(3, 1));
panel.add(new JTextField(5), cc.xy(1, 3));
panel.add(new JTextField(5), cc.xy(3, 3));
return panel;
}
private JComponent buildHorizontalHalfAndHalfPanel() {
FormLayout layout = new FormLayout(
"pref, 6px, 0:grow, 6px, 0:grow",
"pref, 12px, pref");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("Fixed"), cc.xy(1, 1));
panel.add(new JLabel("Gets half of extra space"), cc.xy(3, 1));
panel.add(new JLabel("gets half of extra space"), cc.xy(5, 1));
panel.add(new JTextField(5), cc.xy(1, 3));
panel.add(new JTextField(5), cc.xy(3, 3));
panel.add(new JTextField(5), cc.xy(5, 3));
return panel;
}
private JComponent buildHorizontalPercentMixedPanel() {
FormLayout layout = new FormLayout(
"pref, 6px, 0:grow(0.25), 6px, 0:grow(0.75)",
"pref, 12px, pref");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("Fixed"), cc.xy(1, 1));
panel.add(new JLabel("Gets 25% of extra space"), cc.xy(3, 1));
panel.add(new JLabel("Gets 75% of extra space"), cc.xy(5, 1));
panel.add(new JTextField(5), cc.xy(1, 3));
panel.add(new JTextField(5), cc.xy(3, 3));
panel.add(new JTextField(5), cc.xy(5, 3));
return panel;
}
private JComponent buildHorizontalPercentPanel() {
FormLayout layout = new FormLayout(
"pref:grow(0.33), 6px, pref:grow(0.67)",
"pref, 12px, pref");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("Gets 33% of the space"), cc.xy(1, 1));
panel.add(new JLabel("Gets 67% of the space"), cc.xy(3, 1));
panel.add(new JTextField(5), cc.xy(1, 3));
panel.add(new JTextField(5), cc.xy(3, 3));
return panel;
}
private JComponent buildVerticalGrowing1Panel() {
FormLayout layout = new FormLayout(
"pref, 12px, pref",
"pref, 6px, fill:0:grow(0.25), 6px, fill:0:grow(0.75)");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("Fixed"), cc.xy(1, 1));
panel.add(new JLabel("Gets 25% of extra space"), cc.xy(1, 3));
panel.add(new JLabel("Gets 75% of extra space"), cc.xy(1, 5));
panel.add(createTextArea(4, 30), cc.xy(3, 1));
panel.add(createTextArea(4, 30), cc.xy(3, 3));
panel.add(createTextArea(4, 30), cc.xy(3, 5));
return panel;
}
private JComponent buildVerticalGrowing2Panel() {
FormLayout layout = new FormLayout(
"pref, 12px, pref",
"fill:0:grow(0.25), 6px, fill:0:grow(0.75)");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("Gets 25% of extra space"), cc.xy(1, 1));
panel.add(new JLabel("Gets 75% of extra space"), cc.xy(1, 3));
panel.add(createTextArea(4, 30), cc.xy(3, 1));
panel.add(createTextArea(4, 30), cc.xy(3, 3));
return panel;
}
// Component Creation *****************************************************
private JComponent createTextArea(int rows, int cols) {
return new JScrollPane(new JTextArea(rows, cols),
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
}
}
FormLayout: No Grouping Example 9
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class FormLayoutExample9 extends JPanel {
public FormLayoutExample9() {
super(new BorderLayout());
FormDebugPanel panel = new FormDebugPanel();
FormLayout formLayout =
new FormLayout("p, p, p, p", "p, p");
panel.setLayout(formLayout);
CellConstraints c = new CellConstraints();
panel.add(createLabel("C1:", 30, 30), c.xy(1, 1));
panel.add(createLabel("C2:", 30, 40), c.xy(2, 1));
panel.add(createLabel("C3:", 30, 50), c.xy(3, 1));
panel.add(createLabel("C4:", 30, 60), c.xy(4, 1));
panel.add(createLabel("C5:", 30, 30), c.xy(1, 2));
panel.add(createLabel("C6:", 40, 30), c.xy(2, 2));
panel.add(createLabel("C7:", 50, 30), c.xy(3, 2));
panel.add(createLabel("C8:", 60, 30), c.xy(4, 2));
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: No Grouping Example 9");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample9());
f.pack();
f.setVisible(true);
}
}
FormLayout: Size Specification Example 4
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import javax.swing.*;
public class FormLayoutExample4 extends JPanel {
public FormLayoutExample4() {
super(new BorderLayout());
JPanel panel = new FormDebugPanel();
FormLayout formLayout = new FormLayout("35px, 40pt, 35dlu, 1.0in, 3.5cm, 40.0mm", "min, min, min, min, min, min");
panel.setLayout(formLayout);
CellConstraints c = new CellConstraints();
panel.add(createLabel("30px"), c.xy(1, 1));
panel.add(createLabel("40pt"), c.xy(2, 2));
panel.add(createLabel("35dlu"), c.xy(3, 3));
panel.add(createLabel("1.0in"), c.xy(4, 4));
panel.add(createLabel("3.5cm"), c.xy(5, 5));
panel.add(createLabel("40.0mm"), c.xy(6, 6));
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Size Specification Example 4");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample4());
f.pack();
f.setVisible(true);
}
}
FormLayout: Spacing Example 5
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import javax.swing.*;
public class FormLayoutExample5 extends JPanel {
public FormLayoutExample5() {
super(new BorderLayout());
JPanel panel = new FormDebugPanel();
FormLayout formLayout = new FormLayout("pref, 10dlu, 100dlu", "default, default, default");
panel.setLayout(formLayout);
CellConstraints c = new CellConstraints();
panel.add(createLabel("Small 1"), c.xy(1, 1));
panel.add(createLabel("Larger Label"), c.xy(1, 2));
panel.add(createLabel("Med. Label"), c.xy(1, 3));
panel.add(new JTextField(), c.xy(3, 1));
panel.add(new JTextField(), c.xy(3, 2));
panel.add(new JTextField(), c.xy(3, 3));
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Spacing Example 5");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample5());
f.pack();
f.setVisible(true);
}
}
FormLayout: Spanning Example 7
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class FormLayoutExample7 extends JPanel {
public FormLayoutExample7() {
super(new BorderLayout());
FormDebugPanel panel = new FormDebugPanel();
FormLayout formLayout =
new FormLayout("fill:100px, fill:100px, fill:100px", "fill:100px, fill:100px");
panel.setLayout(formLayout);
CellConstraints c = new CellConstraints();
panel.add(createLabel("Comp1"), c.xywh(1, 1, 1, 2));
panel.add(createLabel("Comp2"), c.xywh(2, 1, 2, 1));
panel.add(createLabel("Comp3"), c.xy(2, 2));
panel.add(createLabel("Comp4"), c.xy(3, 2));
add(panel);
}
public JLabel createLabel(String text, int prefWidth, int prefHeight) {
JLabel label = createLabel(text);
label.setPreferredSize(new Dimension(prefWidth, prefHeight));
return label;
}
public JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setBackground(Color.lightGray);
label.setBorder(BorderFactory.createLineBorder(Color.black));
label.setOpaque(true);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
return label;
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Spanning Example 7");
f.setDefaultCloseOperation(2);
f.add(new FormLayoutExample7());
f.pack();
f.setVisible(true);
}
}
How columns and rows can be grouped in FormLayout
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates how columns and rows can be grouped in FormLayout.
*
* @author Karsten Lentzsch
* @version $Revision: 1.10 $
*/
public class GroupingExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Grouping");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new GroupingExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add("Ungrouped Bar", buildWizardBar(false));
tabbedPane.add("Grouped Bar", buildWizardBar(true));
tabbedPane.add("Ungrouped Rows", buildEditorPanel(false));
tabbedPane.add("Grouped Rows", buildEditorPanel(true));
return tabbedPane;
}
private JComponent buildWizardBar(boolean grouped) {
FormLayout layout = new FormLayout(
"pref, 6px:grow, pref, pref, 12px, pref, 6px, pref",
"pref");
if (grouped) {
layout.setColumnGroups(new int[][]{{1, 3, 4, 6, 8}});
}
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(createNarrowButton("Hilfe"), cc.xy(1, 1));
panel.add(createNarrowButton("< Zur\u00FCck"), cc.xy(3, 1));
panel.add(createNarrowButton("Vor >"), cc.xy(4, 1));
panel.add(createNarrowButton("Beenden"), cc.xy(6, 1));
panel.add(createNarrowButton("Abbrechen"), cc.xy(8, 1));
return panel;
}
private JComponent buildEditorPanel(boolean grouped) {
FormLayout layout = new FormLayout(
"pref, 4dlu, 35dlu, 2dlu, 35dlu, 2dlu, 35dlu, 2dlu, 35dlu",
"p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p");
if (grouped) {
layout.setRowGroups(new int[][] { { 1, 3, 5, 7, 9, 11, 13, 15, 17 } });
}
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("File number:"), cc.xy (1, 1));
panel.add(new JTextField(), cc.xyw(3, 1, 7));
panel.add(new JLabel("BL/MBL number:"), cc.xy (1, 3));
panel.add(new JTextField(), cc.xy (3, 3));
panel.add(new JTextField(), cc.xy (5, 3));
panel.add(new JLabel("Entry date:"), cc.xy (1, 5));
panel.add(new JTextField(), cc.xy (3, 5));
panel.add(new JLabel("RFQ number:"), cc.xy (1, 7));
panel.add(new JTextField(), cc.xyw(3, 7, 7));
panel.add(new JLabel("Goods:"), cc.xy (1, 9));
panel.add(new JCheckBox("Dangerous"), cc.xyw(3, 9, 7));
panel.add(new JLabel("Shipper:"), cc.xy (1, 11));
panel.add(new JTextField(), cc.xyw(3, 11, 7));
panel.add(new JLabel("Customer:"), cc.xy (1, 13));
panel.add(new JTextField(), cc.xyw(3, 13, 5));
panel.add(new JButton("..."), cc.xy (9, 13));
panel.add(new JLabel("Port of loading:"), cc.xy (1, 15));
panel.add(new JTextField(), cc.xyw(3, 15, 7));
panel.add(new JLabel("Destination:"), cc.xy (1, 17));
panel.add(new JTextField(), cc.xyw(3, 17, 7));
return panel;
}
// Component Creation *****************************************************
private JButton createNarrowButton(String text) {
JButton button = new JButton(text);
button.putClientProperty("jgoodies.isNarrow", Boolean.TRUE);
return button;
}
}
How components can span multiple columns and rows
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.ruponent;
import javax.swing.*;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates how components can span multiple columns and rows.
*
* @author Karsten Lentzsch
* @version $Revision: 1.12 $
*/
public class SpanExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Span");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new SpanExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
/**
* Builds and returns a tabbed pane with tabs for the column span example
* and the row span example.
*
* @return a tabbed pane that shows horizontal and vertical spans
*/
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add("Column Span", buildColumnSpanExample());
tabbedPane.add("Row Span", buildRowSpanExample());
return tabbedPane;
}
/**
* Builds and returns a panel where a component spans multiple columns.
*
* @return a panel with a component that spans multiple columns
*/
private JComponent buildColumnSpanExample() {
FormLayout layout = new FormLayout(
"pref, 8px, 100px, 4px, 200px",
"pref, 6px, pref, 6px, pref, 6px, pref");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("Name:"), cc.xy (1, 1));
panel.add(new JTextField(), cc.xyw(3, 1, 3));
panel.add(new JLabel("Phone:"), cc.xy (1, 3));
panel.add(new JTextField(), cc.xyw(3, 3, 3));
panel.add(new JLabel("ZIP/City:"), cc.xy (1, 5));
panel.add(new JTextField(), cc.xy (3, 5));
panel.add(new JTextField(), cc.xy (5, 5));
panel.add(new JLabel("Country:"), cc.xy (1, 7));
panel.add(new JTextField(), cc.xyw(3, 7, 3));
return panel;
}
/**
* Builds and returns a panel where a component spans multiple rows.<p>
*
* This demo method is about layout. The default FocusTraversalPolicy
* will lead to a poor focus traversal order: name, notes, phone, fax;
* where the order should be: name, phone, fax, notes.
* The components are added in the same order as they shall be
* traversed by the focus.
* Hence, if you set a ContainerOrderFocusTraversalPolicy,
* the focus will traverse the fields in the appropriate order.
*
* @return a panel with a component that spans multiple rows
*/
private JComponent buildRowSpanExample() {
FormLayout layout = new FormLayout(
"200px, 25px, 200px",
"pref, 2px, pref, 9px, " +
"pref, 2px, pref, 9px, " +
"pref, 2px, pref");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
Component addressArea = new JScrollPane(new JTextArea());
panel.add(new JLabel("Name"), cc.xy (1, 1));
panel.add(new JTextField(), cc.xy (1, 3));
panel.add(new JLabel("Phone"), cc.xy (1, 5));
panel.add(new JTextField(), cc.xy (1, 7));
panel.add(new JLabel("Fax"), cc.xy (1, 9));
panel.add(new JTextField(), cc.xy (1, 11));
panel.add(new JLabel("Notes"), cc.xy (3, 1));
panel.add(addressArea, cc.xywh(3, 3, 1, 9));
return panel;
}
}
How FormLayout applies the default column and
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates how FormLayout applies the default column and row
* alignments to cells, and how to override the defaults.
*
* @author Karsten Lentzsch
* @version $Revision: 1.9 $
*/
public class CellAlignmentExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Cell Alignments");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new CellAlignmentExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add(buildHorizontalPanel(), "Horizontal");
tabbedPane.add(buildVerticalPanel(), "Vertical");
return tabbedPane;
}
private JComponent buildHorizontalPanel() {
FormLayout layout = new FormLayout(
"r:p, 4dlu, left:pref:g, center:pref:g, right:pref:g, pref:g",
"pref, 8dlu, pref, pref, pref, pref, pref");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
panel.add(new JLabel("Column Spec: "), "1, 1, r, c");
panel.add(new JLabel(" \"left:pref:grow\" "), "3, 1, c, c");
panel.add(new JLabel(" \"center:pref:grow\" "), "4, 1, c, c");
panel.add(new JLabel(" \"right:pref:grow\" "), "5, 1, c, c");
panel.add(new JLabel(" \"pref:grow\" "), "6, 1, c, c");
int row = 3;
addHorizontalButton(panel, 3, row, CellConstraints.DEFAULT);
addHorizontalButton(panel, 4, row, CellConstraints.DEFAULT);
addHorizontalButton(panel, 5, row, CellConstraints.DEFAULT);
addHorizontalButton(panel, 6, row, CellConstraints.DEFAULT);
row++;
addHorizontalButton(panel, 3, row, CellConstraints.FILL);
addHorizontalButton(panel, 4, row, CellConstraints.FILL);
addHorizontalButton(panel, 5, row, CellConstraints.FILL);
addHorizontalButton(panel, 6, row, CellConstraints.FILL);
row++;
addHorizontalButton(panel, 3, row, CellConstraints.LEFT);
addHorizontalButton(panel, 4, row, CellConstraints.LEFT);
addHorizontalButton(panel, 5, row, CellConstraints.LEFT);
addHorizontalButton(panel, 6, row, CellConstraints.LEFT);
row++;
addHorizontalButton(panel, 3, row, CellConstraints.CENTER);
addHorizontalButton(panel, 4, row, CellConstraints.CENTER);
addHorizontalButton(panel, 5, row, CellConstraints.CENTER);
addHorizontalButton(panel, 6, row, CellConstraints.CENTER);
row++;
addHorizontalButton(panel, 3, row, CellConstraints.RIGHT);
addHorizontalButton(panel, 4, row, CellConstraints.RIGHT);
addHorizontalButton(panel, 5, row, CellConstraints.RIGHT);
addHorizontalButton(panel, 6, row, CellConstraints.RIGHT);
return panel;
}
private JComponent buildVerticalPanel() {
FormLayout layout = new FormLayout(
"left:pref, 8dlu, p, c:p, p, p, p",
"p, 4dlu, top:pref:g, center:pref:g, bottom:pref:g, pref:g");
layout.setColumnGroups(new int[][] {{3, 5, 6, 7}});
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
panel.add(new JLabel("Row Spec:"), "1, 1, r, c");
panel.add(new JLabel("\"top:pref:grow\""), "1, 3, r, c");
panel.add(new JLabel("\"center:pref:grow\""), "1, 4, r, c");
panel.add(new JLabel("\"bottom:pref:grow\""), "1, 5, r, c");
panel.add(new JLabel("\"pref:grow\""), "1, 6, r, c");
int col = 3;
addVerticalButton(panel, col, 3, CellConstraints.DEFAULT);
addVerticalButton(panel, col, 4, CellConstraints.DEFAULT);
addVerticalButton(panel, col, 5, CellConstraints.DEFAULT);
addVerticalButton(panel, col, 6, CellConstraints.DEFAULT);
col++;
addVerticalButton(panel, col, 3, CellConstraints.FILL);
addVerticalButton(panel, col, 4, CellConstraints.FILL);
addVerticalButton(panel, col, 5, CellConstraints.FILL);
addVerticalButton(panel, col, 6, CellConstraints.FILL);
col++;
addVerticalButton(panel, col, 3, CellConstraints.TOP);
addVerticalButton(panel, col, 4, CellConstraints.TOP);
addVerticalButton(panel, col, 5, CellConstraints.TOP);
addVerticalButton(panel, col, 6, CellConstraints.TOP);
col++;
addVerticalButton(panel, col, 3, CellConstraints.CENTER);
addVerticalButton(panel, col, 4, CellConstraints.CENTER);
addVerticalButton(panel, col, 5, CellConstraints.CENTER);
addVerticalButton(panel, col, 6, CellConstraints.CENTER);
col++;
addVerticalButton(panel, col, 3, CellConstraints.BOTTOM);
addVerticalButton(panel, col, 4, CellConstraints.BOTTOM);
addVerticalButton(panel, col, 5, CellConstraints.BOTTOM);
addVerticalButton(panel, col, 6, CellConstraints.BOTTOM);
return panel;
}
private void addHorizontalButton(JPanel panel, int col, int row,
CellConstraints.Alignment hAlignment) {
JButton button = new JButton(hAlignment.toString());
panel.add(button, new CellConstraints(col, row,
hAlignment,
CellConstraints.DEFAULT));
}
private void addVerticalButton(JPanel panel, int col, int row,
CellConstraints.Alignment vAlignment) {
JButton button = new JButton(vAlignment.toString());
panel.add(button, new CellConstraints(col, row,
CellConstraints.DEFAULT,
vAlignment));
}
}
Panel Builder Example 1
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class PanelBuilderExample1 extends JPanel {
public PanelBuilderExample1() {
super(new BorderLayout());
PanelBuilder builder = new PanelBuilder(new FormLayout(""));
builder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
builder.appendColumn("right:pref");
builder.appendColumn("fill:max(pref; 100px)");
CellConstraints c = new CellConstraints();
builder.appendRow("top:pref");
builder.addLabel("First:", c.xy(1, 1));
builder.add(new JTextField(), c.xy(2, 1));
builder.appendRow("top:pref");
builder.addLabel("Last:", c.xy(1, 2));
builder.add(new JTextField(), c.xy(2, 2));
builder.appendRow("top:pref");
builder.addLabel("Married:", c.xy(1, 3));
builder.add(new JCheckBox(), c.xy(2, 3));
builder.appendRow("top:pref");
builder.addLabel("Phone:", c.xy(1, 4));
builder.add(new JTextField(), c.xy(2, 4));
builder.appendRow("top:pref");
builder.addLabel("Fax:", c.xy(1, 5));
builder.add(new JTextField(), c.xy(2, 5));
builder.appendRow("top:pref");
builder.addLabel("Email:", c.xy(1, 6));
builder.add(new JTextField(), c.xy(2, 6));
builder.appendRow("top:pref");
builder.addSeparator("Work", c.xywh(1, 7, 2, 1));
builder.appendRow("top:pref");
builder.addLabel("Company:", c.xy(1, 8));
builder.add(new JTextField(), c.xy(2, 8));
builder.appendRow("top:pref");
builder.addLabel("Phone:", c.xy(1, 9));
builder.add(new JTextField(), c.xy(2, 9));
builder.appendRow("top:pref");
builder.addLabel("Fax:", c.xy(1, 10));
builder.add(new JTextField(), c.xy(2, 10));
add(builder.getPanel());
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Panel Builder Example 1");
f.setDefaultCloseOperation(2);
f.add(new PanelBuilderExample1());
f.pack();
f.setVisible(true);
}
}
Panel Builder Example 2
/*
Code revised from Desktop Java Live:
http://www.sourcebeat.ru/downloads/
*/
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.FormLayout;
public class PanelBuilderExample2 extends JPanel {
public PanelBuilderExample2() {
super(new BorderLayout());
PanelBuilder builder = new PanelBuilder(new FormLayout(""));
builder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
builder.appendColumn("right:pref");
builder.appendColumn("fill:max(pref; 100px)");
builder.appendRow("top:pref");
builder.addLabel("First:");
builder.nextColumn();
builder.add(new JTextField());
builder.appendRow("top:pref");
builder.nextLine();
builder.addLabel("Last:");
builder.nextColumn();
builder.add(new JTextField());
builder.appendRow("top:pref");
builder.nextLine();
builder.addLabel("Married:");
builder.nextColumn();
builder.add(new JCheckBox());
builder.appendRow("top:pref");
builder.nextLine();
builder.addLabel("Phone:");
builder.nextColumn();
builder.add(new JTextField());
builder.appendRow("top:pref");
builder.nextLine();
builder.addLabel("Fax:");
builder.nextColumn();
builder.add(new JTextField());
builder.appendRow("top:pref");
builder.nextLine();
builder.addLabel("Email:");
builder.nextColumn();
builder.add(new JTextField());
builder.appendRow("top:pref");
builder.nextLine();
builder.addSeparator("Work");
builder.appendRow("top:pref");
builder.nextLine();
builder.addLabel("Company:");
builder.nextColumn();
builder.add(new JTextField());
builder.appendRow("top:pref");
builder.nextLine();
builder.addLabel("Phone:");
builder.nextColumn();
builder.add(new JTextField());
builder.appendRow("top:pref");
builder.nextLine();
builder.addLabel("Fax:");
builder.nextColumn();
builder.add(new JTextField());
add(builder.getPanel());
}
public static void main(String[] a){
JFrame f = new JFrame("FormLayout: Panel Builder Example 2");
f.setDefaultCloseOperation(2);
f.add(new PanelBuilderExample2());
f.pack();
f.setVisible(true);
}
}
Shows three approaches how to add custom rows to a form that is built using a DefaultFormBuilder
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
/**
* Shows three approaches how to add custom rows to a form that is built
* using a DefaultFormBuilder.<ol>
* <li> single custom row,
* <li> standard + custom row,
* <li> multiple standard rows.
* </ol>
* These differ in the position of the "Comment" label, the alignment
* of font baselines and the height of the custom row.
*
* @author Karsten Lentzsch
* @version $Revision: 1.7 $
*
* @see DefaultFormBuilder
* @see DefaultFormWithCustomAreasExample
*/
public class DefaultFormWithCustomRowsExample {
private JTextField name1Field;
private JTextArea comment1Area;
private JTextField name2Field;
private JTextArea comment2Area;
private JTextField name3Field;
private JTextArea comment3Area;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Custom Rows");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new DefaultFormWithCustomRowsExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
// Component Creation and Initialization **********************************
/**
* Creates and intializes the UI components.
*/
private void initComponents() {
name1Field = new JTextField("Name - font baselines shall be aligned");
name2Field = new JTextField("Name - font baselines shall be aligned");
name3Field = new JTextField("Name - font baselines shall be aligned");
comment1Area = new JTextArea(2, 20);
comment2Area = new JTextArea(2, 20);
comment3Area = new JTextArea(2, 20);
comment1Area.setText("Comment - likely baselines are unaligned");
comment2Area.setText("Comment - baselines shall be aligned");
comment3Area.setText("Comment - baselines shall be aligned");
}
// Building ***************************************************************
/**
* Demonstrates three different ways to add custom rows to a form
* that is build with a {@link DefaultFormBuilder}.
*
* @return the built panel
*/
public JComponent buildPanel() {
initComponents();
FormLayout layout = new FormLayout(
"right:pref, 3dlu, min:grow",
"");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();
builder.setRowGroupingEnabled(true);
CellConstraints cc = new CellConstraints();
// In this approach, we add a gap and a custom row.
// The advantage of this approach is, that we can express
// the row spec and comment area cell constraints freely.
// The disadvantage is the misalignment of the leading label.
// Also the row"s height may be inconsistent with other rows.
builder.appendSeparator("Single Custom Row");
builder.append("Name", name1Field);
builder.appendRow(builder.getLineGapSpec());
builder.appendRow(new RowSpec("top:31dlu")); // Assumes line is 14, gap is 3
builder.nextLine(2);
builder.append("Comment");
builder.add(new JScrollPane(comment1Area),
cc.xy(builder.getColumn(), builder.getRow(), "fill, fill"));
builder.nextLine();
// In this approach, we append a standard row with gap before it.
// The advantage is, that the leading label is aligned well.
// The disadvantage is that the comment area now spans
// multiple cells and is slightly less flexible.
// Also the row"s height may be inconsistent with other rows.
builder.appendSeparator("Standard + Custom Row");
builder.append("Name", name2Field);
builder.append("Comment");
builder.appendRow(new RowSpec("17dlu")); // Assumes line is 14, gap is 3
builder.add(new JScrollPane(comment2Area),
cc.xywh(builder.getColumn(), builder.getRow(), 1, 2));
builder.nextLine(2);
// In this approach, we append two standard rows with associated gaps.
// The advantage is, that the leading label is aligned well,
// and the height is consistent with other rows.
// The disadvantage is that the comment area now spans
// multiple cells and is slightly less flexible.
builder.appendSeparator("Two Standard Rows");
builder.append("Name", name3Field);
builder.append("Comment");
builder.nextLine();
builder.append("");
builder.nextRow(-2);
builder.add(new JScrollPane(comment3Area),
cc.xywh(builder.getColumn(), builder.getRow(), 1, 3));
return builder.getPanel();
}
}
The different sizing units provided by the FormLayout
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates the different sizing units provided by the FormLayout:
* Pixel, Dialog Units (dlu), Point, Millimeter, Centimeter and Inches.
* Pt, mm, cm, in honor the screen resolution; dlus honor the font size too.
*
* @author Karsten Lentzsch
* @version $Revision: 1.9 $
*/
public class UnitsExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Units");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new UnitsExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add("Horizontal", buildHorizontalUnitsPanel());
tabbedPane.add("Horizontal Dlu", buildHorizontalDluPanel());
tabbedPane.add("Vertical", buildVerticalUnitsPanel());
tabbedPane.add("Vertical Dlu", buildVerticalDluPanel());
return tabbedPane;
}
private JComponent buildHorizontalUnitsPanel() {
FormLayout layout = new FormLayout(
"right:pref, 1dlu, left:pref, 4dlu, left:pref",
"");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();
builder.append("72", new JLabel("pt"), buildHorizontalPanel("72pt"));
builder.append("25.4", new JLabel("mm"), buildHorizontalPanel("25.4mm"));
builder.append("2.54", new JLabel("cm"), buildHorizontalPanel("2.54cm"));
builder.append("1", new JLabel("in"), buildHorizontalPanel("1in"));
builder.append("72", new JLabel("px"), buildHorizontalPanel("72px"));
builder.append("96", new JLabel("px"), buildHorizontalPanel("96px"));
builder.append("120", new JLabel("px"), buildHorizontalPanel("120px"));
return builder.getPanel();
}
private JComponent buildHorizontalDluPanel() {
FormLayout layout = new FormLayout(
"right:pref, 1dlu, left:pref, 4dlu, left:pref",
"");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();
builder.append("9", new JLabel("cols"), buildHorizontalPanel(9));
builder.append("50", new JLabel("dlu"), buildHorizontalPanel("50dlu"));
builder.append("75", new JLabel("px"), buildHorizontalPanel("75px"));
builder.append("88", new JLabel("px"), buildHorizontalPanel("88px"));
builder.append("100",new JLabel("px"), buildHorizontalPanel("100px"));
return builder.getPanel();
}
private JComponent buildVerticalUnitsPanel() {
FormLayout layout = new FormLayout(
"c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p",
"pref, 6dlu, top:pref");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("72 pt"), cc.xy(1, 1));
panel.add(buildVerticalPanel("72pt"), cc.xy(1, 3));
panel.add(new JLabel("25.4 mm"), cc.xy(3, 1));
panel.add(buildVerticalPanel("25.4mm"), cc.xy(3, 3));
panel.add(new JLabel("2.54 cm"), cc.xy(5, 1));
panel.add(buildVerticalPanel("2.54cm"), cc.xy(5, 3));
panel.add(new JLabel("1 in"), cc.xy(7, 1));
panel.add(buildVerticalPanel("1in"), cc.xy(7, 3));
panel.add(new JLabel("72 px"), cc.xy(9, 1));
panel.add(buildVerticalPanel("72px"), cc.xy(9, 3));
panel.add(new JLabel("96 px"), cc.xy(11, 1));
panel.add(buildVerticalPanel("96px"), cc.xy(11, 3));
panel.add(new JLabel("120 px"), cc.xy(13, 1));
panel.add(buildVerticalPanel("120px"), cc.xy(13, 3));
return panel;
}
private JComponent buildVerticalDluPanel() {
FormLayout layout = new FormLayout(
"c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p",
"pref, 6dlu, top:pref, 25dlu, pref, 6dlu, top:pref");
JPanel panel = new JPanel(layout);
panel.setBorder(Borders.DIALOG_BORDER);
CellConstraints cc = new CellConstraints();
panel.add(new JLabel("4 rows"), cc.xy(1, 1));
panel.add(buildVerticalPanel("pref", 4), cc.xy(1, 3));
panel.add(new JLabel("42 dlu"), cc.xy(3, 1));
panel.add(buildVerticalPanel("42dlu", 4), cc.xy(3, 3));
panel.add(new JLabel("57 px"), cc.xy(5, 1));
panel.add(buildVerticalPanel("57px", 4), cc.xy(5, 3));
panel.add(new JLabel("63 px"), cc.xy(7, 1));
panel.add(buildVerticalPanel("63px", 4), cc.xy(7, 3));
panel.add(new JLabel("68 px"), cc.xy(9, 1));
panel.add(buildVerticalPanel("68px", 4), cc.xy(9, 3));
panel.add(new JLabel("field"), cc.xy(1, 5));
panel.add(new JTextField(4), cc.xy(1, 7));
panel.add(new JLabel("14 dlu"), cc.xy(3, 5));
panel.add(buildVerticalPanel("14dlu"), cc.xy(3, 7));
panel.add(new JLabel("21 px"), cc.xy(5, 5));
panel.add(buildVerticalPanel("21px"), cc.xy(5, 7));
panel.add(new JLabel("23 px"), cc.xy(7, 5));
panel.add(buildVerticalPanel("23px"), cc.xy(7, 7));
panel.add(new JLabel("24 px"), cc.xy(9, 5));
panel.add(buildVerticalPanel("24px"), cc.xy(9, 7));
panel.add(new JLabel("button"), cc.xy(11, 5));
panel.add(new JButton("..."), cc.xy(11, 7));
return panel;
}
// Component Creation *****************************************************
private JComponent createTextArea(int rows, int cols) {
JTextArea area = new JTextArea(rows, cols);
//area.setText(text);
return new JScrollPane(area,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
}
// Helper Code ************************************************************
private JComponent buildHorizontalPanel(String width) {
FormLayout layout = new FormLayout(width, "pref");
JPanel panel = new JPanel(layout);
panel.add(new JTextField(), new CellConstraints(1, 1));
return panel;
}
private JComponent buildHorizontalPanel(int columns) {
FormLayout layout = new FormLayout("pref, 4dlu, pref", "pref");
JPanel panel = new JPanel(layout);
CellConstraints cc = new CellConstraints();
panel.add(new JTextField(columns),
cc.xy(1, 1));
panel.add(new JLabel("Width of new JTextField(" + columns + ")"),
cc.xy(3, 1));
return panel;
}
private JComponent buildVerticalPanel(String height) {
return buildVerticalPanel(height, 10);
}
private JComponent buildVerticalPanel(String height, int rows) {
FormLayout layout = new FormLayout("pref", "fill:"+ height);
JPanel panel = new JPanel(layout);
CellConstraints cc = new CellConstraints();
panel.add(createTextArea(rows, 5), cc.xy(1, 1));
return panel;
}
}
The use of Factories as provided by the Forms framework
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.ruponent;
import javax.swing.*;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.Sizes;
/**
* Demonstrates the use of Factories as provided by the Forms framework.
*
* @author Karsten Lentzsch
* @version $Revision: 1.9 $
*
* @see com.jgoodies.forms.factories.ButtonBarFactory
*/
public class FormFactoryExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: FormFactory");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new FormFactoryExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
tabbedPane.add(buildDefaultForm(1, 1), "1 - 1");
tabbedPane.add(buildDefaultForm(1, 2), "1 - 2");
tabbedPane.add(buildDefaultForm(1, 3), "1 - 3");
tabbedPane.add(buildDefaultForm(2, 1), "2 - 1");
tabbedPane.add(buildDefaultForm(2, 2), "2 - 2");
tabbedPane.add(buildDefaultForm(3, 1), "3 - 1");
return tabbedPane;
}
private Component buildDefaultForm(int majorCols, int minorCols) {
FormLayout layout =
FormFactory.createColumnLayout(
majorCols,
minorCols,
new ColumnSpec("right:p"),
Sizes.DLUX9,
Sizes.DLUX1);
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();
builder.setLeadingColumnOffset(1);
buildParagraph(builder, 4, majorCols, minorCols, "Propeller Shaft");
buildParagraph(builder, 3, majorCols, minorCols, "Intermediate Shaft");
return builder.getContainer();
}
private void buildParagraph(
DefaultFormBuilder builder,
int rows,
int majorCols,
int minorCols,
String text) {
builder.appendSeparator(text);
for (int row = 0; row < rows; row++) {
buildRow(builder, majorCols, minorCols);
}
}
private void buildRow(
DefaultFormBuilder builder,
int majorCols,
int minorCols) {
int charCols = 50 / (majorCols * (1 + minorCols));
for (int majorCol = 0; majorCol < majorCols; majorCol++) {
buildSection(builder, minorCols, charCols);
}
builder.nextLine();
}
private void buildSection(DefaultFormBuilder builder, int minorCols, int charCols) {
builder.append(nextLabel(), new JTextField(charCols));
for (int minorCol = 1; minorCol < minorCols; minorCol++) {
builder.append(new JTextField(charCols));
}
}
// Helper Code ************************************************************
private static int nextInt = 0;
private String nextLabel() {
if (nextInt++ == LABELS.length - 1)
nextInt = 0;
return LABELS[nextInt];
}
private static final String[] LABELS = {
"da [mm]", "ds [mm]", "kl [cm]", "Weight [Kg]", "Size [mm]",
"da2 [mm]", "ds2 [mm]", "cv [cm]", "pl [cm]", "mt [mm]",
"ep [mm]", "cvn [mm]", "nz [cm]", "Power [kW]", "Length [cm]",
"R [cm]", "D [mm]", "PTI [kW]"
};
}
Three FormLayout component sizes: minimum, default and preferred
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.Insets;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* Demonstrates the three FormLayout component sizes: minimum, default and
* preferred.
* Min and Pref measure the components minimum and preferred size, where the
* Default size behaves like Pref but shrinks if the container space is scarce.
*
* @author Karsten Lentzsch
* @version $Revision: 1.8 $
*/
public class ComponentSizesExample {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Component Sizes");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new ComponentSizesExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public JComponent buildPanel() {
JSplitPane splitPane = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
true,
buildCombinedPanel(),
buildTextPanel());
return splitPane;
}
/**
* Builds and answer the panel that combines the three sizing panels.
*
* @return the combined panel
*/
private JComponent buildCombinedPanel() {
FormLayout layout = new FormLayout(
"30dlu:grow",
"pref, 3dlu, pref, 3dlu, pref");
PanelBuilder builder = new PanelBuilder(layout);
builder.setDefaultDialogBorder();
CellConstraints cc = new CellConstraints();
builder.add(buildMinimumSizePanel(), cc.xy(1, 1));
builder.add(buildDefaultSizePanel(), cc.xy(1, 3));
builder.add(buildPreferredSizePanel(), cc.xy(1, 5));
return builder.getPanel();
}
private JComponent buildMinimumSizePanel() {
FormLayout layout = new FormLayout(
"right:max(25dlu;pref), 3dlu, min",
"pref");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.append("Min", new JTextField(15));
return builder.getPanel();
}
private JComponent buildDefaultSizePanel() {
FormLayout layout = new FormLayout(
"right:max(25dlu;pref), 3dlu, default",
"pref");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.append("Default", new JTextField(15));
return builder.getPanel();
}
private JComponent buildPreferredSizePanel() {
FormLayout layout = new FormLayout(
"right:max(25dlu;pref), 3dlu, pref",
"pref");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.append("Pref", new JTextField(15));
return builder.getPanel();
}
private JComponent buildTextPanel() {
JTextArea textArea = new JTextArea(5, 20);
textArea.setEditable(false);
textArea.setMargin(new Insets(6, 10, 4, 6));
textArea.setText("The text field used in the example on the left\n" +
"has a narrow minimum width and a wider preferred width.\n\n" +
"If you move the split divider to the left and right\n" +
"you can see how "Default" shrinks the field if space is scarce.\n\n" +
"If there"s not enough space for the preferred width\n" +
"the bottom field will be "cut" on the right-hand side.");
JScrollPane scrollpane = new JScrollPane(textArea);
scrollpane.setBorder(new EmptyBorder(0, 0, 0, 0));
return scrollpane;
}
}
Uses the FormLayout and the DefaultFormBuilder
/*
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.swing.*;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
/**
* Uses the FormLayout and the <code>DefaultFormBuilder</code>.
* Columns are specified before the panel is filled with components,
* rows are added dynamically. The builder is used to hold a cursor,
* to add rows dynamically, and to fill components.
* The builder"s convenience methods are used to add labels and separators.<p>
*
* This panel building style is recommended unless you have a more
* powerful builder or don"t want to add rows dynamically.
* See the {@link DynamicRowsExample} for an implementation that specifies
* rows before the panel is filled with components.
*
* @author Karsten Lentzsch
* @version $Revision: 1.8 $
*
* @see DefaultFormBuilder
* @see PlainExample
* @see RowCounterExample
* @see DynamicRowsExample
*/
public class DefaultFormBuilderExample {
private JTextField identifierField;
private JTextField ptiField;
private JTextField powerField;
private JTextField sField;
private JTextField daField;
private JTextField diField;
private JTextField da2Field;
private JTextField di2Field;
private JTextField rField;
private JTextField dField;
private JComboBox locationCombo;
private JTextField kFactorField;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
} catch (Exception e) {
// Likely PlasticXP is not in the class path; ignore.
}
JFrame frame = new JFrame();
frame.setTitle("Forms Tutorial :: Default Form");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JComponent panel = new DefaultFormBuilderExample().buildPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
// Component Creation and Initialization **********************************
/**
* Creates and intializes the UI components.
*/
private void initComponents() {
identifierField = new JTextField();
ptiField = new JTextField();
powerField = new JTextField();
sField = new JTextField();
daField = new JTextField();
diField = new JTextField();
da2Field = new JTextField();
di2Field = new JTextField();
rField = new JTextField();
dField = new JTextField();
locationCombo = createLocationComboBox();
kFactorField = new JTextField();
}
/**
* Creates and returns a combo box for the locations.
*
* @return a combo box for three locations
*/
private JComboBox createLocationComboBox() {
return new JComboBox(
new String[] {
"Propeller nut thread",
"Stern tube front area",
"Shaft taper" });
}
// Building ***************************************************************
/**
* Builds the flange editor panel.
* Columns are specified before components are added to the form,
* rows are added dynamically using the {@link DefaultFormBuilder}.<p>
*
* The builder combines a step that is done again and again:
* add a label, proceed to the next data column and add a component.
*
* @return the built panel
*/
public JComponent buildPanel() {
initComponents();
FormLayout layout = new FormLayout(
"right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
+ "right:max(40dlu;pref), 3dlu, 70dlu",
"");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();
builder.appendSeparator("Flange");
builder.append("&Identifier", identifierField);
builder.nextLine();
builder.append("PTI [kW]", ptiField);
builder.append("Power [kW]", powerField);
builder.append("s [mm]", sField);
builder.nextLine();
builder.appendSeparator("Diameters");
builder.append("&da [mm]", daField);
builder.append("di [mm]", diField);
builder.append("da2 [mm]", da2Field);
builder.append("di2 [mm]", di2Field);
builder.append("R [mm]", rField);
builder.append("D [mm]", dField);
builder.appendSeparator("Criteria");
builder.append("&Location", locationCombo);
builder.append("k-factor", kFactorField);
return builder.getPanel();
}
}