KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > tutorial > basics > BasicSizesExample


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.basics;
32
33 import javax.swing.*;
34
35 import com.jgoodies.forms.factories.Borders;
36 import com.jgoodies.forms.layout.CellConstraints;
37 import com.jgoodies.forms.layout.FormLayout;
38
39 /**
40  * Demonstrates the basic FormLayout sizes: constant, minimum, preferred.
41  *
42  * @author Karsten Lentzsch
43  * @version $Revision: 1.6 $
44  */

45 public final class BasicSizesExample {
46
47     
48     public static void main(String JavaDoc[] args) {
49         try {
50             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
51         } catch (Exception JavaDoc e) {
52             // Likely PlasticXP is not in the class path; ignore.
53
}
54         JFrame frame = new JFrame();
55         frame.setTitle("Forms Tutorial :: Basic Sizes");
56         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
57         JComponent panel = new BasicSizesExample().buildPanel();
58         frame.getContentPane().add(panel);
59         frame.pack();
60         frame.show();
61     }
62
63
64     public JComponent buildPanel() {
65         JTabbedPane tabbedPane = new JTabbedPane();
66         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
67
68         tabbedPane.add("Horizontal", buildHorizontalSizesPanel());
69         tabbedPane.add("Vertical", buildVerticalSizesPanel());
70         return tabbedPane;
71     }
72     
73     
74     private JComponent buildHorizontalSizesPanel() {
75         FormLayout layout = new FormLayout(
76             "pref, 12px, " + "75px, 25px, min, 25px, pref",
77             "pref, 12px, pref");
78             
79         // Create a panel that uses the layout.
80
JPanel panel = new JPanel(layout);
81
82         // Set a default border.
83
panel.setBorder(Borders.DIALOG_BORDER);
84         
85         // Create a reusable CellConstraints instance.
86
CellConstraints cc = new CellConstraints();
87
88         // Add components to the panel.
89
panel.add(new JLabel("75px"), cc.xy(3, 1));
90         panel.add(new JLabel("Min"), cc.xy(5, 1));
91         panel.add(new JLabel("Pref"), cc.xy(7, 1));
92         
93         panel.add(new JLabel("new JTextField(15)"), cc.xy(1, 3));
94
95         panel.add(new JTextField(15), cc.xy(3, 3));
96         panel.add(new JTextField(15), cc.xy(5, 3));
97         panel.add(new JTextField(15), cc.xy(7, 3));
98
99         return panel;
100     }
101     
102     
103     private JComponent buildVerticalSizesPanel() {
104         FormLayout layout = new FormLayout(
105             "pref, 12px, pref",
106             "pref, 12px, 45px, 12px, min, 12px, pref");
107             
108         // Create a panel that uses the layout.
109
JPanel panel = new JPanel(layout);
110
111         // Set a default border.
112
panel.setBorder(Borders.DIALOG_BORDER);
113         
114         // Create a reusable CellConstraints instance.
115
CellConstraints cc = new CellConstraints();
116
117         // Add components to the panel.
118
panel.add(new JLabel("new JTextArea(10, 40)"), cc.xy(3, 1));
119         
120         panel.add(new JLabel("45px"), cc.xy(1, 3));
121         panel.add(new JLabel("Min"), cc.xy(1, 5));
122         panel.add(new JLabel("Pref"), cc.xy(1, 7));
123         
124         panel.add(createTextArea(10, 40), cc.xy(3, 3));
125         panel.add(createTextArea(10, 40), cc.xy(3, 5));
126         panel.add(createTextArea(10, 40), cc.xy(3, 7));
127
128         return panel;
129     }
130     
131     private JComponent createTextArea(int rows, int cols) {
132         return new JScrollPane(new JTextArea(rows, cols),
133                     ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
134                     ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
135     }
136     
137     
138     // Helper Classes *********************************************************
139

140     /**
141      * Creates and answers a button that can have predefined minimum
142      * and preferred sizes. In the constructor you can specify or omit
143      * the minimum width, height and preferred width/height.
144      */

145 // private static class SpecialSizeButton extends JButton {
146
//
147
// private final Dimension fixedMinimumSize;
148
// private final Dimension fixedPreferredSize;
149
//
150
// private SpecialSizeButton(
151
// String text,
152
// Dimension fixedMinimumSize,
153
// Dimension fixedPreferredSize) {
154
// super(text);
155
// this.fixedMinimumSize = fixedMinimumSize;
156
// this.fixedPreferredSize = fixedPreferredSize;
157
// //putClientProperty("jgoodies.isNarrow", Boolean.TRUE);
158
// }
159
//
160
// public Dimension getMinimumSize() {
161
// Dimension d = super.getMinimumSize();
162
// return new Dimension(
163
// fixedMinimumSize.width == -1
164
// ? d.width
165
// : fixedMinimumSize.width,
166
// fixedMinimumSize.height == -1
167
// ? d.height
168
// : fixedMinimumSize.height);
169
// }
170
//
171
// public Dimension getPreferredSize() {
172
// Dimension d = super.getPreferredSize();
173
// return new Dimension(
174
// fixedPreferredSize.width == -1
175
// ? d.width
176
// : fixedPreferredSize.width,
177
// fixedPreferredSize.height == -1
178
// ? d.height
179
// : fixedPreferredSize.height);
180
// }
181
// }
182

183     
184 }
185
186
Popular Tags