Mike
Mike

Reputation: 305

Set JSplitPane divider location from the right

As far as I know, the setDivider() method of JSplitPane can take an int which is an absolute position from the left. For example, someSplitPane.setDivider(120); will set the divider 120px from the left. Is there any way I can do the same thing, but set the divider an absolute position from the right?

Simple implementation:

public class Window extends JFrame {
    
    JSplitPane splitpane;
    
    public Window() {
        initComponents();
    }
    
    private void initComponents() {
        setTitle("Debugging");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(640, 480));
        
        splitpane = new JSplitPane();
        System.out.println(splitpane.getSize().width); // prints 0
        splitpane.setDividerLocation(splitpane.getSize().width - 120);
        getContentPane().add(splitpane);
    }
    
    public static void main(String[] args) {
        
        SwingUtilities.invokeLater(() -> {
            new Window().setVisible(true);
        });
    }
}

EDIT: I have written this code isolated from any existing code, and I reproduce the error. What looks to be happening is the JFrame is instantiating and appears on the desktop, and then about a second later, a 0 (from the commented print statement) is output to the console.

Upvotes: 0

Views: 630

Answers (2)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51515

I made a few changes to your code and created this GUI.

JSplitPane Right

I used a JFrame instead of extending a JFrame.

I set the preferred size of the JSplitPane, rather than the JFrame. Generally, you care more about the size of the JSplitPane than the JFrame. Usually, the JSplitPane will have two JPanels with Swing components, so you don't have to specify a preferred size at that point.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

public class JSplitPaneRight {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new JSplitPaneRight();
        });
    }
    
    public JSplitPaneRight() {
        initComponents();
    }
    
    public void initComponents() {
        JFrame frame = new JFrame("JSplitPane Right");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createSplitPane(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JSplitPane createSplitPane( ) {
        JSplitPane splitpane = new JSplitPane();
        splitpane.setPreferredSize(new Dimension(640, 480));
        int width = splitpane.getPreferredSize().width;
        System.out.println(width); // prints 0
        splitpane.setDividerLocation(width - 120);
        
        return splitpane;
    }

}

Upvotes: 1

camickr
camickr

Reputation: 324128

Assuming you create your GUI correctly by using the EDT you can add the following in the constructor of your class where you create the splitPane:

SwingUtilities.invokeLater(() ->
{
        Dimension d = splitPane.getSize();
        splitPane.setDividerLocation(d.width - 120);
});

This will add code to the EDT. So after the frame is visible and the split pane has been given a size, the divider location will be reset.

Upvotes: 1

Related Questions