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.
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 ;
}
};
splitPane.setEnabled( false );
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 ;
}
};
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 and @Dennis van Beek answers didn't work for me because user still can grab divider and move it while cursor remains default (which should be considered as inability of interaction).
Even if it worked, i still was in need of OneTouchExpandable which will be disabled because of Divider
beeing disabled. So, what i did:
A custom
BasicSplitPaneUI
including customDivider
and method to add it to BasicSplitPaneUI. Custom BasicSplitPaneUI also contains overridden methods:startDragging()
,dragDividerTo(int location)
,finishDraggingTo(int location)
to do nothing:private class SplitPanelUI extends BasicSplitPaneUI{ public SplitPanelUI(){ super(); } protected void setDivider(int dividerSize) { divider = new Divider(this); divider.setDividerSize(dividerSize); } @Override protected void startDragging(){ } @Override protected void dragDividerTo(int location) { } @Override protected void finishDraggingTo(int location) { } }
Added Custom
Divider
with changedСursor
while mouse hovering on it:private class Divider extends BasicSplitPaneDivider { public Divider(BasicSplitPaneUI ui) { super(ui); setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } }
Usage example:
SplitPane sp = new SplitPane(JSplitPane.HORIZONTAL_SPLIT, JPanel1, JPanel2);
sp.setOneTouchExpandable(true);
sp.setEnabled(true);
SplitPanelUI ui = new SplitPanelUI();
ui.installUI(this);
ui.setDivider(dividerSize);
sp.setUI(ui);
this.setResizeWeight(1); //percentage (or use setDividerLocation)
//setDividerLocation(200); //in pixels
I hope it would help someone.
@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);
}
}
© 2022 - 2024 — McMap. All rights reserved.