Resize Control with Java JSplitPane

Theron084 picture Theron084 · Nov 24, 2011 · Viewed 10.3k times · Source

Please see my image below at the link and then read below it for more details on my problem.

Sample image

Imagine that is a Basic frame splited into two with a JSplitPane, by default when you resize your frame the gray part changes it's size, but I would like the white part to resize accordingly to the frame resizing.

Any help into the right direction would be appreciated as I am working on a project now and I am trying out all kind of weird stuff to be prepared for my biggest project set in the new year. :)

Regards Theron

Answer

mprabhat picture mprabhat · Nov 24, 2011

You need to use setResizeWeight to get the left and right take the extra space or reduce in size on JFrame resize, sample code below:

    import java.awt.BorderLayout;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JSplitPane;

    public class TestJSplitPane {
    private void init(){
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setRightComponent(new JButton("Here I Am"));
        splitPane.setLeftComponent(new JButton("Me Too"));
        splitPane.setResizeWeight(0.5);
        frame.add(splitPane, BorderLayout.CENTER);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TestJSplitPane().init();
    }
}

Java Doc for setResizeWeight.

Hope this helps.