KickJava   Java API By Example, From Geeks To Geeks.

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


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.CellConstraints;
37 import com.jgoodies.forms.layout.FormLayout;
38 import com.jgoodies.forms.layout.RowSpec;
39
40 /**
41  * Compares approaches how to append a custom area at the end of
42  * a panel built with the DefaultFormBuilder:
43  * 1) using two custom rows to align the leading label,
44  * 2) using a single custom row with label on top,
45  * 3) using a separator.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.2 $
49  * @see DefaultFormBuilder
50  * @see DefaultFormWithCustomRowsExample
51  */

52
53 public final class DefaultFormWithCustomAreasExample {
54
55     
56
57     public static void main(String JavaDoc[] args) {
58         try {
59             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
60         } catch (Exception JavaDoc e) {
61             // Likely PlasticXP is not in the class path; ignore.
62
}
63         JFrame frame = new JFrame();
64         frame.setTitle("Forms Tutorial :: Custom Areas");
65         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
66         JComponent panel = new DefaultFormWithCustomAreasExample().buildPanel();
67         frame.getContentPane().add(panel);
68         frame.pack();
69         frame.show();
70     }
71
72
73
74     // Building ***************************************************************
75

76     public JComponent buildPanel() {
77         JTabbedPane tabbedPane = new JTabbedPane();
78         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
79
80         tabbedPane.add(buildCustomAreaWithAlignedLabelPanel(), "Aligned label");
81         tabbedPane.add(buildCustomAreaWithTopLabelPanel(), "Top label");
82         tabbedPane.add(buildCustomAreaWithSeparatorPanel(), "Separator");
83         return tabbedPane;
84     }
85     
86     
87     private DefaultFormBuilder buildPanelHeader() {
88         FormLayout layout = new FormLayout(
89                 "right:pref, 3dlu, min:grow",
90                 "");
91         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
92         builder.setDefaultDialogBorder();
93         builder.setRowGroupingEnabled(true);
94         
95         builder.appendSeparator("Customer Data");
96         builder.append("Last Name", new JTextField());
97         builder.append("First Name", new JTextField());
98         builder.append("Street", new JTextField());
99         builder.append("Email", new JTextField());
100
101         return builder;
102     }
103     
104     
105     /**
106      * Demonstrates how to append a larger custom area at the end of
107      * a panel that is build with a {@link DefaultFormBuilder}.
108      * <p>
109      * We add a gap and a single custom row that grows and that
110      * is filled vertically (where the default is center vertically).
111      * The area uses a standard leading label.
112      */

113     private JComponent buildCustomAreaWithAlignedLabelPanel() {
114         DefaultFormBuilder builder = buildPanelHeader();
115
116         CellConstraints cc = new CellConstraints();
117         
118         builder.append("Feeback");
119         builder.appendRow(new RowSpec("0:grow"));
120         builder.add(new JScrollPane(new JTextArea()),
121                     cc.xywh(builder.getColumn(), builder.getRow(), 1, 2, "fill, fill"));
122
123         return builder.getPanel();
124     }
125
126
127     /**
128      * Demonstrates how to append two custom areas at the end of
129      * a panel that is build with a {@link DefaultFormBuilder}.
130      */

131     private JComponent buildCustomAreaWithTopLabelPanel() {
132         DefaultFormBuilder builder = buildPanelHeader();
133
134         CellConstraints cc = new CellConstraints();
135         
136         builder.appendRow(builder.getLineGapSpec());
137         builder.appendRow(new RowSpec("top:pref:grow"));
138         builder.nextLine(2);
139         builder.append("Feedback");
140         builder.add(new JScrollPane(new JTextArea()),
141                     cc.xy(builder.getColumn(), builder.getRow(), "fill, fill"));
142
143         return builder.getPanel();
144     }
145     
146
147     /**
148      * Demonstrates how to append a larger custom area at the end of
149      * a panel that is build with a {@link DefaultFormBuilder}.
150      * <p>
151      * We add a gap and a single custom row that grows and that
152      * is filled vertically (where the default is center vertically).
153      * The area is separated by a titled separator and it is indented
154      * using an empty leading label.
155      */

156     private JComponent buildCustomAreaWithSeparatorPanel() {
157         DefaultFormBuilder builder = buildPanelHeader();
158
159         builder.appendSeparator("Customer Feedback");
160         builder.appendRow(builder.getLineGapSpec());
161         builder.appendRow(new RowSpec("fill:pref:grow"));
162         builder.nextLine(2);
163         builder.append("", new JScrollPane(new JTextArea()));
164
165         return builder.getPanel();
166     }
167
168
169  }
Popular Tags