JSplitPane set resizable false
Asked Answered
T

8

10

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.

Tropic answered 15/8, 2011 at 13:10 Comment(1)
Disable the split pane. See below.Mammilla
S
7

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 ;
    }
};
Subarctic answered 15/8, 2011 at 13:27 Comment(2)
@Tapas Bose, no need to extend the split pane class.Mammilla
@Mammilla But that's exactly what this solution does, it extends JSpiltPane with an anonymous class.Nonagenarian
M
27
splitPane.setEnabled( false );
Mammilla answered 15/8, 2011 at 15:15 Comment(6)
but with nimbus i see no border if it is disabled. :(Subarctic
this solution is simple and awesomeUnman
This disables subcomponents if they inherit the parent status of enabled/disable. e.g. a jlist may be disabled from being able to be selected.Stopwatch
@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.Mammilla
@Mammilla 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.Stopwatch
@Mgamerz, You have custom code that isn't working properly. I gave you a link to the demo code from the Swing tutorial and I already stated it works fine for me. If the tutorial code doesn't work then m\Maybe you have a version / OS bug. This is NOT the way enable is designed to work. That is you can't use disabled on a JPanel to disable all components on the panel. If you have a question then create a proper SSCCE and post a real question on the forum. Stating something doesn't work without posting code means nothing. It is probably your code, not Swing.Mammilla
S
7

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 ;
    }
};
Subarctic answered 15/8, 2011 at 13:27 Comment(2)
@Tapas Bose, no need to extend the split pane class.Mammilla
@Mammilla But that's exactly what this solution does, it extends JSpiltPane with an anonymous class.Nonagenarian
J
7

For preventing users to resize the panes you can also set the divider size to zero.

splitPane.setDividerSize(0);
Jennie answered 17/10, 2016 at 8:34 Comment(0)
K
3

Consider for using Compound Borders with EtchedBorder

Keep answered 15/8, 2011 at 13:23 Comment(0)
P
2
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);
    }
});
Plastered answered 13/11, 2014 at 11:46 Comment(0)
C
1

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);
    }
}
Chiffonier answered 31/1, 2019 at 10:48 Comment(0)
S
1

@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:

  1. A custom BasicSplitPaneUI including custom Divider 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) {
         }
     }
    
  2. 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.

Sitra answered 28/7 at 18:54 Comment(0)
G
0

@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);
    }
}
Godhood answered 13/5, 2022 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.