0

Been a while since I've been here. I'm learning java and have a question as to why the panel I've created in a JSplitPane can be resized beyond the maximum that I've set:

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


public class MainWindow {
 
 
//set all components
 

 

JFrame frame;
JPanel showPanel;//displays individual contact when clicked on in the contacts panel;
JPanel listPanel;// displays the contactsPanel
    JSplitPane contactsSplitPane;
             
public void buildMainWindow() {// open method       
     
    frame = new JFrame("Contacts");
    frame.setBackground(Color.WHITE);
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     
    showPanel = new JPanel();
            showPanel.setBackground(Color.WHITE);
            
            listPanel = new JPanel();
            listPanel.setBackground(Color.LIGHT_GRAY);
            listPanel.setMaximumSize(new Dimension (300,1000));
            
            
            //create SplitPane for the listPanel and showPanel
    contactsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listPanel,showPanel);
    contactsSplitPane.setOneTouchExpandable(true);
    contactsSplitPane.setDividerLocation(50);   
         
    frame.setSize(1000, 1000);    
    frame.setVisible(true);
            frame.add(BorderLayout.CENTER, contactsSplitPane);
 }//close method
 
public static void main (String [] args) {
    MainWindow MainWindow = new MainWindow ();
    MainWindow.buildMainWindow();
}
 
}// close class

feel free to run and compile. I've set the size of the listPanel to a maximum of 300 pixels, but I can resize it way beyond that -- almost to the end of the frame. It's not possible to crate a single resizable pane, no?

Can someone let me know what I'm doing wrong? I'm obviously missing something, but I don't know what.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • This question was originally closed as a duplicate of: https://stackoverflow.com/questions/10157954/java-swing-setmaximumsize-not-working which deals with a JFrame, not a JSplitPane. I reopened the question so we can have answers related to a JSplitPane. – camickr Jun 15 '22 at 01:53

1 Answers1

1

A JSplitPane doesn't respect the maximum size of either component.

However, it does respect the minimum size of a component.

So one approach could be do set the minimum size on the other component added to the split pane. You will need to override the getMinimumSize() method of this component since the size of the split pane can change dynamically.

Something like:

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

public class SplitPaneMaximum extends JPanel
{
    JSplitPane splitPane;

    public SplitPaneMaximum()
    {

        setLayout( new BorderLayout() );

        JPanel red = new JPanel();
        red.setBackground( Color.RED );
        red.setPreferredSize( new Dimension(200, 100) );
        red.setMinimumSize( new Dimension(100, 0) );

        JPanel blue = new JPanel()
        {
            //  Setting a minimum size here will limit the maximum size
            //  of the other component added to the split pane

            @Override
            public Dimension getMinimumSize()
            {
                int parentWidth = getParent().getSize().width;
                Dimension d = getSize();
                d.width = parentWidth - 200;

                return d;
            }
        };

        blue.setBackground( Color.BLUE );
        blue.setPreferredSize( new Dimension(200, 100) );

        splitPane = new JSplitPane();
        splitPane.setLeftComponent( red );
        splitPane.setRightComponent( blue );
        splitPane.setResizeWeight(0.50);
        add(splitPane, BorderLayout.CENTER);
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("SplitPaneMaximum");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SplitPaneMaximum() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Now the width of the red panel can only be sized between 100 and 200 pixels.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Okay -- so then why is there a setMaximumSize() method at all if it's not respected? – Straitsfan Jun 15 '22 at 23:55
  • The size methods are only hints for the layout manager. It is up to the layout manager to determine if it wants to use/ignore the size hints. I would guess the internal layout manager of the JSplitPane was written to only respect the preferred and minimum sizes. – camickr Jun 16 '22 at 00:20