A split pane is a special container that holds two components, each in its own subpane. A splitter bar adjusts the sizes of the two subpanes. In a document viewer, for example, you might use a split pane to show a table of contents next to a page of text.
The following example uses two JLabel
s containing ImageIcon
s, like the previous example. It
displays the two labels, wrapped in JScrollPanes
, on either side of a JSplitPane
(see Figure 17-10). You can drag the splitter bar back
and forth to adjust the sizes of the two contained components.
//file: SplitPaneFrame.java
import
java.awt.*
;
import
java.awt.event.*
;
import
javax.swing.*
;
import
javax.swing.border.*
;
public
class
SplitPaneFrame
{
public
static
void
main
(
String
[]
args
)
{
String
fileOne
=
"Piazza di Spagna.jpg"
;
String
fileTwo
=
"L1-Light.jpg"
;
if
(
args
.
length
>
0
)
fileOne
=
args
[
0
];
if
(
args
.
length
>
1
)
fileTwo
=
args
[
1
];
JFrame
frame
=
new
JFrame
(
"SplitPaneFrame"
);
JLabel
leftImage
=
new
JLabel
(
new
ImageIcon
(
fileOne
)
);
Component
left
=
new
JScrollPane
(
leftImage
);
JLabel
rightImage
=
new
JLabel
(
new
ImageIcon
(
fileTwo
)
);
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
);
}
}
Get Learning Java, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.