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.factories.Borders;
036: import com.jgoodies.forms.layout.CellConstraints;
037: import com.jgoodies.forms.layout.FormLayout;
038:
039: /**
040: * Demonstrates the basic FormLayout sizes: constant, minimum, preferred.
041: *
042: * @author Karsten Lentzsch
043: * @version $Revision: 1.12 $
044: */
045: public final class BasicSizesExample {
046:
047: public static void main(String[] args) {
048: try {
049: UIManager
050: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
051: } catch (Exception e) {
052: // Likely PlasticXP is not in the class path; ignore.
053: }
054: JFrame frame = new JFrame();
055: frame.setTitle("Forms Tutorial :: Basic Sizes");
056: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
057: JComponent panel = new BasicSizesExample().buildPanel();
058: frame.getContentPane().add(panel);
059: frame.pack();
060: frame.setVisible(true);
061: }
062:
063: public JComponent buildPanel() {
064: JTabbedPane tabbedPane = new JTabbedPane();
065: tabbedPane.putClientProperty("jgoodies.noContentBorder",
066: Boolean.TRUE);
067:
068: tabbedPane.add("Horizontal", buildHorizontalSizesPanel());
069: tabbedPane.add("Vertical", buildVerticalSizesPanel());
070: return tabbedPane;
071: }
072:
073: private JComponent buildHorizontalSizesPanel() {
074: FormLayout layout = new FormLayout("pref, 12px, "
075: + "75px, 25px, min, 25px, pref", "pref, 12px, pref");
076:
077: // Create a panel that uses the layout.
078: JPanel panel = new JPanel(layout);
079:
080: // Set a default border.
081: panel.setBorder(Borders.DIALOG_BORDER);
082:
083: // Create a reusable CellConstraints instance.
084: CellConstraints cc = new CellConstraints();
085:
086: // Add components to the panel.
087: panel.add(new JLabel("75px"), cc.xy(3, 1));
088: panel.add(new JLabel("Min"), cc.xy(5, 1));
089: panel.add(new JLabel("Pref"), cc.xy(7, 1));
090:
091: panel.add(new JLabel("new JTextField(15)"), cc.xy(1, 3));
092:
093: panel.add(new JTextField(15), cc.xy(3, 3));
094: panel.add(new JTextField(15), cc.xy(5, 3));
095: panel.add(new JTextField(15), cc.xy(7, 3));
096:
097: return panel;
098: }
099:
100: private JComponent buildVerticalSizesPanel() {
101: FormLayout layout = new FormLayout("pref, 12px, pref",
102: "pref, 12px, 45px, 12px, min, 12px, pref");
103:
104: // Create a panel that uses the layout.
105: JPanel panel = new JPanel(layout);
106:
107: // Set a default border.
108: panel.setBorder(Borders.DIALOG_BORDER);
109:
110: // Create a reusable CellConstraints instance.
111: CellConstraints cc = new CellConstraints();
112:
113: // Add components to the panel.
114: panel.add(new JLabel("new JTextArea(10, 40)"), cc.xy(3, 1));
115:
116: panel.add(new JLabel("45px"), cc.xy(1, 3));
117: panel.add(new JLabel("Min"), cc.xy(1, 5));
118: panel.add(new JLabel("Pref"), cc.xy(1, 7));
119:
120: panel.add(createTextArea(10, 40), cc.xy(3, 3));
121: panel.add(createTextArea(10, 40), cc.xy(3, 5));
122: panel.add(createTextArea(10, 40), cc.xy(3, 7));
123:
124: return panel;
125: }
126:
127: private JComponent createTextArea(int rows, int cols) {
128: return new JScrollPane(new JTextArea(rows, cols),
129: ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
130: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
131: }
132:
133: // Helper Classes *********************************************************
134:
135: /**
136: * Creates and returns a button that can have predefined minimum
137: * and preferred sizes. In the constructor you can specify or omit
138: * the minimum width, height and preferred width/height.
139: */
140: // private static class SpecialSizeButton extends JButton {
141: //
142: // private final Dimension fixedMinimumSize;
143: // private final Dimension fixedPreferredSize;
144: //
145: // private SpecialSizeButton(
146: // String text,
147: // Dimension fixedMinimumSize,
148: // Dimension fixedPreferredSize) {
149: // super(text);
150: // this.fixedMinimumSize = fixedMinimumSize;
151: // this.fixedPreferredSize = fixedPreferredSize;
152: // //putClientProperty("jgoodies.isNarrow", Boolean.TRUE);
153: // }
154: //
155: // public Dimension getMinimumSize() {
156: // Dimension d = super.getMinimumSize();
157: // return new Dimension(
158: // fixedMinimumSize.width == -1
159: // ? d.width
160: // : fixedMinimumSize.width,
161: // fixedMinimumSize.height == -1
162: // ? d.height
163: // : fixedMinimumSize.height);
164: // }
165: //
166: // public Dimension getPreferredSize() {
167: // Dimension d = super.getPreferredSize();
168: // return new Dimension(
169: // fixedPreferredSize.width == -1
170: // ? d.width
171: // : fixedPreferredSize.width,
172: // fixedPreferredSize.height == -1
173: // ? d.height
174: // : fixedPreferredSize.height);
175: // }
176: // }
177:
178: }
|