开发者

How to lock JSplitPane divider when swapping components?

I have a simple extended JSplitPane that I set different panels to at different times when they are needed. Specifically, I split it into an upper and lower section, and I swap out the bottom section frequently. Each time I do, I reset the slider position to how I want it, but sometimes it jumps off at and re-positions itself to the top of the screen (not always).

Here's my code:

public class MainPanel extends JSplitPane{

    public Screen screen;

    public int height;

    public ControlPanel curPanel;

    public MainPanel(Screen screen, int height){
        super(JSplitPane.VERTICAL_SPLIT);

        this.screen = screen;
        this.height = height;

        setDividerSize(2);
        setEnabled(false);

        setTopComponent(screen);

        setToInitControls();
    }

    public void setToInitControls(){
        InitControls initCtrls = new InitControls(this);
        setBottomComponent(initCtrls);
        curPanel = initCtrls;
        setDividerLocation(height / 4 * 3);
    }

    public void setToConfigControls(){
        ConfigControls configCtrls = new ConfigControls(this);
        setBottomComponent(configCtrls);
        curPanel = configCtrls;
        setDividerLocation(height / 4 * 3);
    }

    public void setToWaitControls(){
        WaitControls waitCtrls = new WaitControls(this);
        se开发者_如何学GotBottomComponent(null);
        setBottomComponent(waitCtrls);
        curPanel = waitCtrls;
        setDividerLocation(height / 4 * 3);
    }

    //and so on (I have more methods like these further down)

    //OVERRIDES: I figured overriding these might help. It didn't.
    @Override
    public int getMinimumDividerLocation(){
        return (height / 4 * 3);
    }
    @Override
    public int getMaximumDividerLocation(){
        return (height / 4 * 3);
    }
}

Basically, I use the "setTo...Controls()" methods to swap bottom panels. Is there a way to tell the slider to stay put where I placed it regardless of the panel's preferred sizes, or if not, how do I make the panels know what to shape themselves to fit in? Thanks for any/all suggestions!

EDIT: I should note that these panels do not use layouts. They are custom panels that I use mouse/keyboard listeners on and use my own graphics to paint over them.


I found the solution, thanks to the links above. It's actually quite simple. Instead of using

setDividerLocation(height / 4 * 3);

for every time I added a component, I just replaced it with:

setResizeWeight(0.66);

Did that once inside the constructor, and it never bothered me again. 0.66 is the equivalent decimal position to h/4*3 (I just trial-and-errored it).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜