KickJava   Java API By Example, From Geeks To Geeks.

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


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 FormLayout growing options: none, default, weighted.
41  *
42  * @author Karsten Lentzsch
43  * @version $Revision: 1.6 $
44  */

45 public final class GrowingExample {
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 :: Growing");
56         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
57         JComponent panel = new GrowingExample().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 1", buildHorizontalGrowing1Panel());
69         tabbedPane.add("Horizontal 2", buildHorizontalGrowing2Panel());
70         tabbedPane.add("Horizontal 3", buildHorizontalGrowing3Panel());
71         tabbedPane.add("Horizontal 4", buildHorizontalGrowing4Panel());
72         tabbedPane.add("Vertical 1", buildVerticalGrowing1Panel());
73         tabbedPane.add("Vertical 2", buildVerticalGrowing2Panel());
74         return tabbedPane;
75     }
76     
77     
78     private JComponent buildHorizontalGrowing1Panel() {
79         FormLayout layout = new FormLayout(
80             "pref, 6px, pref:grow",
81             "pref, 12px, pref");
82             
83         JPanel panel = new JPanel(layout);
84         panel.setBorder(Borders.DIALOG_BORDER);
85         CellConstraints cc = new CellConstraints();
86
87         panel.add(new JLabel("Fixed"), cc.xy(1, 1));
88         panel.add(new JLabel("Gets all extra space"), cc.xy(3, 1));
89         
90         panel.add(new JTextField(5), cc.xy(1, 3));
91         panel.add(new JTextField(5), cc.xy(3, 3));
92
93         return panel;
94     }
95     
96     
97     private JComponent buildHorizontalGrowing2Panel() {
98         FormLayout layout = new FormLayout(
99             "pref, 6px, 0:grow, 6px, 0:grow",
100             "pref, 12px, pref");
101             
102         JPanel panel = new JPanel(layout);
103         panel.setBorder(Borders.DIALOG_BORDER);
104         CellConstraints cc = new CellConstraints();
105
106         panel.add(new JLabel("Fixed"), cc.xy(1, 1));
107         panel.add(new JLabel("Gets half of extra space"), cc.xy(3, 1));
108         panel.add(new JLabel("gets half of extra space"), cc.xy(5, 1));
109         
110         panel.add(new JTextField(5), cc.xy(1, 3));
111         panel.add(new JTextField(5), cc.xy(3, 3));
112         panel.add(new JTextField(5), cc.xy(5, 3));
113
114         return panel;
115     }
116     
117     
118     private JComponent buildHorizontalGrowing3Panel() {
119         FormLayout layout = new FormLayout(
120             "pref, 6px, 0:grow(0.25), 6px, 0:grow(0.75)",
121             "pref, 12px, pref");
122             
123         JPanel panel = new JPanel(layout);
124         panel.setBorder(Borders.DIALOG_BORDER);
125         CellConstraints cc = new CellConstraints();
126
127         panel.add(new JLabel("Fixed"), cc.xy(1, 1));
128         panel.add(new JLabel("Gets 25% of extra space"), cc.xy(3, 1));
129         panel.add(new JLabel("Gets 75% of extra space"), cc.xy(5, 1));
130         
131         panel.add(new JTextField(5), cc.xy(1, 3));
132         panel.add(new JTextField(5), cc.xy(3, 3));
133         panel.add(new JTextField(5), cc.xy(5, 3));
134
135         return panel;
136     }
137     
138     
139     private JComponent buildHorizontalGrowing4Panel() {
140         FormLayout layout = new FormLayout(
141             "pref:grow(0.33), 6px, pref:grow(0.67)",
142             "pref, 12px, pref");
143             
144         JPanel panel = new JPanel(layout);
145         panel.setBorder(Borders.DIALOG_BORDER);
146         CellConstraints cc = new CellConstraints();
147
148         panel.add(new JLabel("Gets 33% of the space"), cc.xy(1, 1));
149         panel.add(new JLabel("Gets 67% of the space"), cc.xy(3, 1));
150         
151         panel.add(new JTextField(5), cc.xy(1, 3));
152         panel.add(new JTextField(5), cc.xy(3, 3));
153
154         return panel;
155     }
156     
157     private JComponent buildVerticalGrowing1Panel() {
158         FormLayout layout = new FormLayout(
159             "pref, 12px, pref",
160             "pref, 6px, fill:0:grow(0.25), 6px, fill:0:grow(0.75)");
161             
162         JPanel panel = new JPanel(layout);
163         panel.setBorder(Borders.DIALOG_BORDER);
164         CellConstraints cc = new CellConstraints();
165
166         panel.add(new JLabel("Fixed"), cc.xy(1, 1));
167         panel.add(new JLabel("Gets 25% of extra space"), cc.xy(1, 3));
168         panel.add(new JLabel("Gets 75% of extra space"), cc.xy(1, 5));
169         
170         panel.add(createTextArea(4, 30), cc.xy(3, 1));
171         panel.add(createTextArea(4, 30), cc.xy(3, 3));
172         panel.add(createTextArea(4, 30), cc.xy(3, 5));
173
174         return panel;
175     }
176     
177     private JComponent buildVerticalGrowing2Panel() {
178         FormLayout layout = new FormLayout(
179             "pref, 12px, pref",
180             "fill:0:grow(0.25), 6px, fill:0:grow(0.75)");
181             
182         JPanel panel = new JPanel(layout);
183         panel.setBorder(Borders.DIALOG_BORDER);
184         CellConstraints cc = new CellConstraints();
185
186         panel.add(new JLabel("Gets 25% of extra space"), cc.xy(1, 1));
187         panel.add(new JLabel("Gets 75% of extra space"), cc.xy(1, 3));
188         
189         panel.add(createTextArea(4, 30), cc.xy(3, 1));
190         panel.add(createTextArea(4, 30), cc.xy(3, 3));
191
192         return panel;
193     }
194     
195     
196     // Component Creation *****************************************************
197

198     private JComponent createTextArea(int rows, int cols) {
199         return new JScrollPane(new JTextArea(rows, cols),
200                     ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
201                     ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
202     }
203     
204     
205 }
206
Popular Tags