~ubuntu-branches/ubuntu/trusty/libjgoodies-forms-java/trusty

« back to all changes in this revision

Viewing changes to src/tutorial/com/jgoodies/forms/tutorial/basics/CellAlignmentExample.java

  • Committer: Bazaar Package Importer
  • Author(s): Eric Lavarde
  • Date: 2006-03-23 20:54:19 UTC
  • Revision ID: james.westby@ubuntu.com-20060323205419-emo7oazp2lspvl3b
Tags: upstream-1.0.5
ImportĀ upstreamĀ versionĀ 1.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2002-2004 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.9 $
 
45
 */
 
46
public final class CellAlignmentExample {
 
47
 
 
48
    
 
49
    public static void main(String[] args) {
 
50
        try {
 
51
            UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
 
52
        } catch (Exception 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.EXIT_ON_CLOSE);
 
58
        JComponent panel = new CellAlignmentExample().buildPanel();
 
59
        frame.getContentPane().add(panel);
 
60
        frame.pack();
 
61
        frame.setVisible(true);
 
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
                        "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);
 
81
        
 
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");
 
87
        
 
88
        int row = 3;
 
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);
 
93
 
 
94
        row++;
 
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);
 
99
 
 
100
        row++;
 
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);
 
105
 
 
106
        row++;
 
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);
 
111
 
 
112
        row++;
 
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);
 
117
 
 
118
        return panel;
 
119
    }
 
120
    
 
121
    
 
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);
 
129
 
 
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");
 
135
        
 
136
        int col = 3;
 
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);
 
141
 
 
142
        col++;
 
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);
 
147
 
 
148
        col++;
 
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);
 
153
 
 
154
        col++;
 
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);
 
159
 
 
160
        col++;
 
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);
 
165
 
 
166
        return panel;
 
167
    }
 
168
    
 
169
    
 
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, 
 
174
                                              hAlignment, 
 
175
                                              CellConstraints.DEFAULT));
 
176
    }
 
177
    
 
178
    
 
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,
 
184
                                              vAlignment));
 
185
    }
 
186
    
 
187
    
 
188
}
 
189