How to make JSplitPane
to resizable false
? I didn't want to resize the JSplitPane
, I used it for the border of this pane. Is there any other way to create same border structure to split a panel vertically into two parts.
-
3Disable the split pane. See below.– camickrAug 15, 2011 at 15:15
7 Answers
splitPane.setEnabled( false );
-
-
-
2This disables subcomponents if they inherit the parent status of enabled/disable. e.g. a jlist may be disabled from being able to be selected.– MgamerzJun 23, 2015 at 0:16
-
1@Mgamerz, a JList works fine for me using JDK8 on Windows 7. The setEnabled() method only applies to the component, not child components. For example see the Swing tutorial on How to Use Split Panes for an example with a JList.– camickrJun 23, 2015 at 3:34
-
@camickr When I disable this split pane some of my sub components are no longer selectable. I cannot pick items in a Jlist and a sub-splitpane doesn't have the cursor change to indicate it is movable while it still is. Removing the disable makes it work normally again.– MgamerzJun 23, 2015 at 5:23
You can override the JSplitPane methodes getDividerLocation()
and getLastDividerLocation
and return a constant value.
JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT){
private final int location = 100;
{
setDividerLocation( location );
}
@Override
public int getDividerLocation() {
return location ;
}
@Override
public int getLastDividerLocation() {
return location ;
}
};
-
-
1@camickr But that's exactly what this solution does, it extends JSpiltPane with an anonymous class. Jul 9, 2014 at 12:57
For preventing users to resize the panes you can also set the divider size to zero.
splitPane.setDividerSize(0);
final double pos = split.getDividers().get(0).getPosition();
split.getDividers().get(0).positionProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> arg0,
Number arg1, Number arg2) {
split.getDividers().get(0).setPosition(pos);
}
});
As stated un @camickr answer comments, disabling the whole split pane can disable contained components interactive behavior (for example, they won't show their interactive cursors on hover)
instead, if using BasicSplitPaneUI, you can disable the divider from the UI
public class MySplitPane extends JSplitPane {
public void setResizable(boolean resizable) {
BasicSplitPaneUIui = (BasicSplitPaneUI) this.getUI();
ui.getDivider().setEnabled(resizable);
}
}
@TrogloGeek's answer works best, to avoid problems with the disabled splitpane.
For example, if you want to make a pane that is 'onetouchexpandable', but not resizable, you can use this:
public class FixedExpandableSplitPane extends JSplitPane {
public FixedExpandableSplitPane(int orientation) {
super(orientation);
setOneTouchExpandable(true);
BasicSplitPaneUI ui = (BasicSplitPaneUI) this.getUI();
ui.getDivider().setEnabled(false);
}
}