Subpages: 1. Split Panes: JSplitPane
2. Basic split pane example
3. Gas model simulation using a split pane
8.2 Basic split pane example
This basic introductory demo shows JSplitPane at work. We can manipulate four simple custom panels placed in three JSplitPanes:

Figure 8.1 Split Pane example displaying simple custom panels.
<<file figure8-1.gif>>
The Code: SplitSample.java
see \Chapter8\1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SplitSample extends JFrame{
protected JSplitPane m_sp;
public SplitSample() {
super("Simple SplitSample Example");
setSize(400, 400);
Component c11 = new SimplePanel();
Component c12 = new SimplePanel();
JSplitPane spLeft = new JSplitPane(
JSplitPane.VERTICAL_SPLIT, c11, c12);
spLeft.setDividerSize(8);
spLeft.setContinuousLayout(true);
Component c21 = new SimplePanel();
Component c22 = new SimplePanel();
JSplitPane spRight = new JSplitPane(
JSplitPane.VERTICAL_SPLIT, c21, c22);
spRight.setDividerSize(8);
spRight.setContinuousLayout(true);
m_sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
spLeft, spRight);
m_sp.setContinuousLayout(false);
m_sp.setOneTouchExpandable(true);
getContentPane().add(m_sp, BorderLayout.CENTER);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String argv[]) {
new SplitSample();
}
}
class SimplePanel extends JPanel{
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public Dimension getMinimumSize() {
return new Dimension(40, 40);
}
public void paintComponent(Graphics g) {
g.setColor(Color.black);
Dimension sz = getSize();
g.drawLine(0, 0, sz.width, sz.height);
g.drawLine(sz.width, 0, 0, sz.height);
}
}
Understanding the Code
Class SplitSample
Four instances of SimplePanel (see below) are used to fill a 2x2 structure. Two left components (c11 and c12) are placed in the vertically split spLeft panel. The two right components (c21 and c22) are placed in the vertically split spRight panel. The spLeft and spRight panels are placed in the horizontally split m_sp panel.
Several properties are assigned to demonstrate JSplitPane's behavior. The continuousLayout property is set to true for spLeft and spRight, and false for m_sp panel. So as the divider moves inside the left and right panels, child components are repainted continuously, producing immediate results. However, as the vertical divider is moved it is denoted by a black line until a new position is chosen (i.e. the mouse is released). Only then are its child components validated and repainted. The first kind of behavior is recommended for simple components that can be rendered quickly, while the second is recomended for components whose repainting can take a significant amount of time.
Also note that the oneTouchExpandable property is set to true for the vertical JSplitPane. This places small arrow widgets on the divider. By pressing these arrows with the mouse we can instantly move the divider to the leftmost or rightmost position. When in the left or right-most positions, pressing these arrows will then move the divider to its most recent location, maintained by the lastDividerLocation property.
Class SimplePanel
SimplePanel represents a simple Swing component to be used in this example. Method paintComponent() draws two diagonal lines across this component. Note that the overridden getMinimumSize() method defines the minimum space required for this component. JSplitPane will prohibit the user from moving the divider if the resulting child size will become less than its minimum size.
Note: The arrow widgets associated with the oneTouchExpandable property will move the divider to the extreme location without regard to minimum sizes of child components.
Running the Code
Note how child components can be resized with dividers. Also note the difference between resizing with continuous layout (side panes) and without it (center pane). Play with the "one touch expandable" widgets for quick expansion and collapse.



RSS feed Java FAQ News