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 java.awt.Insets;
034:
035: import javax.swing.*;
036: import javax.swing.border.EmptyBorder;
037:
038: import com.jgoodies.forms.builder.DefaultFormBuilder;
039: import com.jgoodies.forms.builder.PanelBuilder;
040: import com.jgoodies.forms.layout.CellConstraints;
041: import com.jgoodies.forms.layout.FormLayout;
042:
043: /**
044: * Demonstrates the three FormLayout component sizes: minimum, default and
045: * preferred.
046: * Min and Pref measure the components minimum and preferred size, where the
047: * Default size behaves like Pref but shrinks if the container space is scarce.
048: *
049: * @author Karsten Lentzsch
050: * @version $Revision: 1.13 $
051: */
052: public final class ComponentSizesExample {
053:
054: public static void main(String[] args) {
055: try {
056: UIManager
057: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
058: } catch (Exception e) {
059: // Likely PlasticXP is not in the class path; ignore.
060: }
061: JFrame frame = new JFrame();
062: frame.setTitle("Forms Tutorial :: Component Sizes");
063: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
064: JComponent panel = new ComponentSizesExample().buildPanel();
065: frame.getContentPane().add(panel);
066: frame.pack();
067: frame.setVisible(true);
068: }
069:
070: public JComponent buildPanel() {
071: JSplitPane splitPane = new JSplitPane(
072: JSplitPane.HORIZONTAL_SPLIT, true,
073: buildCombinedPanel(), buildTextPanel());
074: return splitPane;
075: }
076:
077: /**
078: * Builds and answer the panel that combines the three sizing panels.
079: *
080: * @return the combined panel
081: */
082: private JComponent buildCombinedPanel() {
083: FormLayout layout = new FormLayout("30dlu:grow",
084: "pref, 3dlu, pref, 3dlu, pref");
085:
086: PanelBuilder builder = new PanelBuilder(layout);
087: builder.setDefaultDialogBorder();
088:
089: CellConstraints cc = new CellConstraints();
090:
091: builder.add(buildMinimumSizePanel(), cc.xy(1, 1));
092: builder.add(buildDefaultSizePanel(), cc.xy(1, 3));
093: builder.add(buildPreferredSizePanel(), cc.xy(1, 5));
094:
095: return builder.getPanel();
096: }
097:
098: private JComponent buildMinimumSizePanel() {
099: FormLayout layout = new FormLayout(
100: "right:max(25dlu;pref), 3dlu, min", "pref");
101: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
102: builder.append("Min", new JTextField(15));
103: return builder.getPanel();
104: }
105:
106: private JComponent buildDefaultSizePanel() {
107: FormLayout layout = new FormLayout(
108: "right:max(25dlu;pref), 3dlu, default", "pref");
109: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
110: builder.append("Default", new JTextField(15));
111: return builder.getPanel();
112: }
113:
114: private JComponent buildPreferredSizePanel() {
115: FormLayout layout = new FormLayout(
116: "right:max(25dlu;pref), 3dlu, pref", "pref");
117: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
118: builder.append("Pref", new JTextField(15));
119: return builder.getPanel();
120: }
121:
122: private JComponent buildTextPanel() {
123: JTextArea textArea = new JTextArea(5, 20);
124: textArea.setMargin(new Insets(6, 10, 4, 6));
125: // Non-editable but shall use the editable background.
126: // textArea.setEditable(false);
127: textArea.putClientProperty("JTextArea.infoBackground",
128: Boolean.TRUE);
129: textArea
130: .setText("The text field used in the example on the left\n"
131: + "has a narrow minimum width and a wider preferred width.\n\n"
132: + "If you move the split divider to the left and right\n"
133: + "you can see how 'Default' shrinks the field if space is scarce.\n\n"
134: + "If there's not enough space for the preferred width\n"
135: + "the bottom field will be 'cut' on the right-hand side.");
136: JScrollPane scrollpane = new JScrollPane(textArea);
137: scrollpane.setBorder(new EmptyBorder(0, 0, 0, 0));
138: return scrollpane;
139: }
140:
141: }
|