Java Reference
In-Depth Information
Table 11-2.
JSplitPane Properties (Continued)
Property Name
Data Type
Access
oneTouchExpandable
boolean
Read-write bound
orientation
int
Read-write bound
resizeWeight
double
Read-write bound
rightComponent
Component
Read-write
topComponent
Component
Read-write
validateRoot
boolean
Read-only
UI
SplitPaneUI
Read-write bound
UIClassID
String
Read-only
Setting Orientation
Besides initializing the orientation within the constructor, you can change the
JSplitPane
orientation by changing the orientation property setting to either
JSplitPane.VERTICAL_SPLIT
or
JSplitPane.HORIZONTAL_SPLIT
. If you try to change the property to a nonequivalent setting,
an
IllegalArgumentException
is thrown.
Dynamically changing the orientation at runtime is not recommended because it can
confuse a user. However, if you're using a visual development tool, you can explicitly set the
orientation for this property after creating the
JSplitPane
. When not programming visually,
you would normally initialize the orientation when you create the
JSplitPane
.
Changing JSplitPane Components
There are four read-write properties for the different positions of a component within a
JSplitPane
:
bottomComponent
,
leftComponent
,
rightComponent
, and
topComponent
. In reality,
these four properties represent two components internally: The left and top components are
one; the right and bottom components represent the other.
You should use the properties that are appropriate for the orientation of your
JSplitPane
.
Using the inappropriate property methods can make life difficult for the maintenance
programmer. Imagine, after creating a user interface, seeing something like the following code
six months later:
JComponent leftButton = new JButton("Left");
JComponent rightButton = new JButton("Right");
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setLeftComponent(leftButton);
splitPane.setRightComponent(rightButton);
If you glance at the source, you might think that the screen will contain a button to the left
and one to the right based on the variable names and the
set
XXX
Component()
methods used.
But because the instantiated
JSplitPane
has a vertical orientation, the interface that's created
looks like Figure 11-6. The variable names are used because of the button labels, not their position.