KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > tutorial > basics > SpanExample


1 /*
2  * Copyright (c) 2003 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.forms.tutorial.basics;
32
33 import java.awt.Component JavaDoc;
34
35 import javax.swing.*;
36
37 import com.jgoodies.forms.factories.Borders;
38 import com.jgoodies.forms.layout.CellConstraints;
39 import com.jgoodies.forms.layout.FormLayout;
40
41 /**
42  * Demonstrates how components can span multiple columns and rows.
43  *
44  * @author Karsten Lentzsch
45  * @version $Revision: 1.7 $
46  */

47 public final class SpanExample {
48     
49     public static void main(String JavaDoc[] args) {
50         try {
51             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
52         } catch (Exception JavaDoc e) {
53             // Likely PlasticXP is not in the class path; ignore.
54
}
55         JFrame frame = new JFrame();
56         frame.setTitle("Forms Tutorial :: Span");
57         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
58         JComponent panel = new SpanExample().buildPanel();
59         frame.getContentPane().add(panel);
60         frame.pack();
61         frame.show();
62     }
63
64
65     /**
66      * Builds and answers a tabbed pane with tabs for the column span example
67      * and the row span example.
68      *
69      * @return a tabbed pane that shows horizontal and vertical spans
70      */

71     public JComponent buildPanel() {
72         JTabbedPane tabbedPane = new JTabbedPane();
73         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
74
75         tabbedPane.add("Column Span", buildColumnSpanExample());
76         tabbedPane.add("Row Span", buildRowSpanExample());
77         return tabbedPane;
78     }
79     
80     
81     /**
82      * Builds and answers a panel where a component spans multiple columns.
83      *
84      * @return a panel with a component that spans multiple columns
85      */

86     private JComponent buildColumnSpanExample() {
87         FormLayout layout = new FormLayout(
88             "pref, 8px, 100px, 4px, 200px",
89             "pref, 6px, pref, 6px, pref, 6px, pref");
90             
91         JPanel panel = new JPanel(layout);
92         panel.setBorder(Borders.DIALOG_BORDER);
93         CellConstraints cc = new CellConstraints();
94
95         panel.add(new JLabel("Name:"), cc.xy (1, 1));
96         panel.add(new JTextField(), cc.xywh(3, 1, 3, 1));
97         
98         panel.add(new JLabel("Phone:"), cc.xy (1, 3));
99         panel.add(new JTextField(), cc.xywh(3, 3, 3, 1));
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.xywh(3, 7, 3, 1));
107         
108         return panel;
109     }
110     
111     
112     /**
113      * Builds and answers a panel where a component spans multiple rows.
114      * <p>
115      * This demo method is about layout. The default FocusTraversalPolicy
116      * will lead to a poor focus traversal order: name, notes, phone, fax;
117      * where the order should be: name, phone, fax, notes.
118      * The components are added in the same order as they shall be
119      * traversed by the focus.
120      * Hence, if you set a ContainerOrderFocusTraversalPolicy,
121      * the focus will traverse the fields in the appropriate order.
122      *
123      * @return a panel with a component that spans multiple rows
124      */

125     private JComponent buildRowSpanExample() {
126         FormLayout layout = new FormLayout(
127             "200px, 25px, 200px",
128             "pref, 2px, pref, 9px, " +
129             "pref, 2px, pref, 9px, " +
130             "pref, 2px, pref");
131             
132         JPanel panel = new JPanel(layout);
133         panel.setBorder(Borders.DIALOG_BORDER);
134         CellConstraints cc = new CellConstraints();
135
136         Component JavaDoc addressArea = new JScrollPane(new JTextArea());
137
138         panel.add(new JLabel("Name"), cc.xy (1, 1));
139         panel.add(new JTextField(), cc.xy (1, 3));
140         
141         panel.add(new JLabel("Phone"), cc.xy (1, 5));
142         panel.add(new JTextField(), cc.xy (1, 7));
143         
144         panel.add(new JLabel("Fax"), cc.xy (1, 9));
145         panel.add(new JTextField(), cc.xy (1, 11));
146         
147         panel.add(new JLabel("Notes"), cc.xy (3, 1));
148         panel.add(addressArea, cc.xywh(3, 3, 1, 9));
149         
150         return panel;
151     }
152     
153     
154 }
155
156
Popular Tags