KickJava   Java API By Example, From Geeks To Geeks.

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


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.builder.PanelBuilder;
38 import com.jgoodies.forms.layout.CellConstraints;
39 import com.jgoodies.forms.layout.FormLayout;
40
41 /**
42  * Demonstrates the <code>FormLayout</code> with a <code>PanelBuilder</code>.
43  * Columns and rows are specified before the panel is filled with components.
44  * Unlike the {@link PlainExample} this class uses a local variable
45  * to keep track of the current row. The advantage over fixed numbers is,
46  * that it's easier to insert new rows later.
47  * <p>
48  * This panel building style is simple and works quite well. However, you may
49  * consider using a more sophisticated form builder to fill the panel and/or
50  * add rows dynamically; see the {@link DynamicRowsExample} for this alternative.
51  *
52  * @author Karsten Lentzsch
53  * @version $Revision: 1.5 $
54  * @see PlainExample
55  * @see DynamicRowsExample
56  * @see DefaultFormBuilderExample
57  */

58
59 public final class RowCounterExample {
60
61     private JTextField identifierField;
62     private JTextField powerField;
63     private JTextField speedField;
64     private JComboBox materialComboBox;
65     private JComboBox iceClassComboBox;
66     private JTextArea machineryCommentArea;
67     private JTextArea inspectionCommentArea;
68
69
70     public static void main(String JavaDoc[] args) {
71         try {
72             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
73         } catch (Exception JavaDoc e) {
74             // Likely PlasticXP is not in the class path; ignore.
75
}
76         JFrame frame = new JFrame();
77         frame.setTitle("Forms Tutorial :: Row Counter");
78         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
79         JComponent panel = new RowCounterExample().buildPanel();
80         frame.getContentPane().add(panel);
81         frame.pack();
82         frame.show();
83     }
84
85
86     // Component Creation and Initialization **********************************
87

88     /**
89      * Creates and intializes the UI components.
90      */

91     private void initComponents() {
92         identifierField = new JTextField();
93         powerField = new JTextField();
94         speedField = new JTextField();
95         materialComboBox = createMaterialComboBox();
96         iceClassComboBox = createIceClassComboBox();
97         machineryCommentArea = new JTextArea();
98         inspectionCommentArea = new JTextArea();
99     }
100
101     /**
102      * Builds and returns a combo box for materials.
103      */

104     private JComboBox createMaterialComboBox() {
105         return new JComboBox(new String JavaDoc[] {"C45E, ReH=600",
106                                             "Bolt Material, ReH=800"});
107     }
108     /**
109      * Builds and returns a combo box for ice classes.
110      */

111     private JComboBox createIceClassComboBox() {
112         return new JComboBox(new String JavaDoc[] { "E", "E1", "E2", "E3", "E4" });
113     }
114
115
116     // Building *************************************************************
117

118     /**
119      * Builds the content pane.
120      */

121     public JComponent buildPanel() {
122         initComponents();
123         Component JavaDoc machineryPane = new JScrollPane(machineryCommentArea);
124         Component JavaDoc inspectionPane = new JScrollPane(inspectionCommentArea);
125
126         FormLayout layout = new FormLayout(
127                 "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
128               + "right:max(40dlu;pref), 3dlu, 70dlu",
129                 "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, "
130               + "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p");
131         layout.setRowGroups(new int[][] { { 3, 13, 15, 17 } });
132         
133         PanelBuilder builder = new PanelBuilder(layout);
134         builder.setDefaultDialogBorder();
135
136         CellConstraints cc = new CellConstraints();
137         int row = 1;
138
139         builder.addSeparator("Shaft", cc.xywh(1, row++, 7, 1));
140         row++;
141         
142         builder.addLabel("Identifier", cc.xy (1, row));
143         builder.add(identifierField, cc.xy (3, row++));
144         row++;
145
146         builder.addLabel("Power", cc.xy (1, row));
147         builder.add(powerField, cc.xy (3, row));
148         builder.addLabel("Speed", cc.xy (5, row));
149         builder.add(speedField, cc.xy (7, row++));
150         row++;
151
152         builder.addLabel("Material", cc.xy (1, row));
153         builder.add(materialComboBox, cc.xy (3, row));
154         builder.addLabel("Ice Class", cc.xy (5, row));
155         builder.add(iceClassComboBox, cc.xy (7, row++));
156         row++;
157
158         builder.addSeparator("Comments", cc.xywh(1, row++, 7, 1));
159         row++;
160         
161         builder.addLabel("Machinery", cc.xy (1, row));
162         builder.add(machineryPane, cc.xywh(3, row++, 5, 3, "f, f"));
163         row += 3;
164
165         builder.addLabel("Inspection", cc.xy (1, row));
166         builder.add(inspectionPane, cc.xywh(3, row++, 5, 3, "f, f"));
167         
168         return builder.getPanel();
169     }
170
171
172 }
Popular Tags