1 /* 2 * 3 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * - Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * - Neither the name of Oracle nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 import javax.swing.*; 35 import javax.swing.event.*; 36 import javax.swing.text.*; 37 import javax.swing.table.*; 38 import javax.swing.border.*; 39 import javax.swing.colorchooser.*; 40 import javax.swing.filechooser.*; 41 import javax.accessibility.*; 42 43 import java.awt.*; 44 import java.awt.event.*; 45 import java.beans.*; 46 import java.util.*; 47 import java.io.*; 48 import java.applet.*; 49 import java.net.*; 50 51 /** 52 * Split Pane demo 53 * 54 * @author Scott Violet 55 * @author Jeff Dinkins 56 */ 57 public class SplitPaneDemo extends DemoModule { 58 59 JSplitPane splitPane = null; 60 JLabel earth = null; 61 JLabel moon = null; 62 63 JTextField divSize; 64 JTextField earthSize; 65 JTextField moonSize; 66 67 /** 68 * main method allows us to run as a standalone demo. 69 */ main(String[] args)70 public static void main(String[] args) { 71 SplitPaneDemo demo = new SplitPaneDemo(null); 72 demo.mainImpl(); 73 } 74 75 /** 76 * SplitPaneDemo Constructor 77 */ SplitPaneDemo(SwingSet2 swingset)78 public SplitPaneDemo(SwingSet2 swingset) { 79 super(swingset, "SplitPaneDemo", "toolbar/JSplitPane.gif"); 80 81 earth = new JLabel(createImageIcon("splitpane/earth.jpg", getString("SplitPaneDemo.earth"))); 82 earth.setMinimumSize(new Dimension(20, 20)); 83 84 moon = new JLabel(createImageIcon("splitpane/moon.jpg", getString("SplitPaneDemo.moon"))); 85 moon.setMinimumSize(new Dimension(20, 20)); 86 87 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, earth, moon); 88 splitPane.setContinuousLayout(true); 89 splitPane.setOneTouchExpandable(true); 90 91 splitPane.setDividerLocation(200); 92 93 getDemoPanel().add(splitPane, BorderLayout.CENTER); 94 getDemoPanel().setBackground(Color.black); 95 96 getDemoPanel().add(createSplitPaneControls(), BorderLayout.SOUTH); 97 } 98 99 /** 100 * Creates controls to alter the JSplitPane. 101 */ createSplitPaneControls()102 protected JPanel createSplitPaneControls() { 103 JPanel wrapper = new JPanel(); 104 ButtonGroup group = new ButtonGroup(); 105 JRadioButton button; 106 107 Box buttonWrapper = new Box(BoxLayout.X_AXIS); 108 109 wrapper.setLayout(new GridLayout(0, 1)); 110 111 /* Create a radio button to vertically split the split pane. */ 112 button = new JRadioButton(getString("SplitPaneDemo.vert_split")); 113 button.setMnemonic(getMnemonic("SplitPaneDemo.vert_split_mnemonic")); 114 button.addActionListener(new ActionListener() { 115 public void actionPerformed(ActionEvent e) { 116 splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); 117 } 118 }); 119 group.add(button); 120 buttonWrapper.add(button); 121 122 /* Create a radio button the horizontally split the split pane. */ 123 button = new JRadioButton(getString("SplitPaneDemo.horz_split")); 124 button.setMnemonic(getMnemonic("SplitPaneDemo.horz_split_mnemonic")); 125 button.setSelected(true); 126 button.addActionListener(new ActionListener() { 127 public void actionPerformed(ActionEvent e) { 128 splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT); 129 } 130 }); 131 group.add(button); 132 buttonWrapper.add(button); 133 134 /* Create a check box as to whether or not the split pane continually 135 lays out the component when dragging. */ 136 JCheckBox checkBox = new JCheckBox(getString("SplitPaneDemo.cont_layout")); 137 checkBox.setMnemonic(getMnemonic("SplitPaneDemo.cont_layout_mnemonic")); 138 checkBox.setSelected(true); 139 140 checkBox.addChangeListener(new ChangeListener() { 141 public void stateChanged(ChangeEvent e) { 142 splitPane.setContinuousLayout( 143 ((JCheckBox)e.getSource()).isSelected()); 144 } 145 }); 146 buttonWrapper.add(checkBox); 147 148 /* Create a check box as to whether or not the split pane divider 149 contains the oneTouchExpandable buttons. */ 150 checkBox = new JCheckBox(getString("SplitPaneDemo.one_touch_expandable")); 151 checkBox.setMnemonic(getMnemonic("SplitPaneDemo.one_touch_expandable_mnemonic")); 152 checkBox.setSelected(true); 153 154 checkBox.addChangeListener(new ChangeListener() { 155 public void stateChanged(ChangeEvent e) { 156 splitPane.setOneTouchExpandable( 157 ((JCheckBox) e.getSource()).isSelected()); 158 } 159 }); 160 buttonWrapper.add(checkBox); 161 wrapper.add(buttonWrapper); 162 163 /* Create a text field to change the divider size. */ 164 JPanel tfWrapper; 165 JLabel label; 166 167 divSize = new JTextField(); 168 divSize.setText(new Integer(splitPane.getDividerSize()).toString()); 169 divSize.setColumns(5); 170 divSize.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.divider_size")); 171 divSize.addActionListener(new ActionListener() { 172 public void actionPerformed(ActionEvent e) { 173 String value = ((JTextField)e.getSource()).getText(); 174 int newSize; 175 176 try { 177 newSize = Integer.parseInt(value); 178 } catch (Exception ex) { 179 newSize = -1; 180 } 181 if(newSize > 0) { 182 splitPane.setDividerSize(newSize); 183 } else { 184 JOptionPane.showMessageDialog(splitPane, 185 getString("SplitPaneDemo.invalid_divider_size"), 186 getString("SplitPaneDemo.error"), 187 JOptionPane.ERROR_MESSAGE); 188 } 189 } 190 }); 191 label = new JLabel(getString("SplitPaneDemo.divider_size")); 192 tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT)); 193 tfWrapper.add(label); 194 tfWrapper.add(divSize); 195 label.setLabelFor(divSize); 196 label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.divider_size_mnemonic")); 197 wrapper.add(tfWrapper); 198 199 /* Create a text field that will change the preferred/minimum size 200 of the earth component. */ 201 earthSize = new JTextField(String.valueOf(earth.getMinimumSize().width)); 202 earthSize.setColumns(5); 203 earthSize.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.first_component_min_size")); 204 earthSize.addActionListener(new ActionListener() { 205 public void actionPerformed(ActionEvent e) { 206 String value = ((JTextField)e.getSource()).getText(); 207 int newSize; 208 209 try { 210 newSize = Integer.parseInt(value); 211 } catch (Exception ex) { 212 newSize = -1; 213 } 214 if(newSize > 10) { 215 earth.setMinimumSize(new Dimension(newSize, newSize)); 216 } else { 217 JOptionPane.showMessageDialog(splitPane, 218 getString("SplitPaneDemo.invalid_min_size") + 219 getString("SplitPaneDemo.must_be_greater_than") + 10, 220 getString("SplitPaneDemo.error"), 221 JOptionPane.ERROR_MESSAGE); 222 } 223 } 224 }); 225 label = new JLabel(getString("SplitPaneDemo.first_component_min_size")); 226 tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT)); 227 tfWrapper.add(label); 228 tfWrapper.add(earthSize); 229 label.setLabelFor(earthSize); 230 label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.first_component_min_size_mnemonic")); 231 wrapper.add(tfWrapper); 232 233 /* Create a text field that will change the preferred/minimum size 234 of the moon component. */ 235 moonSize = new JTextField(String.valueOf(moon.getMinimumSize().width)); 236 moonSize.setColumns(5); 237 moonSize.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.second_component_min_size")); 238 moonSize.addActionListener(new ActionListener() { 239 public void actionPerformed(ActionEvent e) { 240 String value = ((JTextField)e.getSource()).getText(); 241 int newSize; 242 243 try { 244 newSize = Integer.parseInt(value); 245 } catch (Exception ex) { 246 newSize = -1; 247 } 248 if(newSize > 10) { 249 moon.setMinimumSize(new Dimension(newSize, newSize)); 250 } else { 251 JOptionPane.showMessageDialog(splitPane, 252 getString("SplitPaneDemo.invalid_min_size") + 253 getString("SplitPaneDemo.must_be_greater_than") + 10, 254 getString("SplitPaneDemo.error"), 255 JOptionPane.ERROR_MESSAGE); 256 } 257 } 258 }); 259 label = new JLabel(getString("SplitPaneDemo.second_component_min_size")); 260 tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT)); 261 tfWrapper.add(label); 262 tfWrapper.add(moonSize); 263 label.setLabelFor(moonSize); 264 label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.second_component_min_size_mnemonic")); 265 wrapper.add(tfWrapper); 266 267 return wrapper; 268 } 269 updateDragEnabled(boolean dragEnabled)270 void updateDragEnabled(boolean dragEnabled) { 271 divSize.setDragEnabled(dragEnabled); 272 earthSize.setDragEnabled(dragEnabled); 273 moonSize.setDragEnabled(dragEnabled); 274 } 275 276 } 277