KickJava   Java API By Example, From Geeks To Geeks.

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


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.extras.DefaultFormBuilder;
36 import com.jgoodies.forms.factories.Borders;
37 import com.jgoodies.forms.layout.CellConstraints;
38 import com.jgoodies.forms.layout.FormLayout;
39
40 /**
41  * Demonstrates the different sizing units provided by the FormLayout:
42  * Pixel, Dialog Units (dlu), Point, Millimeter, Centimeter and Inches.
43  * Pt, mm, cm, in honor the screen resolution; dlus honor the font size too.
44  *
45  * @author Karsten Lentzsch
46  * @version $Revision: 1.6 $
47  */

48 public final class UnitsExample {
49
50     
51     public static void main(String JavaDoc[] args) {
52         try {
53             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
54         } catch (Exception JavaDoc e) {
55             // Likely PlasticXP is not in the class path; ignore.
56
}
57         JFrame frame = new JFrame();
58         frame.setTitle("Forms Tutorial :: Units");
59         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
60         JComponent panel = new UnitsExample().buildPanel();
61         frame.getContentPane().add(panel);
62         frame.pack();
63         frame.show();
64     }
65
66
67     public JComponent buildPanel() {
68         JTabbedPane tabbedPane = new JTabbedPane();
69         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
70
71         tabbedPane.add("Horizontal", buildHorizontalUnitsPanel());
72         tabbedPane.add("Horizontal Dlu", buildHorizontalDluPanel());
73         tabbedPane.add("Vertical", buildVerticalUnitsPanel());
74         tabbedPane.add("Vertical Dlu", buildVerticalDluPanel());
75         return tabbedPane;
76     }
77     
78     
79     private JComponent buildHorizontalUnitsPanel() {
80         FormLayout layout = new FormLayout(
81             "right:pref, 1dlu, left:pref, 4dlu, left:pref",
82             "");
83         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
84         builder.setDefaultDialogBorder();
85
86         builder.append("72", new JLabel("pt"), buildHorizontalPanel("72pt"));
87         builder.append("25.4", new JLabel("mm"), buildHorizontalPanel("25.4mm"));
88         builder.append("2.54", new JLabel("cm"), buildHorizontalPanel("2.54cm"));
89         builder.append("1", new JLabel("in"), buildHorizontalPanel("1in"));
90         builder.append("72", new JLabel("px"), buildHorizontalPanel("72px"));
91         builder.append("96", new JLabel("px"), buildHorizontalPanel("96px"));
92         builder.append("120", new JLabel("px"), buildHorizontalPanel("120px"));
93         
94         return builder.getPanel();
95     }
96     
97     
98     private JComponent buildHorizontalDluPanel() {
99         FormLayout layout = new FormLayout(
100             "right:pref, 1dlu, left:pref, 4dlu, left:pref",
101             "");
102         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
103         builder.setDefaultDialogBorder();
104
105         builder.append("9", new JLabel("cols"), buildHorizontalPanel(9));
106         builder.append("50", new JLabel("dlu"), buildHorizontalPanel("50dlu"));
107         builder.append("75", new JLabel("px"), buildHorizontalPanel("75px"));
108         builder.append("88", new JLabel("px"), buildHorizontalPanel("88px"));
109         builder.append("100",new JLabel("px"), buildHorizontalPanel("100px"));
110         
111         return builder.getPanel();
112     }
113     
114     
115     private JComponent buildVerticalUnitsPanel() {
116         FormLayout layout = new FormLayout(
117             "c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p",
118             "pref, 6dlu, top:pref");
119             
120         JPanel panel = new JPanel(layout);
121         panel.setBorder(Borders.DIALOG_BORDER);
122         CellConstraints cc = new CellConstraints();
123
124         panel.add(new JLabel("72 pt"), cc.xy(1, 1));
125         panel.add(buildVerticalPanel("72pt"), cc.xy(1, 3));
126         
127         panel.add(new JLabel("25.4 mm"), cc.xy(3, 1));
128         panel.add(buildVerticalPanel("25.4mm"), cc.xy(3, 3));
129         
130         panel.add(new JLabel("2.54 cm"), cc.xy(5, 1));
131         panel.add(buildVerticalPanel("2.54cm"), cc.xy(5, 3));
132         
133         panel.add(new JLabel("1 in"), cc.xy(7, 1));
134         panel.add(buildVerticalPanel("1in"), cc.xy(7, 3));
135
136         panel.add(new JLabel("72 px"), cc.xy(9, 1));
137         panel.add(buildVerticalPanel("72px"), cc.xy(9, 3));
138         
139         panel.add(new JLabel("96 px"), cc.xy(11, 1));
140         panel.add(buildVerticalPanel("96px"), cc.xy(11, 3));
141         
142         panel.add(new JLabel("120 px"), cc.xy(13, 1));
143         panel.add(buildVerticalPanel("120px"), cc.xy(13, 3));
144         
145         return panel;
146     }
147     
148     private JComponent buildVerticalDluPanel() {
149         FormLayout layout = new FormLayout(
150             "c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p",
151             "pref, 6dlu, top:pref, 25dlu, pref, 6dlu, top:pref");
152             
153         JPanel panel = new JPanel(layout);
154         panel.setBorder(Borders.DIALOG_BORDER);
155         CellConstraints cc = new CellConstraints();
156
157         panel.add(new JLabel("4 rows"), cc.xy(1, 1));
158         panel.add(buildVerticalPanel("pref", 4), cc.xy(1, 3));
159         
160         panel.add(new JLabel("42 dlu"), cc.xy(3, 1));
161         panel.add(buildVerticalPanel("42dlu", 4), cc.xy(3, 3));
162         
163         panel.add(new JLabel("57 px"), cc.xy(5, 1));
164         panel.add(buildVerticalPanel("57px", 4), cc.xy(5, 3));
165         
166         panel.add(new JLabel("63 px"), cc.xy(7, 1));
167         panel.add(buildVerticalPanel("63px", 4), cc.xy(7, 3));
168         
169         panel.add(new JLabel("68 px"), cc.xy(9, 1));
170         panel.add(buildVerticalPanel("68px", 4), cc.xy(9, 3));
171         
172
173         panel.add(new JLabel("field"), cc.xy(1, 5));
174         panel.add(new JTextField(4), cc.xy(1, 7));
175         
176         panel.add(new JLabel("14 dlu"), cc.xy(3, 5));
177         panel.add(buildVerticalPanel("14dlu"), cc.xy(3, 7));
178         
179         panel.add(new JLabel("21 px"), cc.xy(5, 5));
180         panel.add(buildVerticalPanel("21px"), cc.xy(5, 7));
181         
182         panel.add(new JLabel("23 px"), cc.xy(7, 5));
183         panel.add(buildVerticalPanel("23px"), cc.xy(7, 7));
184         
185         panel.add(new JLabel("24 px"), cc.xy(9, 5));
186         panel.add(buildVerticalPanel("24px"), cc.xy(9, 7));
187         
188         panel.add(new JLabel("button"), cc.xy(11, 5));
189         panel.add(new JButton("..."), cc.xy(11, 7));
190         
191         return panel;
192     }
193     
194     
195     
196     // Component Creation *****************************************************
197

198     private JComponent createTextArea(int rows, int cols) {
199         JTextArea area = new JTextArea(rows, cols);
200         //area.setText(text);
201
return new JScrollPane(area,
202                     ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
203                     ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
204     }
205     
206     // Helper Code ************************************************************
207

208     private JComponent buildHorizontalPanel(String JavaDoc width) {
209         FormLayout layout = new FormLayout(width, "pref");
210         JPanel panel = new JPanel(layout);
211         panel.add(new JTextField(), new CellConstraints(1, 1));
212         return panel;
213     }
214     
215     private JComponent buildHorizontalPanel(int columns) {
216         FormLayout layout = new FormLayout("pref, 4dlu, pref", "pref");
217         JPanel panel = new JPanel(layout);
218         CellConstraints cc = new CellConstraints();
219         panel.add(new JTextField(columns),
220                   cc.xy(1, 1));
221         panel.add(new JLabel("Width of new JTextField(" + columns + ")"),
222                   cc.xy(3, 1));
223         return panel;
224     }
225     
226     private JComponent buildVerticalPanel(String JavaDoc height) {
227         return buildVerticalPanel(height, 10);
228     }
229     
230     private JComponent buildVerticalPanel(String JavaDoc height, int rows) {
231         FormLayout layout = new FormLayout("pref", "fill:"+ height);
232         JPanel panel = new JPanel(layout);
233         CellConstraints cc = new CellConstraints();
234         panel.add(createTextArea(rows, 5), cc.xy(1, 1));
235         return panel;
236     }
237     
238     
239 }
240
Popular Tags