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.Component;
034:
035: import javax.swing.*;
036:
037: import com.jgoodies.forms.factories.Borders;
038: import com.jgoodies.forms.layout.CellConstraints;
039: import com.jgoodies.forms.layout.FormLayout;
040:
041: /**
042: * Demonstrates how components can span multiple columns and rows.
043: *
044: * @author Karsten Lentzsch
045: * @version $Revision: 1.16 $
046: */
047: public final class SpanExample {
048:
049: public static void main(String[] args) {
050: try {
051: UIManager
052: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
053: } catch (Exception e) {
054: // Likely PlasticXP is not in the class path; ignore.
055: }
056: JFrame frame = new JFrame();
057: frame.setTitle("Forms Tutorial :: Span");
058: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
059: JComponent panel = new SpanExample().buildPanel();
060: frame.getContentPane().add(panel);
061: frame.pack();
062: frame.setVisible(true);
063: }
064:
065: /**
066: * Builds and returns a tabbed pane with tabs for the column span example
067: * and the row span example.
068: *
069: * @return a tabbed pane that shows horizontal and vertical spans
070: */
071: public JComponent buildPanel() {
072: JTabbedPane tabbedPane = new JTabbedPane();
073: tabbedPane.putClientProperty("jgoodies.noContentBorder",
074: Boolean.TRUE);
075:
076: tabbedPane.add("Column Span", buildColumnSpanExample());
077: tabbedPane.add("Row Span", buildRowSpanExample());
078: return tabbedPane;
079: }
080:
081: /**
082: * Builds and returns a panel where a component spans multiple columns.
083: *
084: * @return a panel with a component that spans multiple columns
085: */
086: private JComponent buildColumnSpanExample() {
087: FormLayout layout = new FormLayout(
088: "pref, 8px, 100px, 4px, 200px",
089: "pref, 6px, pref, 6px, pref, 6px, pref");
090:
091: JPanel panel = new JPanel(layout);
092: panel.setBorder(Borders.DIALOG_BORDER);
093: CellConstraints cc = new CellConstraints();
094:
095: panel.add(new JLabel("Name:"), cc.xy(1, 1));
096: panel.add(new JTextField(), cc.xyw(3, 1, 3));
097:
098: panel.add(new JLabel("Phone:"), cc.xy(1, 3));
099: panel.add(new JTextField(), cc.xyw(3, 3, 3));
100:
101: panel.add(new JLabel("ZIP/City:"), cc.xy(1, 5));
102: panel.add(new JTextField(), cc.xy(3, 5));
103: panel.add(new JTextField(), cc.xy(5, 5));
104:
105: panel.add(new JLabel("Country:"), cc.xy(1, 7));
106: panel.add(new JTextField(), cc.xyw(3, 7, 3));
107:
108: return panel;
109: }
110:
111: /**
112: * Builds and returns a panel where a component spans multiple rows.<p>
113: *
114: * This demo method is about layout. The default FocusTraversalPolicy
115: * will lead to a poor focus traversal order: name, notes, phone, fax;
116: * where the order should be: name, phone, fax, notes.
117: * The components are added in the same order as they shall be
118: * traversed by the focus.
119: * Hence, if you set a ContainerOrderFocusTraversalPolicy,
120: * the focus will traverse the fields in the appropriate order.
121: *
122: * @return a panel with a component that spans multiple rows
123: */
124: private JComponent buildRowSpanExample() {
125: FormLayout layout = new FormLayout("200px, 25px, 200px",
126: "pref, 2px, pref, 9px, " + "pref, 2px, pref, 9px, "
127: + "pref, 2px, pref");
128:
129: JPanel panel = new JPanel(layout);
130: panel.setBorder(Borders.DIALOG_BORDER);
131: CellConstraints cc = new CellConstraints();
132:
133: Component addressArea = new JScrollPane(new JTextArea());
134:
135: panel.add(new JLabel("Name"), cc.xy(1, 1));
136: panel.add(new JTextField(), cc.xy(1, 3));
137:
138: panel.add(new JLabel("Phone"), cc.xy(1, 5));
139: panel.add(new JTextField(), cc.xy(1, 7));
140:
141: panel.add(new JLabel("Fax"), cc.xy(1, 9));
142: panel.add(new JTextField(), cc.xy(1, 11));
143:
144: panel.add(new JLabel("Notes"), cc.xy(3, 1));
145: panel.add(addressArea, cc.xywh(3, 3, 1, 9));
146:
147: return panel;
148: }
149:
150: }
|