001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.tutorial.basics;
032:
033: import javax.swing.*;
034:
035: import com.jgoodies.forms.builder.DefaultFormBuilder;
036: import com.jgoodies.forms.factories.Borders;
037: import com.jgoodies.forms.layout.CellConstraints;
038: import com.jgoodies.forms.layout.FormLayout;
039:
040: /**
041: * Demonstrates the different sizing units provided by the FormLayout:
042: * Pixel, Dialog Units (dlu), Point, Millimeter, Centimeter and Inches.
043: * Pt, mm, cm, in honor the screen resolution; dlus honor the font size too.
044: *
045: * @author Karsten Lentzsch
046: * @version $Revision: 1.13 $
047: */
048: public final class UnitsExample {
049:
050: public static void main(String[] args) {
051: try {
052: UIManager
053: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
054: } catch (Exception e) {
055: // Likely PlasticXP is not in the class path; ignore.
056: }
057: JFrame frame = new JFrame();
058: frame.setTitle("Forms Tutorial :: Units");
059: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
060: JComponent panel = new UnitsExample().buildPanel();
061: frame.getContentPane().add(panel);
062: frame.pack();
063: frame.setVisible(true);
064: }
065:
066: public JComponent buildPanel() {
067: JTabbedPane tabbedPane = new JTabbedPane();
068: tabbedPane.putClientProperty("jgoodies.noContentBorder",
069: Boolean.TRUE);
070:
071: tabbedPane.add("Horizontal", buildHorizontalUnitsPanel());
072: tabbedPane.add("Horizontal Dlu", buildHorizontalDluPanel());
073: tabbedPane.add("Vertical", buildVerticalUnitsPanel());
074: tabbedPane.add("Vertical Dlu", buildVerticalDluPanel());
075: return tabbedPane;
076: }
077:
078: private JComponent buildHorizontalUnitsPanel() {
079: FormLayout layout = new FormLayout(
080: "right:pref, 1dlu, left:pref, 4dlu, left:pref", "");
081: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
082: builder.setDefaultDialogBorder();
083:
084: builder.append("72", new JLabel("pt"),
085: buildHorizontalPanel("72pt"));
086: builder.append("25.4", new JLabel("mm"),
087: buildHorizontalPanel("25.4mm"));
088: builder.append("2.54", new JLabel("cm"),
089: buildHorizontalPanel("2.54cm"));
090: builder.append("1", new JLabel("in"),
091: buildHorizontalPanel("1in"));
092: builder.append("72", new JLabel("px"),
093: buildHorizontalPanel("72px"));
094: builder.append("96", new JLabel("px"),
095: buildHorizontalPanel("96px"));
096: builder.append("120", new JLabel("px"),
097: buildHorizontalPanel("120px"));
098:
099: return builder.getPanel();
100: }
101:
102: private JComponent buildHorizontalDluPanel() {
103: FormLayout layout = new FormLayout(
104: "right:pref, 1dlu, left:pref, 4dlu, left:pref", "");
105: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
106: builder.setDefaultDialogBorder();
107:
108: builder
109: .append("9", new JLabel("cols"),
110: buildHorizontalPanel(9));
111: builder.append("50", new JLabel("dlu"),
112: buildHorizontalPanel("50dlu"));
113: builder.append("75", new JLabel("px"),
114: buildHorizontalPanel("75px"));
115: builder.append("88", new JLabel("px"),
116: buildHorizontalPanel("88px"));
117: builder.append("100", new JLabel("px"),
118: buildHorizontalPanel("100px"));
119:
120: return builder.getPanel();
121: }
122:
123: private JComponent buildVerticalUnitsPanel() {
124: FormLayout layout = new FormLayout(
125: "c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p",
126: "pref, 6dlu, top:pref");
127:
128: JPanel panel = new JPanel(layout);
129: panel.setBorder(Borders.DIALOG_BORDER);
130: CellConstraints cc = new CellConstraints();
131:
132: panel.add(new JLabel("72 pt"), cc.xy(1, 1));
133: panel.add(buildVerticalPanel("72pt"), cc.xy(1, 3));
134:
135: panel.add(new JLabel("25.4 mm"), cc.xy(3, 1));
136: panel.add(buildVerticalPanel("25.4mm"), cc.xy(3, 3));
137:
138: panel.add(new JLabel("2.54 cm"), cc.xy(5, 1));
139: panel.add(buildVerticalPanel("2.54cm"), cc.xy(5, 3));
140:
141: panel.add(new JLabel("1 in"), cc.xy(7, 1));
142: panel.add(buildVerticalPanel("1in"), cc.xy(7, 3));
143:
144: panel.add(new JLabel("72 px"), cc.xy(9, 1));
145: panel.add(buildVerticalPanel("72px"), cc.xy(9, 3));
146:
147: panel.add(new JLabel("96 px"), cc.xy(11, 1));
148: panel.add(buildVerticalPanel("96px"), cc.xy(11, 3));
149:
150: panel.add(new JLabel("120 px"), cc.xy(13, 1));
151: panel.add(buildVerticalPanel("120px"), cc.xy(13, 3));
152:
153: return panel;
154: }
155:
156: private JComponent buildVerticalDluPanel() {
157: FormLayout layout = new FormLayout(
158: "c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p, 4dlu, c:p",
159: "pref, 6dlu, top:pref, 25dlu, pref, 6dlu, top:pref");
160:
161: JPanel panel = new JPanel(layout);
162: panel.setBorder(Borders.DIALOG_BORDER);
163: CellConstraints cc = new CellConstraints();
164:
165: panel.add(new JLabel("4 rows"), cc.xy(1, 1));
166: panel.add(buildVerticalPanel("pref", 4), cc.xy(1, 3));
167:
168: panel.add(new JLabel("42 dlu"), cc.xy(3, 1));
169: panel.add(buildVerticalPanel("42dlu", 4), cc.xy(3, 3));
170:
171: panel.add(new JLabel("57 px"), cc.xy(5, 1));
172: panel.add(buildVerticalPanel("57px", 4), cc.xy(5, 3));
173:
174: panel.add(new JLabel("63 px"), cc.xy(7, 1));
175: panel.add(buildVerticalPanel("63px", 4), cc.xy(7, 3));
176:
177: panel.add(new JLabel("68 px"), cc.xy(9, 1));
178: panel.add(buildVerticalPanel("68px", 4), cc.xy(9, 3));
179:
180: panel.add(new JLabel("field"), cc.xy(1, 5));
181: panel.add(new JTextField(4), cc.xy(1, 7));
182:
183: panel.add(new JLabel("14 dlu"), cc.xy(3, 5));
184: panel.add(buildVerticalPanel("14dlu"), cc.xy(3, 7));
185:
186: panel.add(new JLabel("21 px"), cc.xy(5, 5));
187: panel.add(buildVerticalPanel("21px"), cc.xy(5, 7));
188:
189: panel.add(new JLabel("23 px"), cc.xy(7, 5));
190: panel.add(buildVerticalPanel("23px"), cc.xy(7, 7));
191:
192: panel.add(new JLabel("24 px"), cc.xy(9, 5));
193: panel.add(buildVerticalPanel("24px"), cc.xy(9, 7));
194:
195: panel.add(new JLabel("button"), cc.xy(11, 5));
196: panel.add(new JButton("..."), cc.xy(11, 7));
197:
198: return panel;
199: }
200:
201: // Component Creation *****************************************************
202:
203: private JComponent createTextArea(int rows, int cols) {
204: JTextArea area = new JTextArea(rows, cols);
205: //area.setText(text);
206: return new JScrollPane(area,
207: ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
208: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
209: }
210:
211: // Helper Code ************************************************************
212:
213: private JComponent buildHorizontalPanel(String width) {
214: FormLayout layout = new FormLayout(width, "pref");
215: JPanel panel = new JPanel(layout);
216: panel.add(new JTextField(), new CellConstraints(1, 1));
217: return panel;
218: }
219:
220: private JComponent buildHorizontalPanel(int columns) {
221: FormLayout layout = new FormLayout("pref, 4dlu, pref", "pref");
222: JPanel panel = new JPanel(layout);
223: CellConstraints cc = new CellConstraints();
224: panel.add(new JTextField(columns), cc.xy(1, 1));
225: panel.add(
226: new JLabel("Width of new JTextField(" + columns + ")"),
227: cc.xy(3, 1));
228: return panel;
229: }
230:
231: private JComponent buildVerticalPanel(String height) {
232: return buildVerticalPanel(height, 10);
233: }
234:
235: private JComponent buildVerticalPanel(String height, int rows) {
236: FormLayout layout = new FormLayout("pref", "fill:" + height);
237: JPanel panel = new JPanel(layout);
238: CellConstraints cc = new CellConstraints();
239: panel.add(createTextArea(rows, 5), cc.xy(1, 1));
240: return panel;
241: }
242:
243: }
|