KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SplitPaneDemo


1 /*
2  * @(#)SplitPaneDemo.java 1.12 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)SplitPaneDemo.java 1.12 05/11/17
39  */

40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.text.*;
45 import javax.swing.table.*;
46 import javax.swing.border.*;
47 import javax.swing.colorchooser.*;
48 import javax.swing.filechooser.*;
49 import javax.accessibility.*;
50
51 import java.awt.*;
52 import java.awt.event.*;
53 import java.beans.*;
54 import java.util.*;
55 import java.io.*;
56 import java.applet.*;
57 import java.net.*;
58
59 /**
60  * Split Pane demo
61  *
62  * @version 1.12 11/17/05
63  * @author Scott Violet
64  * @author Jeff Dinkins
65  */

66 public class SplitPaneDemo extends DemoModule {
67
68     JSplitPane splitPane = null;
69     JLabel earth = null;
70     JLabel moon = null;
71     
72     JTextField divSize;
73     JTextField earthSize;
74     JTextField moonSize;
75     
76     /**
77      * main method allows us to run as a standalone demo.
78      */

79     public static void main(String JavaDoc[] args) {
80     SplitPaneDemo demo = new SplitPaneDemo(null);
81     demo.mainImpl();
82     }
83     
84     /**
85      * SplitPaneDemo Constructor
86      */

87     public SplitPaneDemo(SwingSet2 swingset) {
88     super(swingset, "SplitPaneDemo", "toolbar/JSplitPane.gif");
89
90     earth = new JLabel(createImageIcon("splitpane/earth.jpg", getString("SplitPaneDemo.earth")));
91     earth.setMinimumSize(new Dimension(20, 20));
92
93     moon = new JLabel(createImageIcon("splitpane/moon.jpg", getString("SplitPaneDemo.moon")));
94     moon.setMinimumSize(new Dimension(20, 20));
95     
96         splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, earth, moon);
97         splitPane.setContinuousLayout(true);
98     splitPane.setOneTouchExpandable(true);
99
100         splitPane.setDividerLocation(200);
101
102     getDemoPanel().add(splitPane, BorderLayout.CENTER);
103     getDemoPanel().setBackground(Color.black);
104
105     getDemoPanel().add(createSplitPaneControls(), BorderLayout.SOUTH);
106     }
107     
108     /**
109      * Creates controls to alter the JSplitPane.
110      */

111     protected JPanel createSplitPaneControls() {
112         JPanel wrapper = new JPanel();
113         ButtonGroup group = new ButtonGroup();
114         JRadioButton button;
115
116         Box buttonWrapper = new Box(BoxLayout.X_AXIS);
117     
118         wrapper.setLayout(new GridLayout(0, 1));
119     
120         /* Create a radio button to vertically split the split pane. */
121         button = new JRadioButton(getString("SplitPaneDemo.vert_split"));
122         button.setMnemonic(getMnemonic("SplitPaneDemo.vert_split_mnemonic"));
123         button.addActionListener(new ActionListener() {
124             public void actionPerformed(ActionEvent e) {
125                 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
126             }
127         });
128         group.add(button);
129         buttonWrapper.add(button);
130
131         /* Create a radio button the horizontally split the split pane. */
132         button = new JRadioButton(getString("SplitPaneDemo.horz_split"));
133         button.setMnemonic(getMnemonic("SplitPaneDemo.horz_split_mnemonic"));
134         button.setSelected(true);
135         button.addActionListener(new ActionListener() {
136             public void actionPerformed(ActionEvent e) {
137                 splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
138             }
139         });
140         group.add(button);
141         buttonWrapper.add(button);
142     
143         /* Create a check box as to whether or not the split pane continually
144            lays out the component when dragging. */

145         JCheckBox checkBox = new JCheckBox(getString("SplitPaneDemo.cont_layout"));
146         checkBox.setMnemonic(getMnemonic("SplitPaneDemo.cont_layout_mnemonic"));
147         checkBox.setSelected(true);
148     
149         checkBox.addChangeListener(new ChangeListener() {
150             public void stateChanged(ChangeEvent e) {
151                 splitPane.setContinuousLayout(
152             ((JCheckBox)e.getSource()).isSelected());
153             }
154         });
155         buttonWrapper.add(checkBox);
156     
157         /* Create a check box as to whether or not the split pane divider
158            contains the oneTouchExpandable buttons. */

159         checkBox = new JCheckBox(getString("SplitPaneDemo.one_touch_expandable"));
160         checkBox.setMnemonic(getMnemonic("SplitPaneDemo.one_touch_expandable_mnemonic"));
161         checkBox.setSelected(true);
162     
163         checkBox.addChangeListener(new ChangeListener() {
164             public void stateChanged(ChangeEvent e) {
165                 splitPane.setOneTouchExpandable(
166             ((JCheckBox) e.getSource()).isSelected());
167         }
168     });
169     buttonWrapper.add(checkBox);
170     wrapper.add(buttonWrapper);
171     
172     /* Create a text field to change the divider size. */
173     JPanel tfWrapper;
174     JLabel label;
175     
176     divSize = new JTextField();
177         divSize.setText(new Integer JavaDoc(splitPane.getDividerSize()).toString());
178         divSize.setColumns(5);
179         divSize.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.divider_size"));
180         divSize.addActionListener(new ActionListener() {
181         public void actionPerformed(ActionEvent e) {
182         String JavaDoc value = ((JTextField)e.getSource()).getText();
183         int newSize;
184         
185         try {
186             newSize = Integer.parseInt(value);
187         } catch (Exception JavaDoc ex) {
188             newSize = -1;
189         }
190         if(newSize > 0) {
191             splitPane.setDividerSize(newSize);
192         } else {
193             JOptionPane.showMessageDialog(splitPane,
194                           getString("SplitPaneDemo.invalid_divider_size"),
195                           getString("SplitPaneDemo.error"),
196                           JOptionPane.ERROR_MESSAGE);
197         }
198         }
199     });
200     label = new JLabel(getString("SplitPaneDemo.divider_size"));
201     tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
202     tfWrapper.add(label);
203         tfWrapper.add(divSize);
204         label.setLabelFor(divSize);
205     label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.divider_size_mnemonic"));
206     wrapper.add(tfWrapper);
207     
208     /* Create a text field that will change the preferred/minimum size
209        of the earth component. */

210     earthSize = new JTextField(String.valueOf(earth.getMinimumSize().width));
211         earthSize.setColumns(5);
212         earthSize.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.first_component_min_size"));
213         earthSize.addActionListener(new ActionListener() {
214         public void actionPerformed(ActionEvent e) {
215         String JavaDoc value = ((JTextField)e.getSource()).getText();
216         int newSize;
217         
218         try {
219             newSize = Integer.parseInt(value);
220         } catch (Exception JavaDoc ex) {
221             newSize = -1;
222         }
223         if(newSize > 10) {
224             earth.setMinimumSize(new Dimension(newSize, newSize));
225         } else {
226             JOptionPane.showMessageDialog(splitPane,
227                           getString("SplitPaneDemo.invalid_min_size") +
228                           getString("SplitPaneDemo.must_be_greater_than") + 10,
229                           getString("SplitPaneDemo.error"),
230                           JOptionPane.ERROR_MESSAGE);
231         }
232         }
233     });
234     label = new JLabel(getString("SplitPaneDemo.first_component_min_size"));
235     tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
236     tfWrapper.add(label);
237         tfWrapper.add(earthSize);
238         label.setLabelFor(earthSize);
239     label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.first_component_min_size_mnemonic"));
240     wrapper.add(tfWrapper);
241     
242     /* Create a text field that will change the preferred/minimum size
243        of the moon component. */

244     moonSize = new JTextField(String.valueOf(moon.getMinimumSize().width));
245         moonSize.setColumns(5);
246         moonSize.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.second_component_min_size"));
247         moonSize.addActionListener(new ActionListener() {
248         public void actionPerformed(ActionEvent e) {
249         String JavaDoc value = ((JTextField)e.getSource()).getText();
250         int newSize;
251         
252         try {
253             newSize = Integer.parseInt(value);
254         } catch (Exception JavaDoc ex) {
255             newSize = -1;
256         }
257         if(newSize > 10) {
258             moon.setMinimumSize(new Dimension(newSize, newSize));
259         } else {
260             JOptionPane.showMessageDialog(splitPane,
261                           getString("SplitPaneDemo.invalid_min_size") +
262                           getString("SplitPaneDemo.must_be_greater_than") + 10,
263                           getString("SplitPaneDemo.error"),
264                           JOptionPane.ERROR_MESSAGE);
265         }
266         }
267     });
268     label = new JLabel(getString("SplitPaneDemo.second_component_min_size"));
269     tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
270     tfWrapper.add(label);
271         tfWrapper.add(moonSize);
272         label.setLabelFor(moonSize);
273     label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.second_component_min_size_mnemonic"));
274     wrapper.add(tfWrapper);
275         
276     return wrapper;
277     }
278     
279     void updateDragEnabled(boolean dragEnabled) {
280         divSize.setDragEnabled(dragEnabled);
281         earthSize.setDragEnabled(dragEnabled);
282         moonSize.setDragEnabled(dragEnabled);
283     }
284     
285 }
286
Popular Tags