Path: blob/master/src/demo/share/jfc/SwingSet2/SplitPaneDemo.java
41149 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.table.*;37import javax.swing.border.*;38import javax.swing.colorchooser.*;39import javax.swing.filechooser.*;40import javax.accessibility.*;4142import java.awt.*;43import java.awt.event.*;44import java.beans.*;45import java.util.*;46import java.io.*;47import java.applet.*;48import java.net.*;4950/**51* Split Pane demo52*53* @author Scott Violet54* @author Jeff Dinkins55*/56public class SplitPaneDemo extends DemoModule {5758JSplitPane splitPane = null;59JLabel earth = null;60JLabel moon = null;6162JTextField divSize;63JTextField earthSize;64JTextField moonSize;6566/**67* main method allows us to run as a standalone demo.68*/69public static void main(String[] args) {70SplitPaneDemo demo = new SplitPaneDemo(null);71demo.mainImpl();72}7374/**75* SplitPaneDemo Constructor76*/77public SplitPaneDemo(SwingSet2 swingset) {78super(swingset, "SplitPaneDemo", "toolbar/JSplitPane.gif");7980earth = new JLabel(createImageIcon("splitpane/earth.jpg", getString("SplitPaneDemo.earth")));81earth.setMinimumSize(new Dimension(20, 20));8283moon = new JLabel(createImageIcon("splitpane/moon.jpg", getString("SplitPaneDemo.moon")));84moon.setMinimumSize(new Dimension(20, 20));8586splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, earth, moon);87splitPane.setContinuousLayout(true);88splitPane.setOneTouchExpandable(true);8990splitPane.setDividerLocation(200);9192getDemoPanel().add(splitPane, BorderLayout.CENTER);93getDemoPanel().setBackground(Color.black);9495getDemoPanel().add(createSplitPaneControls(), BorderLayout.SOUTH);96}9798/**99* Creates controls to alter the JSplitPane.100*/101protected JPanel createSplitPaneControls() {102JPanel wrapper = new JPanel();103ButtonGroup group = new ButtonGroup();104JRadioButton button;105106Box buttonWrapper = new Box(BoxLayout.X_AXIS);107108wrapper.setLayout(new GridLayout(0, 1));109110/* Create a radio button to vertically split the split pane. */111button = new JRadioButton(getString("SplitPaneDemo.vert_split"));112button.setMnemonic(getMnemonic("SplitPaneDemo.vert_split_mnemonic"));113button.addActionListener(new ActionListener() {114public void actionPerformed(ActionEvent e) {115splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);116}117});118group.add(button);119buttonWrapper.add(button);120121/* Create a radio button the horizontally split the split pane. */122button = new JRadioButton(getString("SplitPaneDemo.horz_split"));123button.setMnemonic(getMnemonic("SplitPaneDemo.horz_split_mnemonic"));124button.setSelected(true);125button.addActionListener(new ActionListener() {126public void actionPerformed(ActionEvent e) {127splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);128}129});130group.add(button);131buttonWrapper.add(button);132133/* Create a check box as to whether or not the split pane continually134lays out the component when dragging. */135JCheckBox checkBox = new JCheckBox(getString("SplitPaneDemo.cont_layout"));136checkBox.setMnemonic(getMnemonic("SplitPaneDemo.cont_layout_mnemonic"));137checkBox.setSelected(true);138139checkBox.addChangeListener(new ChangeListener() {140public void stateChanged(ChangeEvent e) {141splitPane.setContinuousLayout(142((JCheckBox)e.getSource()).isSelected());143}144});145buttonWrapper.add(checkBox);146147/* Create a check box as to whether or not the split pane divider148contains the oneTouchExpandable buttons. */149checkBox = new JCheckBox(getString("SplitPaneDemo.one_touch_expandable"));150checkBox.setMnemonic(getMnemonic("SplitPaneDemo.one_touch_expandable_mnemonic"));151checkBox.setSelected(true);152153checkBox.addChangeListener(new ChangeListener() {154public void stateChanged(ChangeEvent e) {155splitPane.setOneTouchExpandable(156((JCheckBox) e.getSource()).isSelected());157}158});159buttonWrapper.add(checkBox);160wrapper.add(buttonWrapper);161162/* Create a text field to change the divider size. */163JPanel tfWrapper;164JLabel label;165166divSize = new JTextField();167divSize.setText(Integer.toString(splitPane.getDividerSize()));168divSize.setColumns(5);169divSize.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.divider_size"));170divSize.addActionListener(new ActionListener() {171public void actionPerformed(ActionEvent e) {172String value = ((JTextField)e.getSource()).getText();173int newSize;174175try {176newSize = Integer.parseInt(value);177} catch (Exception ex) {178newSize = -1;179}180if(newSize > 0) {181splitPane.setDividerSize(newSize);182} else {183JOptionPane.showMessageDialog(splitPane,184getString("SplitPaneDemo.invalid_divider_size"),185getString("SplitPaneDemo.error"),186JOptionPane.ERROR_MESSAGE);187}188}189});190label = new JLabel(getString("SplitPaneDemo.divider_size"));191tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));192tfWrapper.add(label);193tfWrapper.add(divSize);194label.setLabelFor(divSize);195label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.divider_size_mnemonic"));196wrapper.add(tfWrapper);197198/* Create a text field that will change the preferred/minimum size199of the earth component. */200earthSize = new JTextField(String.valueOf(earth.getMinimumSize().width));201earthSize.setColumns(5);202earthSize.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.first_component_min_size"));203earthSize.addActionListener(new ActionListener() {204public void actionPerformed(ActionEvent e) {205String value = ((JTextField)e.getSource()).getText();206int newSize;207208try {209newSize = Integer.parseInt(value);210} catch (Exception ex) {211newSize = -1;212}213if(newSize > 10) {214earth.setMinimumSize(new Dimension(newSize, newSize));215} else {216JOptionPane.showMessageDialog(splitPane,217getString("SplitPaneDemo.invalid_min_size") +218getString("SplitPaneDemo.must_be_greater_than") + 10,219getString("SplitPaneDemo.error"),220JOptionPane.ERROR_MESSAGE);221}222}223});224label = new JLabel(getString("SplitPaneDemo.first_component_min_size"));225tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));226tfWrapper.add(label);227tfWrapper.add(earthSize);228label.setLabelFor(earthSize);229label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.first_component_min_size_mnemonic"));230wrapper.add(tfWrapper);231232/* Create a text field that will change the preferred/minimum size233of the moon component. */234moonSize = new JTextField(String.valueOf(moon.getMinimumSize().width));235moonSize.setColumns(5);236moonSize.getAccessibleContext().setAccessibleName(getString("SplitPaneDemo.second_component_min_size"));237moonSize.addActionListener(new ActionListener() {238public void actionPerformed(ActionEvent e) {239String value = ((JTextField)e.getSource()).getText();240int newSize;241242try {243newSize = Integer.parseInt(value);244} catch (Exception ex) {245newSize = -1;246}247if(newSize > 10) {248moon.setMinimumSize(new Dimension(newSize, newSize));249} else {250JOptionPane.showMessageDialog(splitPane,251getString("SplitPaneDemo.invalid_min_size") +252getString("SplitPaneDemo.must_be_greater_than") + 10,253getString("SplitPaneDemo.error"),254JOptionPane.ERROR_MESSAGE);255}256}257});258label = new JLabel(getString("SplitPaneDemo.second_component_min_size"));259tfWrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));260tfWrapper.add(label);261tfWrapper.add(moonSize);262label.setLabelFor(moonSize);263label.setDisplayedMnemonic(getMnemonic("SplitPaneDemo.second_component_min_size_mnemonic"));264wrapper.add(tfWrapper);265266return wrapper;267}268269void updateDragEnabled(boolean dragEnabled) {270divSize.setDragEnabled(dragEnabled);271earthSize.setDragEnabled(dragEnabled);272moonSize.setDragEnabled(dragEnabled);273}274275}276277278