Reputation: 1
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.
Upvotes: 0
Views: 467
Reputation: 324128
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.
Upvotes: 2