开发者

JSplitPane set resizable false

How to make JSplitPane to resizable false? I didn't want to r开发者_JAVA技巧esize 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.


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);


Consider for using Compound Borders with EtchedBorder


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜