KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Shows three approaches how to add custom rows to a form that is built
42  * using a DefaultFormBuilder.
43  * 1) single custom row,
44  * 2) standard + custom row,
45  * 3) multiple standard rows.
46  *
47  * They differ in the position of the 'Comment' label, the alignment
48  * of font baselines and the height of the custom row.
49  *
50  * @author Karsten Lentzsch
51  * @version $Revision: 1.2 $
52  * @see DefaultFormBuilder
53  */

54
55 public final class DefaultFormWithCustomRowsExample {
56
57     private JTextField name1Field;
58     private JTextArea comment1Area;
59     private JTextField name2Field;
60     private JTextArea comment2Area;
61     private JTextField name3Field;
62     private JTextArea comment3Area;
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 frame = new JFrame();
72         frame.setTitle("Forms Tutorial :: Custom Rows");
73         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
74         JComponent panel = new DefaultFormWithCustomRowsExample().buildPanel();
75         frame.getContentPane().add(panel);
76         frame.pack();
77         frame.show();
78     }
79
80
81     // Component Creation and Initialization **********************************
82

83     /**
84      * Creates and intializes the UI components.
85      */

86     private void initComponents() {
87         name1Field = new JTextField("Name (font baselines shall be aligned)");
88         comment1Area = new JTextArea(2, 20);
89         comment1Area.setText("The label is a little bit too high.\nLikely the font baselines are unaligned.");
90         name2Field = new JTextField("Name (font baselines shall be aligned)");
91         comment2Area = new JTextArea(2, 20);
92         comment2Area.setText("The label is positioned well.\nThe font baselines shall be aligned.");
93         name3Field = new JTextField("Name (font baselines shall be aligned)");
94         comment3Area = new JTextArea(2, 20);
95         comment3Area.setText("The label is positioned well.\nThe font baselines shall be aligned.");
96     }
97
98
99     // Building ***************************************************************
100

101     /**
102      * Demonstrates three different ways to add custom rows to a form
103      * that is build with a {@link DefaultFormBuilder}.
104      */

105     public JComponent buildPanel() {
106         initComponents();
107
108         FormLayout layout = new FormLayout(
109                 "right:pref, 3dlu, min:grow",
110                 "");
111         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
112         builder.setDefaultDialogBorder();
113         builder.setRowGroupingEnabled(true);
114         
115         CellConstraints cc = new CellConstraints();
116
117         // In this approach, we add a gap and a custom row.
118
// The advantage of this approach is, that we can express
119
// the row spec and comment area cell constraints freely.
120
// The disadvantage is the misalignment of the leading label.
121
// Also the row's height may be inconsistent with other rows.
122
builder.appendSeparator("Single Custom Row");
123         builder.append("Name", name1Field);
124         builder.appendRow(builder.getLineGapSpec());
125         builder.appendRow(new RowSpec("top:31dlu")); // Assumes line is 14, gap is 3
126
builder.nextLine(2);
127         builder.append("Comment");
128         builder.add(new JScrollPane(comment1Area),
129                     cc.xy(builder.getColumn(), builder.getRow(), "fill, fill"));
130         builder.nextLine();
131
132         // In this approach, we append a standard row with gap before it.
133
// The advantage is, that the leading label is aligned well.
134
// The disadvantage is that the comment area now spans
135
// multiple cells and is slightly less flexible.
136
// Also the row's height may be inconsistent with other rows.
137
builder.appendSeparator("Standard + Custom Row");
138         builder.append("Name", name2Field);
139         builder.append("Comment");
140         builder.appendRow(new RowSpec("17dlu")); // Assumes line is 14, gap is 3
141
builder.add(new JScrollPane(comment2Area),
142                     cc.xywh(builder.getColumn(), builder.getRow(), 1, 2));
143         builder.nextLine(2);
144
145         // In this approach, we append two standard rows with associated gaps.
146
// The advantage is, that the leading label is aligned well,
147
// and the height is consistent with other rows.
148
// The disadvantage is that the comment area now spans
149
// multiple cells and is slightly less flexible.
150
builder.appendSeparator("Two Standard Rows");
151         builder.append("Name", name3Field);
152         builder.append("Comment");
153         builder.nextLine();
154         builder.append("");
155         builder.nextRow(-2);
156         builder.add(new JScrollPane(comment3Area),
157                     cc.xywh(builder.getColumn(), builder.getRow(), 1, 3));
158
159         return builder.getPanel();
160     }
161
162
163 }
Popular Tags