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.pitfalls;
032:
033: import javax.swing.*;
034: import javax.swing.text.JTextComponent;
035:
036: import com.jgoodies.forms.builder.PanelBuilder;
037: import com.jgoodies.forms.debug.FormDebugUtils;
038: import com.jgoodies.forms.layout.CellConstraints;
039: import com.jgoodies.forms.layout.FormLayout;
040:
041: /**
042: * Demonstrates a frequent pitfall when specifying a growing row.
043: * In this layout a row grows, but the text area in that row is centered
044: * and doesn't 'grow'. In other words, the area doesn't fill
045: * the available vertical space.
046: *
047: * @author Karsten Lentzsch
048: * @version $Revision: 1.7 $
049: */
050:
051: public final class VerticalGrowthExample {
052:
053: // UI Components **********************************************************
054:
055: private JTextComponent notesArea;
056:
057: // Self Launch ************************************************************
058:
059: public static void main(String[] args) {
060: try {
061: UIManager
062: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
063: } catch (Exception e) {
064: // Likely PlasticXP is not in the class path; ignore.
065: }
066: JFrame frame = new JFrame();
067: frame.setTitle("Forms Tutorial :: Vertical Growth");
068: frame
069: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
070: JComponent panel = new VerticalGrowthExample().buildPanel();
071: frame.getContentPane().add(panel);
072: frame.setSize(500, 400);
073: frame.setVisible(true);
074: }
075:
076: // Component Initialization ***********************************************
077:
078: /**
079: * Creates and configures the UI components.
080: */
081: private void initComponents() {
082: notesArea = new JTextArea(
083: "This text area doesn't consume the available vertical space.\n\n"
084: + "The row is specified as 'pref:grow', and so the row grows.\n"
085: + "It's just that the text area doesn't fill the row's vertical space.\n\n"
086: + "Since the row's alignment is not explicitly defined,\n"
087: + "it uses the 'center' alignment as default. But in this case\n"
088: + "we want to 'fill'. The row spec should read: 'fill:pref:grow'.");
089: }
090:
091: // Building ***************************************************************
092:
093: /**
094: * Builds and returns a panel with a title and scrollable text area.<p>
095: *
096: * The FormDebugUtils dumps
097: *
098: * @return the built panel
099: */
100: public JComponent buildPanel() {
101: initComponents();
102:
103: FormLayout layout = new FormLayout("pref:grow",
104: "pref, 3dlu, pref:grow" // Correct: "pref, 3dlu, fill:pref:grow"
105: );
106:
107: PanelBuilder builder = new PanelBuilder(layout);
108: builder.setDefaultDialogBorder();
109: CellConstraints cc = new CellConstraints();
110: builder.addTitle("An Example for FAQ #3.3", cc.xy(1, 1));
111: builder.add(new JScrollPane(notesArea), cc.xy(1, 3));
112:
113: FormDebugUtils.dumpRowSpecs(layout);
114: FormDebugUtils.dumpConstraints(builder.getPanel());
115: return builder.getPanel();
116: }
117:
118: }
|