KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > tutorial > building > ButtonBarsExample


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.building;
32
33 import java.awt.Component JavaDoc;
34
35 import javax.swing.*;
36
37 import com.jgoodies.forms.builder.ButtonBarBuilder;
38 import com.jgoodies.forms.factories.Borders;
39 import com.jgoodies.forms.layout.CellConstraints;
40 import com.jgoodies.forms.layout.FormLayout;
41
42 /**
43  * Demonstrates how to build button bars using a ButtonBarBuilder.
44  *
45  * @author Karsten Lentzsch
46  * @version $Revision: 1.6 $
47  * @see ButtonBarBuilder
48  * @see ButtonBarFactory
49  */

50 public final class ButtonBarsExample {
51     
52     public static void main(String JavaDoc[] args) {
53         try {
54             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
55         } catch (Exception JavaDoc e) {
56             // Likely PlasticXP is not in the class path; ignore.
57
}
58         JFrame frame = new JFrame();
59         frame.setTitle("Forms Tutorial :: Button Bars");
60         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
61         JComponent panel = new ButtonBarsExample().buildPanel();
62         frame.getContentPane().add(panel);
63         frame.pack();
64         frame.show();
65     }
66
67
68     public JComponent buildPanel() {
69         JTabbedPane tabbedPane = new JTabbedPane();
70         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
71
72         tabbedPane.add(buildButtonBar1Panel(), "No Builder");
73         tabbedPane.add(buildButtonBar2Panel(), "With Builder");
74         tabbedPane.add(buildButtonBar3Panel(), "Related");
75         tabbedPane.add(buildButtonBar4Panel(), "Unrelated ");
76         tabbedPane.add(buildButtonMixedBar1Panel(), "Mix 1");
77         tabbedPane.add(buildButtonMixedBar2Panel(), "Mix 2");
78         return tabbedPane;
79     }
80     
81     private Component JavaDoc buildButtonBar1Panel() {
82         JPanel buttonBar = new JPanel(
83             new FormLayout("0:grow, p, 4px, p", "p"));
84         buttonBar.add(new JButton("Yes"), "2, 1");
85         buttonBar.add(new JButton("No"), "4, 1");
86         
87         return wrap(buttonBar,
88             "\nThis bar has been built without a ButtonBarBuilder.\n" +
89             " o the buttons have no minimum widths and\n" +
90             " o gaps may be inconsistent between team members.");
91     }
92
93     private Component JavaDoc buildButtonBar2Panel() {
94         ButtonBarBuilder builder = new ButtonBarBuilder();
95         builder.addGlue();
96         builder.addGridded(new JButton("Yes"));
97         builder.addRelatedGap();
98         builder.addGridded(new JButton("No"));
99          
100         return wrap(builder.getPanel(),
101             "\nThis bar has been built with a ButtonBarBuilder.\n" +
102             " o The buttons have a minimum widths and\n" +
103             " o the button gap is a logical size that follows a style guide.");
104     }
105     
106     private Component JavaDoc buildButtonBar3Panel() {
107         ButtonBarBuilder builder = new ButtonBarBuilder();
108         builder.addGlue();
109         builder.addGridded(new JButton("Related"));
110         builder.addRelatedGap();
111         builder.addGridded(new JButton("Related"));
112         builder.addRelatedGap();
113         builder.addGridded(new JButton("Related"));
114
115         return wrap(builder.getPanel(),
116             "\nThis bar uses the logical gap for related buttons.\n");
117     }
118     
119     private Component JavaDoc buildButtonBar4Panel() {
120         ButtonBarBuilder builder = new ButtonBarBuilder();
121         builder.addGlue();
122         builder.addGridded(new JButton("Unrelated"));
123         builder.addUnrelatedGap();
124         builder.addGridded(new JButton("Unrelated"));
125         builder.addUnrelatedGap();
126         builder.addGridded(new JButton("Unrelated"));
127
128         return wrap(builder.getPanel(),
129             "\nThis bar uses the logical gap for unrelated buttons.\n" +
130             "It is a little bit wider than the related gap.");
131     }
132     
133     private Component JavaDoc buildButtonMixedBar1Panel() {
134         ButtonBarBuilder builder = new ButtonBarBuilder();
135         builder.addGridded(new JButton("Help"));
136         builder.addGlue();
137         builder.addUnrelatedGap();
138         builder.addFixed(new JButton("Copy to Clipboard"));
139         builder.addUnrelatedGap();
140         builder.addGridded(new JButton("OK"));
141         builder.addRelatedGap();
142         builder.addGridded(new JButton("Cancel"));
143
144         return wrap(builder.getPanel(),
145             "\nDemonstrates a glue (between Help and the rest),\n" +
146             "has related and unrelated buttons and an ungridded button\n" +
147             "with a default margin (Copy to Clipboard).");
148     }
149     
150     private Component JavaDoc buildButtonMixedBar2Panel() {
151         ButtonBarBuilder builder = new ButtonBarBuilder();
152         builder.addGridded(new JButton("Help"));
153         builder.addGlue();
154         builder.addUnrelatedGap();
155         builder.addFixedNarrow(new JButton("Copy to Clipboard"));
156         builder.addUnrelatedGap();
157         builder.addGridded(new JButton("OK"));
158         builder.addRelatedGap();
159         builder.addGridded(new JButton("Cancel"));
160
161         return wrap(builder.getPanel(),
162             "\nDemonstrates a glue (between Help and the rest),\n" +
163             "has related and unrelated buttons and an ungridded button\n" +
164             "with a narrow margin (Copy to Clipboard).");
165     }
166     
167     
168     // Helper Code ************************************************************
169

170     private Component JavaDoc wrap(Component JavaDoc buttonBar, String JavaDoc text) {
171         Component JavaDoc textPane = new JScrollPane(new JTextArea(text));
172         
173         FormLayout layout = new FormLayout(
174                         "fill:default:grow",
175                         "fill:p:grow, 4dlu, p");
176         JPanel panel = new JPanel(layout);
177         CellConstraints cc = new CellConstraints();
178         panel.setBorder(Borders.DIALOG_BORDER);
179         panel.add(textPane, cc.xy(1, 1));
180         panel.add(buttonBar, cc.xy(1, 3));
181         return panel;
182     }
183     
184 }
185
186
Popular Tags