KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > tutorial > building > PlainExample


1 /*
2  * Copyright (c) 2003 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.forms.tutorial.building;
32
33 import java.awt.Component JavaDoc;
34
35 import javax.swing.*;
36
37 import com.jgoodies.forms.factories.Borders;
38 import com.jgoodies.forms.factories.DefaultComponentFactory;
39 import com.jgoodies.forms.layout.CellConstraints;
40 import com.jgoodies.forms.layout.FormLayout;
41
42 /**
43  * Demonstrates a <i>pure</i> use of the FormLayout.
44  * Columns and rows are specified before the panel is filled with
45  * components. And the panel is filled without a builder.
46  * <p>
47  * This panel building style is simple but not recommended. Other panel
48  * building styles use a builder to fill the panel and/or create
49  * form rows dynamically. See the {@link PanelBuilderExample} for
50  * a slightly better panel building style that can use the builder
51  * to create text labels and separators.
52  *
53  * @author Karsten Lentzsch
54  * @version $Revision: 1.4 $
55  * @see PanelBuilderExample
56  * @see RowCounterExample
57  * @see DynamicRowsExample
58  * @see DefaultFormBuilderExample
59  */

60
61 public final class PlainExample {
62
63     private JTextField companyNameField;
64     private JTextField contactPersonField;
65     private JTextField orderNoField;
66     private JTextField inspectorField;
67     private JTextField referenceNoField;
68     private JComboBox approvalStatusComboBox;
69     private JTextField shipYardField;
70     private JTextField registerNoField;
71     private JTextField hullNumbersField;
72     private JComboBox projectTypeComboBox;
73
74  
75     public static void main(String JavaDoc[] args) {
76         try {
77             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
78         } catch (Exception JavaDoc e) {
79             // Likely PlasticXP is not in the class path; ignore.
80
}
81         JFrame frame = new JFrame();
82         frame.setTitle("Forms Tutorial :: Plain Building");
83         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
84         JComponent panel = new PlainExample().buildPanel();
85         frame.getContentPane().add(panel);
86         frame.pack();
87         frame.show();
88     }
89     
90
91     // Component Creation and Initialization **********************************
92

93     /**
94      * Creates and intializes the UI components.
95      */

96     private void initComponents() {
97         companyNameField = new JTextField();
98         contactPersonField = new JTextField();
99         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     private JComboBox createApprovalStatusComboBox() {
113         return new JComboBox(
114             new String JavaDoc[] { "In Progress", "Finished", "Released" });
115     }
116
117     /**
118      * Creates and returns a combo box for the project types.
119      */

120     private JComboBox createProjectTypeComboBox() {
121         return new JComboBox(
122             new String JavaDoc[] { "New Building", "Conversion", "Repair" });
123     }
124
125
126     // Building *************************************************************
127

128     /**
129      * Builds the pane.
130      */

131     public JComponent buildPanel() {
132         initComponents();
133
134         FormLayout layout = new FormLayout(
135                 "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
136               + "right:max(40dlu;pref), 3dlu, 70dlu",
137                 "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
138                 "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
139                 "p, 3dlu, p, 3dlu, p, 3dlu, p");
140                 
141         JPanel panel = new JPanel(layout);
142         panel.setBorder(Borders.DIALOG_BORDER);
143
144         // Fill the table with labels and components.
145
CellConstraints cc = new CellConstraints();
146         panel.add(createSeparator("Manufacturer"), cc.xywh(1, 1, 7, 1));
147         panel.add(new JLabel("Company"), cc.xy (1, 3));
148         panel.add(companyNameField, cc.xywh(3, 3, 5, 1));
149         panel.add(new JLabel("Contact"), cc.xy (1, 5));
150         panel.add(contactPersonField, cc.xywh(3, 5, 5, 1));
151         panel.add(new JLabel("Order No"), cc.xy (1, 7));
152         panel.add(orderNoField, cc.xy (3, 7));
153
154         panel.add(createSeparator("Inspector"), cc.xywh(1, 9, 7, 1));
155         panel.add(new JLabel("Name"), cc.xy (1, 11));
156         panel.add(inspectorField, cc.xywh(3, 11, 5, 1));
157         panel.add(new JLabel("Reference No"), cc.xy (1, 13));
158         panel.add(referenceNoField, cc.xy (3, 13));
159         panel.add(new JLabel("Status"), cc.xy (1, 15));
160         panel.add(approvalStatusComboBox, cc.xy (3, 15));
161         
162         panel.add(createSeparator("Ship"), cc.xywh(1, 17, 7, 1));
163         panel.add(new JLabel("Shipyard"), cc.xy (1, 19));
164         panel.add(shipYardField, cc.xywh(3, 19, 5, 1));
165         panel.add(new JLabel("Register No"), cc.xy (1, 21));
166         panel.add(registerNoField, cc.xy (3, 21));
167         panel.add(new JLabel("Hull No"), cc.xy (5, 21));
168         panel.add(hullNumbersField, cc.xy (7, 21));
169         panel.add(new JLabel("Project Type"), cc.xy (1, 23));
170         panel.add(projectTypeComboBox, cc.xy (3, 23));
171         
172         return panel;
173     }
174     
175     /**
176      * Creates and answer a separator with a label in the left hand side.
177      *
178      * @param text the label's text
179      * @return a separator with label in the left hand side
180      */

181     private Component JavaDoc createSeparator(String JavaDoc text) {
182         return DefaultComponentFactory.getInstance().createSeparator(text);
183     }
184
185 }
Popular Tags