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 java.awt.Component;
034:
035: import javax.swing.*;
036:
037: import com.jgoodies.forms.factories.Borders;
038: import com.jgoodies.forms.factories.DefaultComponentFactory;
039: import com.jgoodies.forms.layout.CellConstraints;
040: import com.jgoodies.forms.layout.FormLayout;
041:
042: /**
043: * Demonstrates a <em>pure</em> use of the FormLayout.
044: * Columns and rows are specified before the panel is filled with
045: * components. And the panel is filled without a builder.<p>
046: *
047: * This panel building style is simple but not recommended. Other panel
048: * building styles use a builder to fill the panel and/or create
049: * form rows dynamically. See the {@link PanelBuilderExample} for
050: * a slightly better panel building style that can use the builder
051: * to create text labels and separators.
052: *
053: * @author Karsten Lentzsch
054: * @version $Revision: 1.13 $
055: *
056: * @see PanelBuilderExample
057: * @see RowCounterExample
058: * @see DynamicRowsExample
059: * @see DefaultFormBuilderExample
060: */
061:
062: public final class PlainExample {
063:
064: private JTextField companyNameField;
065: private JTextField contactPersonField;
066: private JTextField orderNoField;
067: private JTextField inspectorField;
068: private JTextField referenceNoField;
069: private JComboBox approvalStatusComboBox;
070: private JTextField shipYardField;
071: private JTextField registerNoField;
072: private JTextField hullNumbersField;
073: private JComboBox projectTypeComboBox;
074:
075: public static void main(String[] args) {
076: try {
077: UIManager
078: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
079: } catch (Exception e) {
080: // Likely PlasticXP is not in the class path; ignore.
081: }
082: JFrame frame = new JFrame();
083: frame.setTitle("Forms Tutorial :: Plain Building");
084: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
085: JComponent panel = new PlainExample().buildPanel();
086: frame.getContentPane().add(panel);
087: frame.pack();
088: frame.setVisible(true);
089: }
090:
091: // Component Creation and Initialization **********************************
092:
093: /**
094: * Creates and intializes the UI components.
095: */
096: private void initComponents() {
097: companyNameField = new JTextField();
098: contactPersonField = new JTextField();
099: orderNoField = new JTextField();
100: inspectorField = new JTextField();
101: referenceNoField = new JTextField();
102: approvalStatusComboBox = createApprovalStatusComboBox();
103: shipYardField = new JTextField();
104: registerNoField = new JTextField();
105: hullNumbersField = new JTextField();
106: projectTypeComboBox = createProjectTypeComboBox();
107: }
108:
109: /**
110: * Creates and returns a combo box for the approval states.
111: *
112: * @return a combo box for the approval status
113: */
114: private JComboBox createApprovalStatusComboBox() {
115: return new JComboBox(new String[] { "In Progress", "Finished",
116: "Released" });
117: }
118:
119: /**
120: * Creates and returns a combo box for the project types.
121: *
122: * @return a combo box for the project type
123: */
124: private JComboBox createProjectTypeComboBox() {
125: return new JComboBox(new String[] { "New Building",
126: "Conversion", "Repair" });
127: }
128:
129: // Building *************************************************************
130:
131: /**
132: * Builds the pane.
133: *
134: * @return the built panel
135: */
136: public JComponent buildPanel() {
137: initComponents();
138:
139: FormLayout layout = new FormLayout(
140: "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
141: + "right:max(40dlu;pref), 3dlu, 70dlu",
142: "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, "
143: + "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, "
144: + "p, 3dlu, p, 3dlu, p, 3dlu, p");
145:
146: JPanel panel = new JPanel(layout);
147: panel.setBorder(Borders.DIALOG_BORDER);
148:
149: // Fill the table with labels and components.
150: CellConstraints cc = new CellConstraints();
151: panel.add(createSeparator("Manufacturer"), cc.xyw(1, 1, 7));
152: panel.add(new JLabel("Company"), cc.xy(1, 3));
153: panel.add(companyNameField, cc.xyw(3, 3, 5));
154: panel.add(new JLabel("Contact"), cc.xy(1, 5));
155: panel.add(contactPersonField, cc.xyw(3, 5, 5));
156: panel.add(new JLabel("Order No"), cc.xy(1, 7));
157: panel.add(orderNoField, cc.xy(3, 7));
158:
159: panel.add(createSeparator("Inspector"), cc.xyw(1, 9, 7));
160: panel.add(new JLabel("Name"), cc.xy(1, 11));
161: panel.add(inspectorField, cc.xyw(3, 11, 5));
162: panel.add(new JLabel("Reference No"), cc.xy(1, 13));
163: panel.add(referenceNoField, cc.xy(3, 13));
164: panel.add(new JLabel("Status"), cc.xy(1, 15));
165: panel.add(approvalStatusComboBox, cc.xy(3, 15));
166:
167: panel.add(createSeparator("Ship"), cc.xyw(1, 17, 7));
168: panel.add(new JLabel("Shipyard"), cc.xy(1, 19));
169: panel.add(shipYardField, cc.xyw(3, 19, 5));
170: panel.add(new JLabel("Register No"), cc.xy(1, 21));
171: panel.add(registerNoField, cc.xy(3, 21));
172: panel.add(new JLabel("Hull No"), cc.xy(5, 21));
173: panel.add(hullNumbersField, cc.xy(7, 21));
174: panel.add(new JLabel("Project Type"), cc.xy(1, 23));
175: panel.add(projectTypeComboBox, cc.xy(3, 23));
176:
177: return panel;
178: }
179:
180: /**
181: * Creates and returns a separator with a label in the left hand side.<p>
182: *
183: * <pre>
184: * createSeparator("Name"); // No mnemonic
185: * createSeparator("N&ame"); // Mnemonic is 'a'
186: * createSeparator("Save &as"); // Mnemonic is the second 'a'
187: * createSeparator("Look&&Feel"); // No mnemonic, text is Look&Feel
188: * </pre>
189: *
190: * @param textWithMnemonic the label's text -
191: * may contain an ampersand (<tt>&</tt>) to mark a mnemonic
192: * @return a separator with label in the left hand side
193: */
194: private Component createSeparator(String textWithMnemonic) {
195: return DefaultComponentFactory.getInstance().createSeparator(
196: textWithMnemonic);
197: }
198:
199: }
|