Double JSplitPane swing

101 views Asked by At

I have a double JSplitPane like below:

-------------------------------------
|       |                   |       |
|       |                   |       |
|       |                   |       |
|       |                   |       |
|       |                   |       |
|       |                   |       |
|       |                   |       |
|       |                   |       |
|       |                   |       |
-------------------------------------

By the following code:

import javax.swing.*;
import java.awt.*;

public class Example {

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Hello this is my MVCE");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(800, 600));
        frame.setFocusable(true);

        JSplitPane outer= new JSplitPane();
        outer.setResizeWeight(0.2);
        outer.setLeftComponent(new JLabel());

        JSplitPane innerPane= new JSplitPane();
        innerPane.setResizeWeight(0.725);
        innerPane.setLeftComponent(new JLabel());
        innerPane.setRightComponent(new JLabel());
        outer.setRightComponent(innerPane);

        frame.add(outer);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

I want the left and side columns to be 20% width each and the middle column to be 60% width.

When I run my code, It looks like the ratio of these split panes is roughly 0.2:0.6:0.2 but I want this to be perfect.

I notice this ratio changing when I resize the frame alot of times (inside-out-etc), specifically I see that the left column is larger than the right column. How can I get the left and right columns to be the same width?

Thanks

0

There are 0 answers