001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.tutorial.basics;
032:
033: import javax.swing.*;
034:
035: import com.jgoodies.forms.factories.Borders;
036: import com.jgoodies.forms.layout.CellConstraints;
037: import com.jgoodies.forms.layout.FormLayout;
038:
039: /**
040: * Demonstrates how FormLayout applies the default column and row
041: * alignments to cells, and how to override the defaults.
042: *
043: * @author Karsten Lentzsch
044: * @version $Revision: 1.13 $
045: */
046: public final class CellAlignmentExample {
047:
048: public static void main(String[] args) {
049: try {
050: UIManager
051: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
052: } catch (Exception e) {
053: // Likely PlasticXP is not in the class path; ignore.
054: }
055: JFrame frame = new JFrame();
056: frame.setTitle("Forms Tutorial :: Cell Alignments");
057: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
058: JComponent panel = new CellAlignmentExample().buildPanel();
059: frame.getContentPane().add(panel);
060: frame.pack();
061: frame.setVisible(true);
062: }
063:
064: public JComponent buildPanel() {
065: JTabbedPane tabbedPane = new JTabbedPane();
066: tabbedPane.putClientProperty("jgoodies.noContentBorder",
067: Boolean.TRUE);
068:
069: tabbedPane.add(buildHorizontalPanel(), "Horizontal");
070: tabbedPane.add(buildVerticalPanel(), "Vertical");
071: return tabbedPane;
072: }
073:
074: private JComponent buildHorizontalPanel() {
075: FormLayout layout = new FormLayout(
076: "r:p, 4dlu, left:pref:g, center:pref:g, right:pref:g, pref:g",
077: "pref, 8dlu, pref, pref, pref, pref, pref");
078: JPanel panel = new JPanel(layout);
079: panel.setBorder(Borders.DIALOG_BORDER);
080:
081: panel.add(new JLabel("Column Spec: "), "1, 1, r, c");
082: panel.add(new JLabel(" \"left:pref:grow\" "), "3, 1, c, c");
083: panel.add(new JLabel(" \"center:pref:grow\" "), "4, 1, c, c");
084: panel.add(new JLabel(" \"right:pref:grow\" "), "5, 1, c, c");
085: panel.add(new JLabel(" \"pref:grow\" "), "6, 1, c, c");
086:
087: int row = 3;
088: addHorizontalButton(panel, 3, row, CellConstraints.DEFAULT);
089: addHorizontalButton(panel, 4, row, CellConstraints.DEFAULT);
090: addHorizontalButton(panel, 5, row, CellConstraints.DEFAULT);
091: addHorizontalButton(panel, 6, row, CellConstraints.DEFAULT);
092:
093: row++;
094: addHorizontalButton(panel, 3, row, CellConstraints.FILL);
095: addHorizontalButton(panel, 4, row, CellConstraints.FILL);
096: addHorizontalButton(panel, 5, row, CellConstraints.FILL);
097: addHorizontalButton(panel, 6, row, CellConstraints.FILL);
098:
099: row++;
100: addHorizontalButton(panel, 3, row, CellConstraints.LEFT);
101: addHorizontalButton(panel, 4, row, CellConstraints.LEFT);
102: addHorizontalButton(panel, 5, row, CellConstraints.LEFT);
103: addHorizontalButton(panel, 6, row, CellConstraints.LEFT);
104:
105: row++;
106: addHorizontalButton(panel, 3, row, CellConstraints.CENTER);
107: addHorizontalButton(panel, 4, row, CellConstraints.CENTER);
108: addHorizontalButton(panel, 5, row, CellConstraints.CENTER);
109: addHorizontalButton(panel, 6, row, CellConstraints.CENTER);
110:
111: row++;
112: addHorizontalButton(panel, 3, row, CellConstraints.RIGHT);
113: addHorizontalButton(panel, 4, row, CellConstraints.RIGHT);
114: addHorizontalButton(panel, 5, row, CellConstraints.RIGHT);
115: addHorizontalButton(panel, 6, row, CellConstraints.RIGHT);
116:
117: return panel;
118: }
119:
120: private JComponent buildVerticalPanel() {
121: FormLayout layout = new FormLayout(
122: "left:pref, 8dlu, p, c:p, p, p, p",
123: "p, 4dlu, top:pref:g, center:pref:g, bottom:pref:g, pref:g");
124: layout.setColumnGroups(new int[][] { { 3, 5, 6, 7 } });
125: JPanel panel = new JPanel(layout);
126: panel.setBorder(Borders.DIALOG_BORDER);
127:
128: panel.add(new JLabel("Row Spec:"), "1, 1, r, c");
129: panel.add(new JLabel("\"top:pref:grow\""), "1, 3, r, c");
130: panel.add(new JLabel("\"center:pref:grow\""), "1, 4, r, c");
131: panel.add(new JLabel("\"bottom:pref:grow\""), "1, 5, r, c");
132: panel.add(new JLabel("\"pref:grow\""), "1, 6, r, c");
133:
134: int col = 3;
135: addVerticalButton(panel, col, 3, CellConstraints.DEFAULT);
136: addVerticalButton(panel, col, 4, CellConstraints.DEFAULT);
137: addVerticalButton(panel, col, 5, CellConstraints.DEFAULT);
138: addVerticalButton(panel, col, 6, CellConstraints.DEFAULT);
139:
140: col++;
141: addVerticalButton(panel, col, 3, CellConstraints.FILL);
142: addVerticalButton(panel, col, 4, CellConstraints.FILL);
143: addVerticalButton(panel, col, 5, CellConstraints.FILL);
144: addVerticalButton(panel, col, 6, CellConstraints.FILL);
145:
146: col++;
147: addVerticalButton(panel, col, 3, CellConstraints.TOP);
148: addVerticalButton(panel, col, 4, CellConstraints.TOP);
149: addVerticalButton(panel, col, 5, CellConstraints.TOP);
150: addVerticalButton(panel, col, 6, CellConstraints.TOP);
151:
152: col++;
153: addVerticalButton(panel, col, 3, CellConstraints.CENTER);
154: addVerticalButton(panel, col, 4, CellConstraints.CENTER);
155: addVerticalButton(panel, col, 5, CellConstraints.CENTER);
156: addVerticalButton(panel, col, 6, CellConstraints.CENTER);
157:
158: col++;
159: addVerticalButton(panel, col, 3, CellConstraints.BOTTOM);
160: addVerticalButton(panel, col, 4, CellConstraints.BOTTOM);
161: addVerticalButton(panel, col, 5, CellConstraints.BOTTOM);
162: addVerticalButton(panel, col, 6, CellConstraints.BOTTOM);
163:
164: return panel;
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, hAlignment,
171: CellConstraints.DEFAULT));
172: }
173:
174: private void addVerticalButton(JPanel panel, int col, int row,
175: CellConstraints.Alignment vAlignment) {
176: JButton button = new JButton(vAlignment.toString());
177: panel.add(button, new CellConstraints(col, row,
178: CellConstraints.DEFAULT, vAlignment));
179: }
180:
181: }
|