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.building;
032:
033: import javax.swing.*;
034:
035: import com.jgoodies.forms.builder.DefaultFormBuilder;
036: import com.jgoodies.forms.layout.CellConstraints;
037: import com.jgoodies.forms.layout.FormLayout;
038: import com.jgoodies.forms.layout.RowSpec;
039:
040: /**
041: * Compares approaches how to append a custom area at the end of
042: * a panel built with the DefaultFormBuilder:<ol>
043: * <li> using two custom rows to align the leading label,
044: * <li> using a single custom row with label on top,
045: * <li> using a separator.
046: * </ol>
047: * These differ in the position of the leading 'Feedback" label,
048: * and in turn in the alignment of font baselines between label
049: * and the text area.
050: *
051: * @author Karsten Lentzsch
052: * @version $Revision: 1.13 $
053: *
054: * @see DefaultFormBuilder
055: * @see DefaultFormWithCustomRowsExample
056: */
057:
058: public final class DefaultFormWithCustomAreasExample {
059:
060: public static void main(String[] args) {
061: try {
062: UIManager
063: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
064: } catch (Exception e) {
065: // Likely PlasticXP is not in the class path; ignore.
066: }
067: JFrame frame = new JFrame();
068: frame.setTitle("Forms Tutorial :: Custom Areas");
069: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
070: JComponent panel = new DefaultFormWithCustomAreasExample()
071: .buildPanel();
072: frame.getContentPane().add(panel);
073: frame.pack();
074: frame.setVisible(true);
075: }
076:
077: // Building ***************************************************************
078:
079: public JComponent buildPanel() {
080: JTabbedPane tabbedPane = new JTabbedPane();
081: tabbedPane.putClientProperty("jgoodies.noContentBorder",
082: Boolean.TRUE);
083:
084: tabbedPane.add(buildCustomAreaWithAlignedLabelPanel(),
085: "Aligned label");
086: tabbedPane.add(buildCustomAreaWithTopLabelPanel(), "Top label");
087: tabbedPane
088: .add(buildCustomAreaWithSeparatorPanel(), "Separator");
089: return tabbedPane;
090: }
091:
092: private DefaultFormBuilder buildPanelHeader() {
093: // Column specs only, rows will be added dynamically.
094: FormLayout layout = new FormLayout("right:pref, 3dlu, min:grow");
095: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
096: builder.setDefaultDialogBorder();
097: builder.setRowGroupingEnabled(true);
098:
099: builder.appendSeparator("Customer Data");
100: builder.append("Last Name", new JTextField());
101: builder.append("First Name", new JTextField());
102: builder.append("Street", new JTextField());
103: builder.append("Email", new JTextField());
104:
105: return builder;
106: }
107:
108: /**
109: * Demonstrates how to append a larger custom area at the end of
110: * a panel that is build with a {@link DefaultFormBuilder}.<p>
111: *
112: * We add a gap and a single custom row that grows and that
113: * is filled vertically (where the default is center vertically).
114: * The area uses a standard leading label.
115: *
116: * @return the custom area panel with aligned labels
117: */
118: private JComponent buildCustomAreaWithAlignedLabelPanel() {
119: DefaultFormBuilder builder = buildPanelHeader();
120:
121: CellConstraints cc = new CellConstraints();
122: builder.append("Feedback");
123: builder.appendRow(new RowSpec("0:grow"));
124: builder.add(new JScrollPane(new JTextArea(
125: "Feedback - font baselines shall be aligned")), cc
126: .xywh(builder.getColumn(), builder.getRow(), 1, 2,
127: "fill, fill"));
128:
129: return builder.getPanel();
130: }
131:
132: /**
133: * Demonstrates how to append two custom areas at the end of
134: * a panel that is build with a DefaultFormBuilder.
135: *
136: * @return the custom area panel with label in the top
137: */
138: private JComponent buildCustomAreaWithTopLabelPanel() {
139: DefaultFormBuilder builder = buildPanelHeader();
140:
141: CellConstraints cc = new CellConstraints();
142: builder.appendRow(builder.getLineGapSpec());
143: builder.appendRow(new RowSpec("top:28dlu:grow"));
144: builder.nextLine(2);
145: builder.append("Feedback");
146: builder.add(new JScrollPane(new JTextArea(
147: "Feedback - likely the baselines are not aligned")),
148: cc.xy(builder.getColumn(), builder.getRow(),
149: "fill, fill"));
150:
151: return builder.getPanel();
152: }
153:
154: /**
155: * Demonstrates how to append a larger custom area at the end of
156: * a panel that is build with a DefaultFormBuilder.<p>
157: *
158: * We add a gap and a single custom row that grows and that
159: * is filled vertically (where the default is center vertically).
160: * The area is separated by a titled separator and it is indented
161: * using an empty leading label.
162: *
163: * @return the custom area panel with separators
164: */
165: private JComponent buildCustomAreaWithSeparatorPanel() {
166: DefaultFormBuilder builder = buildPanelHeader();
167:
168: builder.appendSeparator("Customer Feedback");
169: builder.appendRow(builder.getLineGapSpec());
170: builder.appendRow(new RowSpec("fill:28dlu:grow"));
171: builder.nextLine(2);
172: builder.append("", new JScrollPane(new JTextArea()));
173:
174: return builder.getPanel();
175: }
176:
177: }
|