KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.*;
34
35 import com.jgoodies.forms.extras.DefaultFormBuilder;
36 import com.jgoodies.forms.layout.FormLayout;
37
38 /**
39  * Uses the FormLayout and the <code>DefaultFormBuilder</code>.
40  * Columns are specified before the panel is filled with components,
41  * rows are added dynamically. The builder is used to hold a cursor,
42  * to add rows dynamically, and to fill components.
43  * The builder's convenience methods are used to add labels and separators.
44  * <p>
45  * This panel building style is recommended unless you have a more
46  * powerful builder or don't want to add rows dynamically.
47  * See the {@link DynamicRowsExample} for an implementation that specifies
48  * rows before the panel is filled with components.
49  *
50  * @author Karsten Lentzsch
51  * @version $Revision: 1.4 $
52  * @see DefaultFormBuilder
53  * @see PlainExample
54  * @see RowCounterExample
55  * @see DynamicRowsExample
56  */

57
58 public final class DefaultFormBuilderExample {
59
60     private JTextField identifierField;
61     private JTextField ptiField;
62     private JTextField powerField;
63     private JTextField sField;
64     private JTextField daField;
65     private JTextField diField;
66     private JTextField da2Field;
67     private JTextField di2Field;
68     private JTextField rField;
69     private JTextField dField;
70     private JComboBox locationCombo;
71     private JTextField kFactorField;
72     
73
74     public static void main(String JavaDoc[] args) {
75         try {
76             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
77         } catch (Exception JavaDoc e) {
78             // Likely PlasticXP is not in the class path; ignore.
79
}
80         JFrame frame = new JFrame();
81         frame.setTitle("Forms Tutorial :: Default Form");
82         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
83         JComponent panel = new DefaultFormBuilderExample().buildPanel();
84         frame.getContentPane().add(panel);
85         frame.pack();
86         frame.show();
87     }
88
89
90     // Component Creation and Initialization **********************************
91

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

95     private void initComponents() {
96         identifierField = new JTextField();
97         ptiField = new JTextField();
98         powerField = new JTextField();
99         sField = new JTextField();
100         daField = new JTextField();
101         diField = new JTextField();
102         da2Field = new JTextField();
103         di2Field = new JTextField();
104         rField = new JTextField();
105         dField = new JTextField();
106         locationCombo = createLocationComboBox();
107         kFactorField = new JTextField();
108     }
109
110     /**
111      * Creates and returns a combo box for the locations.
112      */

113     private JComboBox createLocationComboBox() {
114         return new JComboBox(
115             new String JavaDoc[] {
116                 "Propeller nut thread",
117                 "Stern tube front area",
118                 "Shaft taper" });
119     }
120
121
122     // Building ***************************************************************
123

124     /**
125      * Builds the flange editor panel.
126      * Columns are specified before components are added to the form,
127      * rows are added dynamically using the {@link DefaultFormBuilder}.
128      * <p>
129      * The builder combines a step that is done again and again:
130      * add a label, proceed to the next data column and add a component.
131      */

132     public JComponent buildPanel() {
133         initComponents();
134
135         FormLayout layout = new FormLayout(
136                 "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
137               + "right:max(40dlu;pref), 3dlu, 70dlu",
138                 "");
139         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
140         builder.setDefaultDialogBorder();
141
142
143         builder.appendSeparator("Flange");
144
145         builder.append("&Identifier", identifierField);
146         builder.nextLine();
147
148         builder.append("PTI [kW]", ptiField);
149         builder.append("Power [kW]", powerField);
150
151         builder.append("s [mm]", sField);
152         builder.nextLine();
153
154
155         builder.appendSeparator("Diameters");
156
157         builder.append("&da [mm]", daField);
158         builder.append("di [mm]", diField);
159
160         builder.append("da2 [mm]", da2Field);
161         builder.append("di2 [mm]", di2Field);
162
163         builder.append("R [mm]", rField);
164         builder.append("D [mm]", dField);
165
166
167         builder.appendSeparator("Criteria");
168
169         builder.append("&Location", locationCombo);
170         builder.append("k-factor", kFactorField);
171
172         return builder.getPanel();
173     }
174
175 }
Popular Tags