001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.tutorial.building;
032:
033: import javax.swing.JComboBox;
034: import javax.swing.JComponent;
035: import javax.swing.JFrame;
036: import javax.swing.JTextField;
037: import javax.swing.UIManager;
038: import javax.swing.WindowConstants;
039:
040: import com.jgoodies.forms.builder.PanelBuilder;
041: import com.jgoodies.forms.layout.CellConstraints;
042: import com.jgoodies.forms.layout.FormLayout;
043:
044: /**
045: * Demonstrates a typical use of the FormLayout.
046: * Columns and rows are specified before the panel is filled with
047: * components, and the panel is filled with a PanelBuilder.<p>
048: *
049: * Unlike the PlainExample, this implementation can delegate
050: * the component creation for text labels and titled separators
051: * to the builder.<p>
052: *
053: * This panel building style is recommended for panels with
054: * a medium number of rows and components. If the panel has more rows,
055: * you may consider using a row variable to address the current row.
056: *
057: * @author Karsten Lentzsch
058: * @version $Revision: 1.13 $
059: *
060: * @see PanelBuilder
061: * @see RowCounterExample
062: * @see DynamicRowsExample
063: * @see DefaultFormBuilderExample
064: */
065:
066: public final class PanelBuilderExample {
067:
068: private JTextField companyNameField;
069: private JTextField contactPersonField;
070: private JTextField orderNoField;
071: private JTextField inspectorField;
072: private JTextField referenceNoField;
073: private JComboBox approvalStatusComboBox;
074: private JTextField shipYardField;
075: private JTextField registerNoField;
076: private JTextField hullNumbersField;
077: private JComboBox projectTypeComboBox;
078:
079: public static void main(String[] args) {
080: try {
081: UIManager
082: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
083: } catch (Exception e) {
084: // Likely PlasticXP is not in the class path; ignore.
085: }
086: JFrame frame = new JFrame();
087: frame.setTitle("Forms Tutorial :: PanelBuilder");
088: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
089: JComponent panel = new PanelBuilderExample().buildPanel();
090: frame.getContentPane().add(panel);
091: frame.pack();
092: frame.setVisible(true);
093: }
094:
095: // Component Creation and Initialization **********************************
096:
097: /**
098: * Creates and intializes the UI components.
099: */
100: private void initComponents() {
101: companyNameField = new JTextField();
102: contactPersonField = new JTextField();
103: orderNoField = new JTextField();
104: inspectorField = new JTextField();
105: referenceNoField = new JTextField();
106: approvalStatusComboBox = createApprovalStatusComboBox();
107: shipYardField = new JTextField();
108: registerNoField = new JTextField();
109: hullNumbersField = new JTextField();
110: projectTypeComboBox = createProjectTypeComboBox();
111: }
112:
113: /**
114: * Creates and returns a combo box for the approval states.
115: *
116: * @return a combo box for the approval status
117: */
118: private JComboBox createApprovalStatusComboBox() {
119: return new JComboBox(new String[] { "In Progress", "Finished",
120: "Released" });
121: }
122:
123: /**
124: * Creates and returns a combo box for the project types.
125: *
126: * @return a combo box for the project type
127: */
128: private JComboBox createProjectTypeComboBox() {
129: return new JComboBox(new String[] { "New Building",
130: "Conversion", "Repair" });
131: }
132:
133: // Building *************************************************************
134:
135: /**
136: * Builds the pane.
137: *
138: * @return the built panel
139: */
140: public JComponent buildPanel() {
141: initComponents();
142:
143: FormLayout layout = new FormLayout(
144: "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
145: + "right:max(40dlu;pref), 3dlu, 70dlu",
146: "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, "
147: + "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, "
148: + "p, 3dlu, p, 3dlu, p, 3dlu, p");
149:
150: PanelBuilder builder = new PanelBuilder(layout);
151: builder.setDefaultDialogBorder();
152:
153: // Fill the table with labels and components.
154: CellConstraints cc = new CellConstraints();
155: builder.addSeparator("Manufacturer", cc.xyw(1, 1, 7));
156: builder.addLabel("Company", cc.xy(1, 3));
157: builder.add(companyNameField, cc.xyw(3, 3, 5));
158: builder.addLabel("Contact", cc.xy(1, 5));
159: builder.add(contactPersonField, cc.xyw(3, 5, 5));
160: builder.addLabel("Order No", cc.xy(1, 7));
161: builder.add(orderNoField, cc.xy(3, 7));
162:
163: builder.addSeparator("Inspector", cc.xyw(1, 9, 7));
164: builder.addLabel("Name", cc.xy(1, 11));
165: builder.add(inspectorField, cc.xyw(3, 11, 5));
166: builder.addLabel("Reference No", cc.xy(1, 13));
167: builder.add(referenceNoField, cc.xy(3, 13));
168: builder.addLabel("Status", cc.xy(1, 15));
169: builder.add(approvalStatusComboBox, cc.xy(3, 15));
170:
171: builder.addSeparator("Ship", cc.xyw(1, 17, 7));
172: builder.addLabel("Shipyard", cc.xy(1, 19));
173: builder.add(shipYardField, cc.xyw(3, 19, 5));
174: builder.addLabel("Register No", cc.xy(1, 21));
175: builder.add(registerNoField, cc.xy(3, 21));
176: builder.addLabel("Hull No", cc.xy(5, 21));
177: builder.add(hullNumbersField, cc.xy(7, 21));
178: builder.addLabel("Project Type", cc.xy(1, 23));
179: builder.add(projectTypeComboBox, cc.xy(3, 23));
180:
181: return builder.getPanel();
182: }
183:
184: }
|