Search code examples
javaswingjsplitpane

How to equally split a JSplitPane?


So i am using Java and Swing and am trying to program a window with a JSplitPane equally split on each side. I have got the JSplitPane, however one side is nearly the full size of the window and the other is tiny.

package com.harrykitchener.backup;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Main 
{
    private JMenuBar menuBar;
    private JMenu fileMenu, editMenu, helpMenu;
    private JPanel leftPanel, rightPanel;
    private JButton openButton;

    public Main()
    {
        JPanel mainCard = new JPanel(new BorderLayout(8, 8));
        menuBar = new JMenuBar();
        fileMenu = new JMenu("File");
        editMenu = new JMenu("Edit");
        helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        mainCard.add(menuBar);

        leftPanel = new JPanel();

        rightPanel = new JPanel();


        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);

        JFrame window = new JFrame("Pseudo code text editor");
        window.setJMenuBar(menuBar);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(splitPane);
        window.setSize(1280, 720);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main();
            }
        });
    }

}

enter image description here


Solution

  • Use

    splitPane.setResizeWeight(0.5);
    

    Please see the java doc for more information.