KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > tutorial > QuickStartExample


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;
32
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JFrame JavaDoc;
35 import javax.swing.JTextField JavaDoc;
36 import javax.swing.UIManager JavaDoc;
37 import javax.swing.WindowConstants JavaDoc;
38
39 import com.jgoodies.forms.builder.PanelBuilder;
40 import com.jgoodies.forms.layout.CellConstraints;
41 import com.jgoodies.forms.layout.FormLayout;
42
43 /**
44  * Quickly introduces the most important features of the FormLayout:
45  * create and configure a layout, create a builder, add components.
46  * <p>
47  * Note that this class is not a JPanel subclass;
48  * it justs uses a JPanel as layout container that will be returned
49  * by <code>#buildPanel()</code>.
50  *
51  * @author Karsten Lentzsch
52  * @version $Revision: 1.4 $
53  */

54
55 public final class QuickStartExample {
56
57     private JTextField JavaDoc companyField;
58     private JTextField JavaDoc contactField;
59     private JTextField JavaDoc ptiField;
60     private JTextField JavaDoc powerField;
61     private JTextField JavaDoc radiusField;
62     private JTextField JavaDoc diameterField;
63
64  
65     public static void main(String JavaDoc[] args) {
66         try {
67             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
68         } catch (Exception JavaDoc e) {
69             // Likely PlasticXP is not in the class path; ignore.
70
}
71         JFrame JavaDoc frame = new JFrame JavaDoc();
72         frame.setTitle("Forms Tutorial :: Quick Start");
73         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
74         JComponent JavaDoc panel = new QuickStartExample().buildPanel();
75         frame.getContentPane().add(panel);
76         frame.pack();
77         frame.show();
78     }
79     
80
81     // Component Creation and Initialization **********************************
82

83     /**
84      * Creates, intializes and configures the UI components.
85      * Real applications may further bind the components to underlying models.
86      */

87     private void initComponents() {
88         companyField = new JTextField JavaDoc();
89         contactField = new JTextField JavaDoc();
90         ptiField = new JTextField JavaDoc(6);
91         powerField = new JTextField JavaDoc(10);
92         radiusField = new JTextField JavaDoc(8);
93         diameterField = new JTextField JavaDoc(8);
94     }
95
96     // Building *************************************************************
97

98     /**
99      * Builds the panel. Initializes and configures components first,
100      * then creates a FormLayout, configures the layout, creates a builder,
101      * sets a border, and finally adds the components.
102      */

103     public JComponent JavaDoc buildPanel() {
104         // Separating the component initialization and configuration
105
// from the layout code makes both parts easier to read.
106
initComponents();
107
108         // Create a FormLayout instance on the given column and row specs.
109
// For almost all forms you specify the columns; sometimes rows are
110
// created dynamically. In this case the labels are right aligned.
111
FormLayout layout = new FormLayout(
112                 "right:pref, 3dlu, pref, 7dlu, right:pref, 3dlu, pref", // cols
113
"p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, p, 3dlu, p"); // rows
114

115         // Specify that columns 1 & 5 as well as 3 & 7 have equal widths.
116
layout.setColumnGroups(new int[][]{{1, 5}, {3, 7}});
117         
118         // Create a builder that assists in adding components to the container.
119
// Wrap the panel with a standardized border.
120
PanelBuilder builder = new PanelBuilder(layout);
121         builder.setDefaultDialogBorder();
122
123         // Obtain a reusable constraints object to place components in the grid.
124
CellConstraints cc = new CellConstraints();
125
126         // Fill the grid with components; the builder offers to create
127
// frequently used components, e.g. separators and labels.
128

129         // Add a titled separator to cell (1, 1) that spans 7 columns and 1 row.
130
builder.addSeparator("General", cc.xywh(1, 1, 7, 1));
131         builder.addLabel("Company", cc.xy (1, 3));
132         builder.add(companyField, cc.xywh(3, 3, 5, 1));
133         builder.addLabel("Contact", cc.xy (1, 5));
134         builder.add(contactField, cc.xywh(3, 5, 5, 1));
135
136         builder.addSeparator("Propeller", cc.xywh(1, 7, 7, 1));
137         builder.addLabel("PTI [kW]", cc.xy (1, 9));
138         builder.add(ptiField, cc.xy (3, 9));
139         builder.addLabel("Power [kW]", cc.xy (5, 9));
140         builder.add(powerField, cc.xy (7, 9));
141         builder.addLabel("R [mm]", cc.xy (1, 11));
142         builder.add(radiusField, cc.xy (3, 11));
143         builder.addLabel("D [mm]", cc.xy (5, 11));
144         builder.add(diameterField, cc.xy (7, 11));
145         
146         // The builder holds the layout container that we now return.
147
return builder.getPanel();
148     }
149     
150 }
Popular Tags