I have a problem that I have "solved" in that it now does what I want, but I'd like to know why it works. I have a JFrame. Within that JFrame I have three JPanels, which I'd like the user to be able to adjust. I would like to be able to assign the dimensions of these JPanels exactly. To adjust the sizes, the user drags two JSplitPane dividers. The dimensions were continually off by one pixel until I added 1 to lines 46 and 47 below to get them right, but I am not sure why I had to do that. Here's the program.

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


public class SplitPaneGUI extends JFrame
{
    static final int ORIG_RIGHT_WIDTH = 650;
    static final int ORIG_RIGHT_HEIGHT = 350;
    static final int ORIG_LEFT_WIDTH = 250;
    static final int ORIG_LEFT_HEIGHT = 350;
    static final int ORIG_TOP_WIDTH = ORIG_LEFT_WIDTH + ORIG_RIGHT_WIDTH;
    static final int ORIG_TOP_HEIGHT = 200;
    
    JSplitPane wp, bp;
    JPanel tp, rp, lp;
    
    int verticalOffset;
    int horizontalOffset;
    int frameWidth;
    int frameHeight;

    public SplitPaneGUI () 
    {
        tp = new JPanel ();
        rp = new JPanel ();
        lp = new JPanel ();
        
        // first try.  Will definitely be wrong.  Need to get the vertical and
        // horizontal offsets.
        
        tp.setSize (ORIG_TOP_WIDTH, ORIG_TOP_HEIGHT);        
        rp.setSize (ORIG_RIGHT_WIDTH, ORIG_RIGHT_HEIGHT);
        lp.setSize (ORIG_LEFT_WIDTH, ORIG_LEFT_HEIGHT);
        
        frameWidth = ORIG_TOP_WIDTH;
        frameHeight = ORIG_RIGHT_HEIGHT + ORIG_TOP_HEIGHT;
        
        this.setSize (frameWidth, frameHeight);        

        this.setVisible (true);
        this.setTitle ("Split Pane GUI Experiment");

        bp = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, lp, rp);
        wp = new JSplitPane (JSplitPane.VERTICAL_SPLIT, tp, bp);
               
        bp.setDividerLocation (ORIG_LEFT_WIDTH + 1); // added 1 here to "fix"
        wp.setDividerLocation (ORIG_TOP_HEIGHT + 1); // added 1 here to "fix"
        
        this.add (wp);
        this.validate ();
        
        // get offsets and reset sizes based on them.
        
        horizontalOffset = this.getWidth () - lp.getWidth () - rp.getWidth ();
        verticalOffset = this.getHeight () - lp.getHeight () - tp.getHeight ();
        
        frameWidth = frameWidth + horizontalOffset;
        frameHeight = frameHeight + verticalOffset;
        this.setSize (frameWidth, frameHeight);
        validate ();
         
        System.out.println ("Frame Dimensions " + this.getWidth () + " x " + this.getHeight ());
        System.out.println ("Top Dimensions   " + tp.getWidth ()   + " x " + tp.getHeight ());
        System.out.println ("Left Dimensions  " + lp.getWidth ()   + " x " + lp.getHeight ());
        System.out.println ("Right Dimensions " + rp.getWidth ()   + " x " + rp.getHeight ());
        System.out.println ("Vertical Divide Line " + bp.getDividerLocation());
        System.out.println ("Horizontal Divide Line " + wp.getDividerLocation());
    }
    
    
    public static void main (String args[])
    {
        new SplitPaneGUI ();
    }
}

Here are the results:

Frame Dimensions 922 x 598
Top Dimensions   912 x 200
Left Dimensions  250 x 350
Right Dimensions 650 x 350
Vertical Divide Line 251
Horizontal Divide Line 201

My question is: shouldn't the green and red values above match each other? Why do they vary by one? I imagine it has to do with whether the top/left pixel is considered pixel 0 or pixel 1, but that is just a guess and if anyone can confirm, that'd be great. Thank you.

Recommended Answers

All 2 Replies

The top left pixel is 0,0

The divider itself, however, also has a width, and I believe the "location" for the divider is probably based on the "center" of the divider. Try using "getDividerSize", and some math for the adjustment.

The top left pixel is 0,0

The divider itself, however, also has a width, and I believe the "location" for the divider is probably based on the "center" of the divider. Try using "getDividerSize", and some math for the adjustment.

Thank you for responding, masijade, and for confirming that pixels start at 0, not 1. I'm going to research this further, and experiment some more and try to nail it down. I will report back what I learn. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.