KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.*;
34
35 import com.jgoodies.forms.factories.Borders;
36 import com.jgoodies.forms.layout.CellConstraints;
37 import com.jgoodies.forms.layout.FormLayout;
38
39 /**
40  * Demonstrates how FormLayout applies the default column and row
41  * alignments to cells, and how to override the defaults.
42  *
43  * @author Karsten Lentzsch
44  * @version $Revision: 1.6 $
45  */

46 public final class CellAlignmentExample {
47
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 :: Cell Alignments");
57         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
58         JComponent panel = new CellAlignmentExample().buildPanel();
59         frame.getContentPane().add(panel);
60         frame.pack();
61         frame.show();
62     }
63
64
65     public JComponent buildPanel() {
66         JTabbedPane tabbedPane = new JTabbedPane();
67         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
68
69         tabbedPane.add(buildHorizontalPanel(), "Horizontal");
70         tabbedPane.add(buildVerticalPanel(), "Vertical");
71         return tabbedPane;
72     }
73     
74     
75     private JComponent buildHorizontalPanel() {
76         FormLayout layout = new FormLayout(
77                         "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);
81         
82         panel.add(new JLabel("Left"), "1, 1, c, c");
83         panel.add(new JLabel("Center"), "2, 1, c, c");
84         panel.add(new JLabel("Right"), "3, 1, c, c");
85         panel.add(new JLabel("Default"), "4, 1, c, c");
86         
87         int row = 3;
88         addHorizontalButton(panel, 1, row, CellConstraints.DEFAULT);
89         addHorizontalButton(panel, 2, row, CellConstraints.DEFAULT);
90         addHorizontalButton(panel, 3, row, CellConstraints.DEFAULT);
91         addHorizontalButton(panel, 4, row, CellConstraints.DEFAULT);
92
93         row++;
94         addHorizontalButton(panel, 1, row, CellConstraints.FILL);
95         addHorizontalButton(panel, 2, row, CellConstraints.FILL);
96         addHorizontalButton(panel, 3, row, CellConstraints.FILL);
97         addHorizontalButton(panel, 4, row, CellConstraints.FILL);
98
99         row++;
100         addHorizontalButton(panel, 1, row, CellConstraints.LEFT);
101         addHorizontalButton(panel, 2, row, CellConstraints.LEFT);
102         addHorizontalButton(panel, 3, row, CellConstraints.LEFT);
103         addHorizontalButton(panel, 4, row, CellConstraints.LEFT);
104
105         row++;
106         addHorizontalButton(panel, 1, row, CellConstraints.CENTER);
107         addHorizontalButton(panel, 2, row, CellConstraints.CENTER);
108         addHorizontalButton(panel, 3, row, CellConstraints.CENTER);
109         addHorizontalButton(panel, 4, row, CellConstraints.CENTER);
110
111         row++;
112         addHorizontalButton(panel, 1, row, CellConstraints.RIGHT);
113         addHorizontalButton(panel, 2, row, CellConstraints.RIGHT);
114         addHorizontalButton(panel, 3, row, CellConstraints.RIGHT);
115         addHorizontalButton(panel, 4, row, CellConstraints.RIGHT);
116
117         return panel;
118     }
119     
120     
121     private JComponent buildVerticalPanel() {
122         FormLayout layout = new FormLayout(
123                         "left:pref, 8dlu, l:p, l:p, l:p, l:p, l:p",
124                         "top:pref:g, center:pref:g, bottom:pref:g, pref:g");
125         JPanel panel = new JPanel(layout);
126         panel.setBorder(Borders.DIALOG_BORDER);
127
128         panel.add(new JLabel("Top"), "1, 1, r, c");
129         panel.add(new JLabel("Center"), "1, 2, r, c");
130         panel.add(new JLabel("Bottom"), "1, 3, r, c");
131         panel.add(new JLabel("Default"), "1, 4, r, c");
132         
133         int col = 3;
134         addVerticalButton(panel, col, 1, CellConstraints.DEFAULT);
135         addVerticalButton(panel, col, 2, CellConstraints.DEFAULT);
136         addVerticalButton(panel, col, 3, CellConstraints.DEFAULT);
137         addVerticalButton(panel, col, 4, CellConstraints.DEFAULT);
138
139         col++;
140         addVerticalButton(panel, col, 1, CellConstraints.FILL);
141         addVerticalButton(panel, col, 2, CellConstraints.FILL);
142         addVerticalButton(panel, col, 3, CellConstraints.FILL);
143         addVerticalButton(panel, col, 4, CellConstraints.FILL);
144
145         col++;
146         addVerticalButton(panel, col, 1, CellConstraints.TOP);
147         addVerticalButton(panel, col, 2, CellConstraints.TOP);
148         addVerticalButton(panel, col, 3, CellConstraints.TOP);
149         addVerticalButton(panel, col, 4, CellConstraints.TOP);
150
151         col++;
152         addVerticalButton(panel, col, 1, CellConstraints.CENTER);
153         addVerticalButton(panel, col, 2, CellConstraints.CENTER);
154         addVerticalButton(panel, col, 3, CellConstraints.CENTER);
155         addVerticalButton(panel, col, 4, CellConstraints.CENTER);
156
157         col++;
158         addVerticalButton(panel, col, 1, CellConstraints.BOTTOM);
159         addVerticalButton(panel, col, 2, CellConstraints.BOTTOM);
160         addVerticalButton(panel, col, 3, CellConstraints.BOTTOM);
161         addVerticalButton(panel, col, 4, CellConstraints.BOTTOM);
162
163         return panel;
164     }
165     
166     
167     private void addHorizontalButton(JPanel panel, int col, int row,
168                                     CellConstraints.Alignment hAlignment) {
169         JButton button = new JButton(hAlignment.toString());
170         panel.add(button, new CellConstraints(col, row,
171                                               hAlignment,
172                                               CellConstraints.DEFAULT));
173     }
174     
175     
176     private void addVerticalButton(JPanel panel, int col, int row,
177                                     CellConstraints.Alignment vAlignment) {
178         JButton button = new JButton(vAlignment.toString());
179         panel.add(button, new CellConstraints(col, row,
180                                               CellConstraints.DEFAULT,
181                                               vAlignment));
182     }
183     
184     
185 }
186
187
Popular Tags