KickJava   Java API By Example, From Geeks To Geeks.

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


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 the different FormLayout alignments.
41  *
42  * @author Karsten Lentzsch
43  * @version $Revision: 1.7 $
44  */

45 public final class AlignmentExample {
46
47     
48     public static void main(String JavaDoc[] args) {
49         try {
50             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
51         } catch (Exception JavaDoc e) {
52             // Likely PlasticXP is not in the class path; ignore.
53
}
54         JFrame frame = new JFrame();
55         frame.setTitle("Forms Tutorial :: Alignments");
56         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
57         JComponent panel = new AlignmentExample().buildPanel();
58         frame.getContentPane().add(panel);
59         frame.pack();
60         frame.show();
61     }
62
63
64     public JComponent buildPanel() {
65         JTabbedPane tabbedPane = new JTabbedPane();
66         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
67
68         tabbedPane.add("Horizontal", buildHorizontalButtons());
69         tabbedPane.add("Vertical", buildVerticalButtons());
70         return tabbedPane;
71     }
72     
73     
74     private JComponent buildHorizontalButtons() {
75         FormLayout layout = new FormLayout(
76             "left:pref, 15px, center:pref, 15px, right:pref, 15px, fill:pref, 15px, pref",
77             "pref, 12px, pref, 4px, pref, 4px, pref, 4px, pref, 4px, pref");
78             
79         // Create a panel that uses the layout.
80
JPanel panel = new JPanel(layout);
81
82         // Set a default border.
83
panel.setBorder(Borders.DIALOG_BORDER);
84         
85         // Create a reusable CellConstraints instance.
86
CellConstraints cc = new CellConstraints();
87
88         // Add components to the panel.
89
panel.add(new JLabel("Left"), cc.xy(1, 1));
90         panel.add(new JButton("Name"), cc.xy(1, 3));
91         panel.add(new JButton("Phone"), cc.xy(1, 5));
92         panel.add(new JButton("Fax"), cc.xy(1, 7));
93         panel.add(new JButton("Email"), cc.xy(1, 9));
94         panel.add(new JButton("Address"), cc.xy(1, 11));
95
96         panel.add(new JLabel("Center"), cc.xy(3, 1));
97         panel.add(new JButton("Name"), cc.xy(3, 3));
98         panel.add(new JButton("Phone"), cc.xy(3, 5));
99         panel.add(new JButton("Fax"), cc.xy(3, 7));
100         panel.add(new JButton("Email"), cc.xy(3, 9));
101         panel.add(new JButton("Address"), cc.xy(3, 11));
102
103         panel.add(new JLabel("Right"), cc.xy(5, 1));
104         panel.add(new JButton("Name"), cc.xy(5, 3));
105         panel.add(new JButton("Phone"), cc.xy(5, 5));
106         panel.add(new JButton("Fax"), cc.xy(5, 7));
107         panel.add(new JButton("Email"), cc.xy(5, 9));
108         panel.add(new JButton("Address"), cc.xy(5, 11));
109
110         panel.add(new JLabel("Fill"), cc.xy(7, 1, "center, center"));
111         panel.add(new JButton("Name"), cc.xy(7, 3));
112         panel.add(new JButton("Phone"), cc.xy(7, 5));
113         panel.add(new JButton("Fax"), cc.xy(7, 7));
114         panel.add(new JButton("Email"), cc.xy(7, 9));
115         panel.add(new JButton("Address"), cc.xy(7, 11));
116
117         panel.add(new JLabel("Default"), cc.xy(9, 1, "center, center"));
118         panel.add(new JButton("Name"), cc.xy(9, 3));
119         panel.add(new JButton("Phone"), cc.xy(9, 5));
120         panel.add(new JButton("Fax"), cc.xy(9, 7));
121         panel.add(new JButton("Email"), cc.xy(9, 9));
122         panel.add(new JButton("Address"), cc.xy(9, 11));
123
124         return panel;
125     }
126     
127     
128     private JComponent buildVerticalButtons() {
129         FormLayout layout = new FormLayout(
130             "pref, 8dlu, pref, 4dlu, pref",
131             "top:pref, 9dlu, center:pref, 9dlu, bottom:pref, 9dlu, fill:pref, 9dlu, pref");
132             
133         // Create a panel that uses the layout.
134
JPanel panel = new JPanel(layout);
135
136         // Set a default border.
137
panel.setBorder(Borders.DIALOG_BORDER);
138         
139         // Create a reusable CellConstraints instance.
140
CellConstraints cc = new CellConstraints();
141
142         // Add components to the panel.
143
panel.add(new JLabel("Top"), cc.xy(1, 1));
144         panel.add(createSmallButton(), cc.xy(3, 1));
145         panel.add(createMediumButton(), cc.xy(5, 1));
146
147         panel.add(new JLabel("Center"), cc.xy(1, 3));
148         panel.add(createSmallButton(), cc.xy(3, 3));
149         panel.add(createMediumButton(), cc.xy(5, 3));
150
151         panel.add(new JLabel("Bottom"), cc.xy(1, 5));
152         panel.add(createSmallButton(), cc.xy(3, 5));
153         panel.add(createMediumButton(), cc.xy(5, 5));
154
155         panel.add(new JLabel("Fill"), cc.xy(1, 7));
156         panel.add(createSmallButton(), cc.xy(3, 7));
157         panel.add(createMediumButton(), cc.xy(5, 7));
158
159         panel.add(new JLabel("Default"), cc.xy(1, 9));
160         panel.add(createSmallButton(), cc.xy(3, 9));
161         panel.add(createMediumButton(), cc.xy(5, 9));
162
163         return panel;
164     }
165     
166     private JButton createSmallButton() {
167         return new JButton("<html>One</html>");
168     }
169     
170     private JButton createMediumButton() {
171         return new JButton("<html>One<br>Two</html>");
172     }
173     
174     
175 }
176
177
Popular Tags