KickJava   Java API By Example, From Geeks To Geeks.

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


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.builder.PanelBuilder;
36 import com.jgoodies.forms.layout.FormLayout;
37
38 /**
39  * Combines the <code>FormLayout</code> with the <code>PanelBuilder</code>.
40  * Columns and rows are specified before the panel is filled with components.
41  * The builder's cursor is used to determine the location of the next component.
42  * And the builder's convenience methods are used to add labels and separators.
43  * <p>
44  * This panel building style is intended for learning purposes only.
45  * The recommended style is demonstrated in the {@link DefaultFormBuilderExample}.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.4 $
49  * @see PlainExample
50  * @see RowCounterExample
51  * @see DefaultFormBuilderExample
52  */

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

90     /**
91      * Creates and intializes the UI components.
92      */

93     private void initComponents() {
94         identifierField = new JTextField();
95         ptiField = new JTextField();
96         powerField = new JTextField();
97         lenField = new JTextField();
98         daField = new JTextField();
99         diField = new JTextField();
100         da2Field = new JTextField();
101         di2Field = new JTextField();
102         rField = new JTextField();
103         dField = new JTextField();
104         locationCombo = createLocationComboBox();
105         kFactorField = new JTextField();
106         holesCheckBox = new JCheckBox("Has radial holes", true);
107         slotsCheckBox = new JCheckBox("Has longitudinal slots");
108     }
109
110     /**
111      * Creates and answers 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 pane.
126      */

127     public JComponent buildPanel() {
128         initComponents();
129
130         FormLayout layout = new FormLayout(
131                 "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
132               + "right:max(40dlu;pref), 3dlu, 70dlu",
133                 "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, "
134               + "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, "
135               + "p, 3dlu, p, 3dlu, p, 3dlu, p");
136
137         PanelBuilder builder = new PanelBuilder(layout);
138         builder.setDefaultDialogBorder();
139
140         builder.addSeparator("Segment");
141         builder.nextLine(2);
142
143         builder.addLabel("Identifier"); builder.nextColumn(2);
144         builder.add(identifierField);
145         builder.nextLine(2);
146
147         builder.addLabel("PTI [kW]"); builder.nextColumn(2);
148         builder.add(ptiField); builder.nextColumn(2);
149         builder.addLabel("Power [kW]"); builder.nextColumn(2);
150         builder.add(powerField);
151         builder.nextLine(2);
152
153         builder.addLabel("len [mm]"); builder.nextColumn(2);
154         builder.add(lenField);
155         builder.nextLine(2);
156
157         builder.addSeparator("Diameters");
158         builder.nextLine(2);
159
160         builder.addLabel("da [mm]"); builder.nextColumn(2);
161         builder.add(daField); builder.nextColumn(2);
162         builder.addLabel("di [mm]"); builder.nextColumn(2);
163         builder.add(diField);
164         builder.nextLine(2);
165
166         builder.addLabel("da2 [mm]"); builder.nextColumn(2);
167         builder.add(da2Field); builder.nextColumn(2);
168         builder.addLabel("di2 [mm]"); builder.nextColumn(2);
169         builder.add(di2Field);
170
171         builder.nextLine(2);
172         builder.addLabel("R [mm]"); builder.nextColumn(2);
173         builder.add(rField); builder.nextColumn(2);
174         builder.addLabel("D [mm]"); builder.nextColumn(2);
175         builder.add(dField);
176         builder.nextLine(2);
177
178         builder.addSeparator("Criteria");
179         builder.nextLine(2);
180
181         builder.addLabel("Location"); builder.nextColumn(2);
182         builder.add(locationCombo); builder.nextColumn(2);
183         builder.addLabel("k-factor"); builder.nextColumn(2);
184         builder.add(kFactorField);
185         builder.nextLine(2);
186
187         builder.addLabel("Holes"); builder.nextColumn(2);
188         builder.setColumnSpan(5);
189         builder.add(holesCheckBox);
190         builder.setColumnSpan(1);
191         builder.nextLine(2);
192
193         builder.addLabel("Slots"); builder.nextColumn(2);
194         builder.setColumnSpan(5);
195         builder.add(slotsCheckBox);
196         builder.setColumnSpan(1);
197         
198         return builder.getPanel();
199     }
200
201
202 }
Popular Tags