KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.swing.border.EmptyBorder JavaDoc;
35
36 import com.jgoodies.forms.builder.PanelBuilder;
37 import com.jgoodies.forms.extras.DefaultFormBuilder;
38 import com.jgoodies.forms.layout.CellConstraints;
39 import com.jgoodies.forms.layout.FormLayout;
40
41 /**
42  * Demonstrates the three FormLayout component sizes: minimum, default and
43  * preferred.
44  * Min and Pref measure the components minimum and preferred size, where the
45  * Default size behaves like Pref but shrinks if the container space is scarce.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.3 $
49  */

50 public final class ComponentSizesExample {
51
52     
53     public static void main(String JavaDoc[] args) {
54         try {
55             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
56         } catch (Exception JavaDoc e) {
57             // Likely PlasticXP is not in the class path; ignore.
58
}
59         JFrame frame = new JFrame();
60         frame.setTitle("Forms Tutorial :: Component Sizes");
61         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
62         JComponent panel = new ComponentSizesExample().buildPanel();
63         frame.getContentPane().add(panel);
64         frame.pack();
65         frame.show();
66     }
67
68
69     public JComponent buildPanel() {
70         JSplitPane splitPane = new JSplitPane(
71             JSplitPane.HORIZONTAL_SPLIT,
72             true,
73             buildCombinedPanel(),
74             buildTextPanel());
75         return splitPane;
76     }
77     
78         
79     /**
80      * Builds and answer the panel that combines the three sizing panels.
81      */

82     private JComponent buildCombinedPanel() {
83         FormLayout layout = new FormLayout(
84             "30dlu:grow",
85             "pref, 3dlu, pref, 3dlu, pref");
86             
87         PanelBuilder builder = new PanelBuilder(layout);
88         builder.setDefaultDialogBorder();
89
90         CellConstraints cc = new CellConstraints();
91
92         builder.add(buildMinimumSizePanel(), cc.xy(1, 1));
93         builder.add(buildDefaultSizePanel(), cc.xy(1, 3));
94         builder.add(buildPreferredSizePanel(), cc.xy(1, 5));
95         
96         return builder.getPanel();
97     }
98     
99
100     private JComponent buildMinimumSizePanel() {
101         FormLayout layout = new FormLayout(
102                 "right:max(25dlu;pref), 3dlu, min",
103                 "pref");
104         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
105         builder.append("Min", new JTextField(15));
106         return builder.getPanel();
107     }
108     
109     private JComponent buildDefaultSizePanel() {
110         FormLayout layout = new FormLayout(
111                 "right:max(25dlu;pref), 3dlu, default",
112                 "pref");
113         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
114         builder.append("Default", new JTextField(15));
115         return builder.getPanel();
116     }
117     
118     private JComponent buildPreferredSizePanel() {
119         FormLayout layout = new FormLayout(
120                 "right:max(25dlu;pref), 3dlu, pref",
121                 "pref");
122         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
123         builder.append("Pref", new JTextField(15));
124         return builder.getPanel();
125     }
126     
127     private JComponent buildTextPanel() {
128         JTextArea textArea = new JTextArea(5, 20);
129         textArea.setEditable(false);
130         textArea.setText("The text field used in the example on the left\n" +
131         "has a narrow minimum width and a wider preferred width.\n\n" +
132         "If you move the split divider to the left and right\n" +
133         "you can see how 'Default' shrinks the field if space is scarce.\n\n" +
134         "If there's not enough space for the preferred width\n" +
135         "the bottom field will be 'cut' on the right-hand side.");
136         JScrollPane scrollpane = new JScrollPane(textArea);
137         scrollpane.setBorder(new EmptyBorder JavaDoc(0, 0, 0, 0));
138         return scrollpane;
139     }
140     
141     
142 }
143
144
Popular Tags