JSplitPane Example
A JSplitPane contains two JPanels, horizontally or vertically aligned. In this JSplitPane example we align them vertically.
A central bar allows the user to adjust the amount showing in each JSplitPane. This puts the user in control of screen resource management and is to be found in tools such as email programs in which long lists of information (perhaps names or addresses) need to be dealt with.
// JSplitPane Example
import java.awt.*;
import javax.swing.*;
class JSplitPaneExample {
static JPanel createPanel(String[] sa) {
JPanel p = new JPanel(new GridLayout(0,1));
for(int i=0;i<sa.length;++i) p.add(new JLabel(sa[i]));
return p;
}
public static void main(String[] args) {
JPanel p1 = createPanel(new String[]{
"James Dean","Johnny Depp","Scarlett Johansson",
"Keira Knightley","Robert Carlyle","Ewan McGregor",
"Sean Connery","Leonardo di Caprio"}
);
JPanel p2 = createPanel(new String[]{
"East of Eden","Sweeney Todd","The Other Boleyn Girl",
"Atonement","28 Weeks Later","The Long Way Down",
"From Russia with Love","Revolutionary Road"}
);
final JSplitPane jSplitPane =
new JSplitPane(JSplitPane.VERTICAL_SPLIT, p1, p2);
jSplitPane.setContinuousLayout(true);
JFrame f = new JFrame("JSplitPane");
f.getContentPane().add(jSplitPane);
f.setSize(180, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
