KickJava   Java API By Example, From Geeks To Geeks.

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


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 columns and rows can be grouped in FormLayout.
41  *
42  * @author Karsten Lentzsch
43  * @version $Revision: 1.6 $
44  */

45 public final class GroupingExample {
46     
47     public static void main(String JavaDoc[] args) {
48         try {
49             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
50         } catch (Exception JavaDoc e) {
51             // Likely PlasticXP is not in the class path; ignore.
52
}
53         JFrame frame = new JFrame();
54         frame.setTitle("Forms Tutorial :: Grouping");
55         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
56         JComponent panel = new GroupingExample().buildPanel();
57         frame.getContentPane().add(panel);
58         frame.pack();
59         frame.show();
60     }
61
62
63     public JComponent buildPanel() {
64         JTabbedPane tabbedPane = new JTabbedPane();
65         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
66
67         tabbedPane.add("Ungrouped Bar", buildWizardBar(false));
68         tabbedPane.add("Grouped Bar", buildWizardBar(true));
69         tabbedPane.add("Ungrouped Rows", buildEditorPanel(false));
70         tabbedPane.add("Grouped Rows", buildEditorPanel(true));
71         return tabbedPane;
72     }
73     
74     
75     private JComponent buildWizardBar(boolean grouped) {
76         FormLayout layout = new FormLayout(
77             "pref, 6px:grow, pref, pref, 12px, pref, 6px, pref",
78             "pref");
79         if (grouped) {
80             layout.setColumnGroups(new int[][]{{1, 3, 4, 6, 8}});
81         }
82         JPanel panel = new JPanel(layout);
83         panel.setBorder(Borders.DIALOG_BORDER);
84         CellConstraints cc = new CellConstraints();
85
86         panel.add(createNarrowButton("Hilfe"), cc.xy(1, 1));
87         panel.add(createNarrowButton("< Zurück"), cc.xy(3, 1));
88         panel.add(createNarrowButton("Vor >"), cc.xy(4, 1));
89         panel.add(createNarrowButton("Beenden"), cc.xy(6, 1));
90         panel.add(createNarrowButton("Abbrechen"), cc.xy(8, 1));
91         
92         return panel;
93     }
94     
95     
96     private JComponent buildEditorPanel(boolean grouped) {
97         FormLayout layout = new FormLayout(
98                 "pref, 4dlu, 35dlu, 2dlu, 35dlu, 2dlu, 35dlu, 2dlu, 35dlu",
99                 "p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p");
100         if (grouped) {
101             layout.setRowGroups(new int[][] { { 1, 3, 5, 7, 9, 11, 13, 15, 17 } });
102         }
103             
104         JPanel panel = new JPanel(layout);
105         panel.setBorder(Borders.DIALOG_BORDER);
106         CellConstraints cc = new CellConstraints();
107
108         panel.add(new JLabel("File number:"), cc.xy (1, 1));
109         panel.add(new JTextField(), cc.xywh(3, 1, 7, 1));
110         panel.add(new JLabel("BL/MBL number:"), cc.xy (1, 3));
111         panel.add(new JTextField(), cc.xy (3, 3));
112         panel.add(new JTextField(), cc.xy (5, 3));
113         panel.add(new JLabel("Entry date:"), cc.xy (1, 5));
114         panel.add(new JTextField(), cc.xy (3, 5));
115         panel.add(new JLabel("RFQ number:"), cc.xy (1, 7));
116         panel.add(new JTextField(), cc.xywh(3, 7, 7, 1));
117         panel.add(new JLabel("Goods:"), cc.xy (1, 9));
118         panel.add(new JCheckBox("Dangerous"), cc.xywh(3, 9, 7, 1));
119         panel.add(new JLabel("Shipper:"), cc.xy (1, 11));
120         panel.add(new JTextField(), cc.xywh(3, 11, 7, 1));
121         panel.add(new JLabel("Customer:"), cc.xy (1, 13));
122         panel.add(new JTextField(), cc.xywh(3, 13, 5, 1));
123         panel.add(new JButton("..."), cc.xy (9, 13));
124         panel.add(new JLabel("Port of loading:"), cc.xy (1, 15));
125         panel.add(new JTextField(), cc.xywh(3, 15, 7, 1));
126         panel.add(new JLabel("Destination:"), cc.xy (1, 17));
127         panel.add(new JTextField(), cc.xywh(3, 17, 7, 1));
128         
129         return panel;
130     }
131     
132     
133     // Component Creation *****************************************************
134

135     private JButton createNarrowButton(String JavaDoc text) {
136         JButton button = new JButton(text);
137         button.putClientProperty("jgoodies.isNarrow", Boolean.TRUE);
138         return button;
139     }
140     
141     
142 }
143
144
Popular Tags