KickJava   Java API By Example, From Geeks To Geeks.

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


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.JComboBox JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JFrame JavaDoc;
36 import javax.swing.JTextField JavaDoc;
37 import javax.swing.UIManager JavaDoc;
38 import javax.swing.WindowConstants JavaDoc;
39
40 import com.jgoodies.forms.builder.PanelBuilder;
41 import com.jgoodies.forms.layout.CellConstraints;
42 import com.jgoodies.forms.layout.FormLayout;
43
44 /**
45  * Demonstrates a typical use of the FormLayout.
46  * Columns and rows are specified before the panel is filled with
47  * components, and the panel is filled with a PanelBuilder.
48  * <p>
49  * Unlike the PlainExample, this implementation can delegate
50  * the component creation for text labels and titled separators
51  * to the builder.
52  * <p>
53  * This panel building style is recommended for panels with
54  * a medium number of rows and components. If the panel has more rows,
55  * you may consider using a row variable to address the current row.
56  *
57  * @author Karsten Lentzsch
58  * @version $Revision: 1.5 $
59  * @see PanelBuilder
60  * @see RowCounterExample
61  * @see DynamicRowsExample
62  * @see DefaultFormBuilderExample
63  */

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

97     /**
98      * Creates and intializes the UI components.
99      */

100     private void initComponents() {
101         companyNameField = new JTextField JavaDoc();
102         contactPersonField = new JTextField JavaDoc();
103         orderNoField = new JTextField JavaDoc();
104         inspectorField = new JTextField JavaDoc();
105         referenceNoField = new JTextField JavaDoc();
106         approvalStatusComboBox = createApprovalStatusComboBox();
107         shipYardField = new JTextField JavaDoc();
108         registerNoField = new JTextField JavaDoc();
109         hullNumbersField = new JTextField JavaDoc();
110         projectTypeComboBox = createProjectTypeComboBox();
111     }
112
113     /**
114      * Creates and returns a combo box for the approval states.
115      */

116     private JComboBox JavaDoc createApprovalStatusComboBox() {
117         return new JComboBox JavaDoc(
118             new String JavaDoc[] { "In Progress", "Finished", "Released" });
119     }
120
121     /**
122      * Creates and returns a combo box for the project types.
123      */

124     private JComboBox JavaDoc createProjectTypeComboBox() {
125         return new JComboBox JavaDoc(
126             new String JavaDoc[] { "New Building", "Conversion", "Repair" });
127     }
128
129
130     // Building *************************************************************
131

132     /**
133      * Builds the pane.
134      */

135     public JComponent JavaDoc buildPanel() {
136         initComponents();
137
138         FormLayout layout = new FormLayout(
139                 "right:max(40dlu;pref), 3dlu, 70dlu, 7dlu, "
140               + "right:max(40dlu;pref), 3dlu, 70dlu",
141                 "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
142                 "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
143                 "p, 3dlu, p, 3dlu, p, 3dlu, p");
144                 
145         PanelBuilder builder = new PanelBuilder(layout);
146         builder.setDefaultDialogBorder();
147
148         // Fill the table with labels and components.
149
CellConstraints cc = new CellConstraints();
150         builder.addSeparator("Manufacturer", cc.xywh(1, 1, 7, 1));
151         builder.addLabel("Company", cc.xy (1, 3));
152         builder.add(companyNameField, cc.xywh(3, 3, 5, 1));
153         builder.addLabel("Contact", cc.xy (1, 5));
154         builder.add(contactPersonField, cc.xywh(3, 5, 5, 1));
155         builder.addLabel("Order No", cc.xy (1, 7));
156         builder.add(orderNoField, cc.xy (3, 7));
157
158         builder.addSeparator("Inspector", cc.xywh(1, 9, 7, 1));
159         builder.addLabel("Name", cc.xy (1, 11));
160         builder.add(inspectorField, cc.xywh(3, 11, 5, 1));
161         builder.addLabel("Reference No", cc.xy (1, 13));
162         builder.add(referenceNoField, cc.xy (3, 13));
163         builder.addLabel("Status", cc.xy (1, 15));
164         builder.add(approvalStatusComboBox, cc.xy (3, 15));
165         
166         builder.addSeparator("Ship", cc.xywh(1, 17, 7, 1));
167         builder.addLabel("Shipyard", cc.xy (1, 19));
168         builder.add(shipYardField, cc.xywh(3, 19, 5, 1));
169         builder.addLabel("Register No", cc.xy (1, 21));
170         builder.add(registerNoField, cc.xy (3, 21));
171         builder.addLabel("Hull No", cc.xy (5, 21));
172         builder.add(hullNumbersField, cc.xy (7, 21));
173         builder.addLabel("Project Type", cc.xy (1, 23));
174         builder.add(projectTypeComboBox, cc.xy (3, 23));
175         
176         return builder.getPanel();
177     }
178     
179 }
Popular Tags