Java Tutorial/Swing/JSplitPane — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:44, 31 мая 2010
Содержание
- 1 Continuously move the divider and resize its child components while the user is dragging the divider
- 2 Create a left-right split pane
- 3 Create a top-bottom split pane
- 4 Customizing a JSplitPane Look and Feel
- 5 Distributing Space When a JSplitPane Container Is Resized
- 6 Getting the Setting the Children in a JSplitPane Container
- 7 JSplitPane
- 8 Listening for JSplitPane Property Changes
- 9 Moving the JSplitPane Divider
- 10 Nested JSplitPane
- 11 Resizing Components and Working with a One-Touch Expandable Divider
- 12 set Divider Location for JSplitPane
- 13 Setting Orientation: JSplitPane.VERTICAL_SPLIT or JSplitPane.HORIZONTAL_SPLIT
- 14 The split pane supports a one-touch-expandable capability that allows the user to conveniently move the divider to either end with a single click
Continuously move the divider and resize its child components while the user is dragging the divider
import javax.swing.JButton;
import javax.swing.JSplitPane;
public class Main {
public static void main(String[] argv) throws Exception {
JButton topComponent = new JButton("top");
JButton bottomComponent = new JButton("bottom");
JSplitPane vpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topComponent, bottomComponent);
boolean b = vpane.isContinuousLayout(); // false by default
// Set the split pane to continuously resize the child components which the divider is dragged
vpane.setContinuousLayout(true);
}
}
Create a left-right split pane
import javax.swing.JButton;
import javax.swing.JSplitPane;
public class Main {
public static void main(String[] argv) throws Exception {
JButton leftComponent = new JButton("left");
JButton rightComponent = new JButton("right");
// Create a left-right split pane
JSplitPane hpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftComponent, rightComponent);
}
}
Create a top-bottom split pane
import javax.swing.JButton;
import javax.swing.JSplitPane;
public class Main {
public static void main(String[] argv) throws Exception {
JButton topComponent = new JButton("top");
JButton bottomComponent = new JButton("bottom");
JSplitPane vpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topComponent, bottomComponent);
}
}
Customizing a JSplitPane Look and Feel
Property StringObject TypeSplitPane.actionMapActionMapSplitPane.activeThumbColorSplitPane.ancestorInputMapInputMapSplitPane.backgroundColorSplitPane.borderBorderSplitPane.centerOneTouchButtonsBooleanSplitPane.darkShadowColorSplitPane.dividerFocusColorColorSplitPane.dividerSizeIntegerSplitPane.foregroundColorSplitPane.highlightColorSplitPane.leftButtonTextStringSplitPane.oneTouchButtonOffsetIntegerSplitPane.oneTouchButtonSizeIntegerSplitPane.oneTouchButtonsOpaqueBooleanSplitPane.oneTouchExpandableBooleanSplitPane.oneTouchOffsetIntegerSplitPane.rightButtonTextStringSplitPane.shadowColorSplitPane.sizeIntegerSplitPane.supportsOneTouchButtonsBooleanSplitPaneDivider.borderBorderSplitPaneDivider.draggingColorColorSplitPaneDivider.oneTouchButtonSizeIntegerSplitPaneUIString
Distributing Space When a JSplitPane Container Is Resized
import javax.swing.JButton;
import javax.swing.JSplitPane;
public class Main {
public static void main(String[] argv) throws Exception {
JButton leftComponent = new JButton("left");
JButton rightComponent = new JButton("right");
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftComponent, rightComponent);
double weight = pane.getResizeWeight(); // 0.0 by default
weight = 1D;
pane.setResizeWeight(weight);
weight = .5D;
pane.setResizeWeight(weight);
}
}
Getting the Setting the Children in a JSplitPane Container
import javax.swing.JButton;
import javax.swing.JSplitPane;
public class Main {
public static void main(String[] argv) throws Exception {
JButton leftComponent = new JButton("left");
JButton rightComponent = new JButton("right");
JButton topComponent = new JButton("top");
JButton bottomComponent = new JButton("bottom");
JSplitPane hpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftComponent, rightComponent);
JSplitPane vpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topComponent, bottomComponent);
leftComponent = (JButton) hpane.getLeftComponent();
rightComponent = (JButton) hpane.getRightComponent();
topComponent = (JButton) vpane.getTopComponent();
bottomComponent = (JButton) vpane.getBottomComponent();
hpane.setLeftComponent(topComponent);
hpane.setRightComponent(bottomComponent);
vpane.setTopComponent(leftComponent);
vpane.setBottomComponent(rightComponent);
}
}
JSplitPane
- Allows you to display components in a single row or column.
- JSplitPane can display two-and only two-components.
- The components are of variable size and separated by a movable divider.
public JSplitPane()
JSplitPane splitPane = new JSplitPane();
public JSplitPane(int newOrientation)
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
public JSplitPane(int newOrientation, boolean newContinuousLayout)
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true);
public JSplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent)
JComponent topComponent = new JButton("Top Button");
JComponent bottomComponent = new JButton("Bottom Button");
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topComponent, bottomComponent);
public JSplitPane(int newOrientation, boolean newContinuousLayout, Component newLeftComponent, Component newRightComponent)
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, topComponent, bottomComponent);
The continuousLayout property setting determines how the split pane reacts when the user drags the divider.
- false (the default), only the divider is redrawn when dragged.
- true, the JSplitPane resizes and redraws the components on each side of the divider as the user drags the divider.
Listening for JSplitPane Property Changes
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class PropertySplitPane {
public static void main(String args[]) {
JFrame frame = new JFrame("Property Split");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setContinuousLayout(true);
splitPane.setOneTouchExpandable(true);
JComponent topComponent = new JButton("A");
splitPane.setTopComponent(topComponent);
JComponent bottomComponent = new JButton("B");
splitPane.setBottomComponent(bottomComponent);
PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent changeEvent) {
JSplitPane sourceSplitPane = (JSplitPane) changeEvent.getSource();
String propertyName = changeEvent.getPropertyName();
if (propertyName.equals(JSplitPane.LAST_DIVIDER_LOCATION_PROPERTY)) {
int current = sourceSplitPane.getDividerLocation();
System.out.println("Current: " + current);
Integer last = (Integer) changeEvent.getNewValue();
System.out.println("Last: " + last);
Integer priorLast = (Integer) changeEvent.getOldValue();
System.out.println("Prior last: " + priorLast);
}
}
};
splitPane.addPropertyChangeListener(propertyChangeListener);
frame.add(splitPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
Moving the JSplitPane Divider
Reset the divider position to that position by calling the resetToPreferredSizes() method of JSplitPane.
- Change the dividerLocation property with setDividerLocation(newLocation).
- 0.0 and 1.0, representing a percentage of the JSplitPane container width.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class MovingJSplitPaneDivider {
public static void main(String[] a) {
JFrame horizontalFrame = new JFrame();
horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent topButton = new JButton("Left");
JComponent bottomButton = new JButton("Right");
final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(topButton);
splitPane.setBottomComponent(bottomButton);
horizontalFrame.add(splitPane, BorderLayout.CENTER);
horizontalFrame.setSize(150, 150);
horizontalFrame.setVisible(true);
splitPane.setDividerLocation(0.5);
}
}
With the system-provided look and feel classes, pressing the F8 key allows you to move the divider with the keyboard keys such as Home, End, or the arrows. F8 isn"t a modifier like Shift or Alt.
Nested JSplitPane
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
public class NestedJSplitPane{
public static void main(String[] a) {
int HORIZSPLIT = JSplitPane.HORIZONTAL_SPLIT;
int VERTSPLIT = JSplitPane.VERTICAL_SPLIT;
boolean continuousLayout = true;
JLabel label1 = new JLabel("a");
JLabel label2 = new JLabel("b");
JLabel label3 = new JLabel("c");
JSplitPane splitPane1 = new JSplitPane(VERTSPLIT, continuousLayout, label1, label2);
splitPane1.setOneTouchExpandable(true);
splitPane1.setDividerSize(2);
splitPane1.setDividerLocation(0.5);
JSplitPane splitPane2 = new JSplitPane(HORIZSPLIT, splitPane1, label3);
splitPane2.setOneTouchExpandable(true);
splitPane2.setDividerLocation(0.4);
splitPane2.setDividerSize(2);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(splitPane2);
frame.pack();
frame.setVisible(true);
}
}
Resizing Components and Working with a One-Touch Expandable Divider
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class TouchExpandableSplitPane {
public static void main(String args[]) {
JFrame frame = new JFrame("Property Split");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setContinuousLayout(true);
splitPane.setOneTouchExpandable(true);
JComponent topComponent = new JButton("A");
splitPane.setTopComponent(topComponent);
JComponent bottomComponent = new JButton("B");
splitPane.setBottomComponent(bottomComponent);
frame.add(splitPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
set Divider Location for JSplitPane
import java.awt.ruponent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("SplitPaneFrame");
JLabel leftImage = new JLabel(new ImageIcon("a.gif"));
Component left = new JScrollPane(leftImage);
JLabel rightImage = new JLabel(new ImageIcon("b.gif"));
Component right = new JScrollPane(rightImage);
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
split.setDividerLocation(100);
frame.getContentPane().add(split);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Setting Orientation: JSplitPane.VERTICAL_SPLIT or JSplitPane.HORIZONTAL_SPLIT
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class JSplitPaneVerticalSetLeftRight {
public static void main(String[] a) {
JFrame horizontalFrame = new JFrame();
horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent leftButton = new JButton("Left");
JComponent rightButton = new JButton("Right");
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setLeftComponent(leftButton);
splitPane.setRightComponent(rightButton);
horizontalFrame.add(splitPane, BorderLayout.CENTER);
horizontalFrame.setSize(150, 150);
horizontalFrame.setVisible(true);
}
}
The split pane supports a one-touch-expandable capability that allows the user to conveniently move the divider to either end with a single click
import javax.swing.JButton;
import javax.swing.JSplitPane;
public class Main {
public static void main(String[] argv) throws Exception {
JButton topComponent = new JButton("top");
JButton bottomComponent = new JButton("bottom");
JSplitPane vpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topComponent, bottomComponent);
boolean b = vpane.isOneTouchExpandable(); // false by default
vpane.setOneTouchExpandable(true);
}
}