2
* Copyright (c) 2002-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
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.
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.
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.
31
package com.jgoodies.forms.tutorial.basics;
35
import com.jgoodies.forms.factories.Borders;
36
import com.jgoodies.forms.layout.CellConstraints;
37
import com.jgoodies.forms.layout.FormLayout;
40
* Demonstrates how FormLayout applies the default column and row
41
* alignments to cells, and how to override the defaults.
43
* @author Karsten Lentzsch
44
* @version $Revision: 1.9 $
46
public final class CellAlignmentExample {
49
public static void main(String[] args) {
51
UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
52
} catch (Exception e) {
53
// Likely PlasticXP is not in the class path; ignore.
55
JFrame frame = new JFrame();
56
frame.setTitle("Forms Tutorial :: Cell Alignments");
57
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
58
JComponent panel = new CellAlignmentExample().buildPanel();
59
frame.getContentPane().add(panel);
61
frame.setVisible(true);
65
public JComponent buildPanel() {
66
JTabbedPane tabbedPane = new JTabbedPane();
67
tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
69
tabbedPane.add(buildHorizontalPanel(), "Horizontal");
70
tabbedPane.add(buildVerticalPanel(), "Vertical");
75
private JComponent buildHorizontalPanel() {
76
FormLayout layout = new FormLayout(
77
"r:p, 4dlu, left:pref:g, center:pref:g, right:pref:g, pref:g",
78
"pref, 8dlu, pref, pref, pref, pref, pref");
79
JPanel panel = new JPanel(layout);
80
panel.setBorder(Borders.DIALOG_BORDER);
82
panel.add(new JLabel("Column Spec: "), "1, 1, r, c");
83
panel.add(new JLabel(" \"left:pref:grow\" "), "3, 1, c, c");
84
panel.add(new JLabel(" \"center:pref:grow\" "), "4, 1, c, c");
85
panel.add(new JLabel(" \"right:pref:grow\" "), "5, 1, c, c");
86
panel.add(new JLabel(" \"pref:grow\" "), "6, 1, c, c");
89
addHorizontalButton(panel, 3, row, CellConstraints.DEFAULT);
90
addHorizontalButton(panel, 4, row, CellConstraints.DEFAULT);
91
addHorizontalButton(panel, 5, row, CellConstraints.DEFAULT);
92
addHorizontalButton(panel, 6, row, CellConstraints.DEFAULT);
95
addHorizontalButton(panel, 3, row, CellConstraints.FILL);
96
addHorizontalButton(panel, 4, row, CellConstraints.FILL);
97
addHorizontalButton(panel, 5, row, CellConstraints.FILL);
98
addHorizontalButton(panel, 6, row, CellConstraints.FILL);
101
addHorizontalButton(panel, 3, row, CellConstraints.LEFT);
102
addHorizontalButton(panel, 4, row, CellConstraints.LEFT);
103
addHorizontalButton(panel, 5, row, CellConstraints.LEFT);
104
addHorizontalButton(panel, 6, row, CellConstraints.LEFT);
107
addHorizontalButton(panel, 3, row, CellConstraints.CENTER);
108
addHorizontalButton(panel, 4, row, CellConstraints.CENTER);
109
addHorizontalButton(panel, 5, row, CellConstraints.CENTER);
110
addHorizontalButton(panel, 6, row, CellConstraints.CENTER);
113
addHorizontalButton(panel, 3, row, CellConstraints.RIGHT);
114
addHorizontalButton(panel, 4, row, CellConstraints.RIGHT);
115
addHorizontalButton(panel, 5, row, CellConstraints.RIGHT);
116
addHorizontalButton(panel, 6, row, CellConstraints.RIGHT);
122
private JComponent buildVerticalPanel() {
123
FormLayout layout = new FormLayout(
124
"left:pref, 8dlu, p, c:p, p, p, p",
125
"p, 4dlu, top:pref:g, center:pref:g, bottom:pref:g, pref:g");
126
layout.setColumnGroups(new int[][] {{3, 5, 6, 7}});
127
JPanel panel = new JPanel(layout);
128
panel.setBorder(Borders.DIALOG_BORDER);
130
panel.add(new JLabel("Row Spec:"), "1, 1, r, c");
131
panel.add(new JLabel("\"top:pref:grow\""), "1, 3, r, c");
132
panel.add(new JLabel("\"center:pref:grow\""), "1, 4, r, c");
133
panel.add(new JLabel("\"bottom:pref:grow\""), "1, 5, r, c");
134
panel.add(new JLabel("\"pref:grow\""), "1, 6, r, c");
137
addVerticalButton(panel, col, 3, CellConstraints.DEFAULT);
138
addVerticalButton(panel, col, 4, CellConstraints.DEFAULT);
139
addVerticalButton(panel, col, 5, CellConstraints.DEFAULT);
140
addVerticalButton(panel, col, 6, CellConstraints.DEFAULT);
143
addVerticalButton(panel, col, 3, CellConstraints.FILL);
144
addVerticalButton(panel, col, 4, CellConstraints.FILL);
145
addVerticalButton(panel, col, 5, CellConstraints.FILL);
146
addVerticalButton(panel, col, 6, CellConstraints.FILL);
149
addVerticalButton(panel, col, 3, CellConstraints.TOP);
150
addVerticalButton(panel, col, 4, CellConstraints.TOP);
151
addVerticalButton(panel, col, 5, CellConstraints.TOP);
152
addVerticalButton(panel, col, 6, CellConstraints.TOP);
155
addVerticalButton(panel, col, 3, CellConstraints.CENTER);
156
addVerticalButton(panel, col, 4, CellConstraints.CENTER);
157
addVerticalButton(panel, col, 5, CellConstraints.CENTER);
158
addVerticalButton(panel, col, 6, CellConstraints.CENTER);
161
addVerticalButton(panel, col, 3, CellConstraints.BOTTOM);
162
addVerticalButton(panel, col, 4, CellConstraints.BOTTOM);
163
addVerticalButton(panel, col, 5, CellConstraints.BOTTOM);
164
addVerticalButton(panel, col, 6, CellConstraints.BOTTOM);
170
private void addHorizontalButton(JPanel panel, int col, int row,
171
CellConstraints.Alignment hAlignment) {
172
JButton button = new JButton(hAlignment.toString());
173
panel.add(button, new CellConstraints(col, row,
175
CellConstraints.DEFAULT));
179
private void addVerticalButton(JPanel panel, int col, int row,
180
CellConstraints.Alignment vAlignment) {
181
JButton button = new JButton(vAlignment.toString());
182
panel.add(button, new CellConstraints(col, row,
183
CellConstraints.DEFAULT,