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: * Shows three approaches how to add custom rows to a form that is built
042: * using a DefaultFormBuilder.<ol>
043: * <li> single custom row,
044: * <li> standard + custom row,
045: * <li> multiple standard rows.
046: * </ol>
047: * These differ in the position of the 'Comment' label, the alignment
048: * of font baselines and the height of the custom row.
049: *
050: * @author Karsten Lentzsch
051: * @version $Revision: 1.12 $
052: *
053: * @see DefaultFormBuilder
054: * @see DefaultFormWithCustomAreasExample
055: */
056:
057: public final class DefaultFormWithCustomRowsExample {
058:
059: private JTextField name1Field;
060: private JTextArea comment1Area;
061: private JTextField name2Field;
062: private JTextArea comment2Area;
063: private JTextField name3Field;
064: private JTextArea comment3Area;
065:
066: public static void main(String[] args) {
067: try {
068: UIManager
069: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
070: } catch (Exception e) {
071: // Likely PlasticXP is not in the class path; ignore.
072: }
073: JFrame frame = new JFrame();
074: frame.setTitle("Forms Tutorial :: Custom Rows");
075: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
076: JComponent panel = new DefaultFormWithCustomRowsExample()
077: .buildPanel();
078: frame.getContentPane().add(panel);
079: frame.pack();
080: frame.setVisible(true);
081: }
082:
083: // Component Creation and Initialization **********************************
084:
085: /**
086: * Creates and intializes the UI components.
087: */
088: private void initComponents() {
089: name1Field = new JTextField(
090: "Name - font baselines shall be aligned");
091: name2Field = new JTextField(
092: "Name - font baselines shall be aligned");
093: name3Field = new JTextField(
094: "Name - font baselines shall be aligned");
095: comment1Area = new JTextArea(2, 20);
096: comment2Area = new JTextArea(2, 20);
097: comment3Area = new JTextArea(2, 20);
098: comment1Area
099: .setText("Comment - likely baselines are unaligned");
100: comment2Area.setText("Comment - baselines shall be aligned");
101: comment3Area.setText("Comment - baselines shall be aligned");
102: }
103:
104: // Building ***************************************************************
105:
106: /**
107: * Demonstrates three different ways to add custom rows to a form
108: * that is build with a {@link DefaultFormBuilder}.
109: *
110: * @return the built panel
111: */
112: public JComponent buildPanel() {
113: initComponents();
114:
115: // Column specs only, rows will be added dynamically.
116: FormLayout layout = new FormLayout("right:pref, 3dlu, min:grow");
117: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
118: builder.setDefaultDialogBorder();
119: builder.setRowGroupingEnabled(true);
120:
121: CellConstraints cc = new CellConstraints();
122:
123: // In this approach, we add a gap and a custom row.
124: // The advantage of this approach is, that we can express
125: // the row spec and comment area cell constraints freely.
126: // The disadvantage is the misalignment of the leading label.
127: // Also the row's height may be inconsistent with other rows.
128: builder.appendSeparator("Single Custom Row");
129: builder.append("Name", name1Field);
130: builder.appendRow(builder.getLineGapSpec());
131: builder.appendRow(new RowSpec("top:31dlu")); // Assumes line is 14, gap is 3
132: builder.nextLine(2);
133: builder.append("Comment");
134: builder.add(new JScrollPane(comment1Area), cc.xy(builder
135: .getColumn(), builder.getRow(), "fill, fill"));
136: builder.nextLine();
137:
138: // In this approach, we append a standard row with gap before it.
139: // The advantage is, that the leading label is aligned well.
140: // The disadvantage is that the comment area now spans
141: // multiple cells and is slightly less flexible.
142: // Also the row's height may be inconsistent with other rows.
143: builder.appendSeparator("Standard + Custom Row");
144: builder.append("Name", name2Field);
145: builder.append("Comment");
146: builder.appendRow(new RowSpec("17dlu")); // Assumes line is 14, gap is 3
147: builder.add(new JScrollPane(comment2Area), cc.xywh(builder
148: .getColumn(), builder.getRow(), 1, 2));
149: builder.nextLine(2);
150:
151: // In this approach, we append two standard rows with associated gaps.
152: // The advantage is, that the leading label is aligned well,
153: // and the height is consistent with other rows.
154: // The disadvantage is that the comment area now spans
155: // multiple cells and is slightly less flexible.
156: builder.appendSeparator("Two Standard Rows");
157: builder.append("Name", name3Field);
158: builder.append("Comment");
159: builder.nextLine();
160: builder.append("");
161: builder.nextRow(-2);
162: builder.add(new JScrollPane(comment3Area), cc.xywh(builder
163: .getColumn(), builder.getRow(), 1, 3));
164:
165: return builder.getPanel();
166: }
167:
168: }
|