JSplitPane: maximum doesn't work.
I have a vertical splitPane (i.e. divider splits to top and bottom component). Now I want the divider to behave that the bottom component is not allowed to get bigger than a maximum height. So I did:
//result is the splitPane's "newRightComponent"
result.setMaximumSize(new Dimension(300, 300));
result.setPreferredSize(result.getMaximumSize());
result.setSize(result.getMaximumSize());
But that doesn't work. Instead the divider can still be moved up to the top border and about 300 (?) dots above the bottom border. (Yes, my "result" object is the componentn that is placed at the bottom.)
Is there a way to tell splitpane that the right/bottom component should always have a fixed width/height?
# 1
> Is there a way to tell splitpane that the right/bottom component should always have a fixed width/height?
The point of using a split pane is so that the size can vary. If you want a fixed size then use a BorderLayout and add your fixed size component to the SOUTH or EAST and the variable sized component to the CENTER.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
# 2
Nope, I want a splitPane because the user should be able to reduce the size of the lower panel - up do hide it - so splitPane is what I like. A BorderLayout just displays it.
# 3
> the user should be able to reduce the size of the lower panel
Thats not what your question says. You said it should have a "fixed" width/height.
Read the JSplitPane API. It says you can set the minimum size. So do the opposite of what you are currently doing. Set the minimum size of the top/left component so the bottom/right component can't grow too big.
# 4
I can't set the minimum size of the top component as I don't know it. The top component is dynamic in size. Therefore I use a JSCrollpane to display the componentn that can grow/shrink. SetDividerLocation also doesn't help as 1.0/00 just resizes it to full or hides it completely. It seems my requirement for JSplitPane can't be realized. The lower component should be of fixed size (height) but the user should also be able to hide this component. I thought of using JSplitPane as it has those OneTouchExpandable arrows.
# 5
Are you using JScollPane as your top component or as a container for the JSplitPane?
# 6
My top component is a JScrollPane. It holds a JPanel that grows shrinks in any size (i.e. the generated fractal image can be of any dot size/resolution). The bottom component is a JPanel with a GridBagLayout showing standard Swing component like buttons, textfields, etc. It just needs a fixed height.
So, even when I resize the frame the bottom component should always stay the same (either fixed height or not-expanded). I wasn't able to achieve this. I can just set it up at the beginning when the frame is created with an initial image size at the top. Then I can set a fixed frame size (height of top image + wanted height of bottom component), calculate the minimum height of the top scrollpane and show it. But as soon as I resize the frame and the top scrollpane doesn't scroll anymore, the minimum size is exceeded ans the bottom component gets more space in height.
# 7
See if my example can help you:
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(300, 300);
final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JPanel panel1 = new JPanel();
final JPanel panel2 = new JPanel();
splitPane.setOneTouchExpandable(true);
splitPane.setTopComponent(panel1);
splitPane.setBottomComponent(panel2);
panel2.setPreferredSize(new Dimension(300, 100));
panel1.setPreferredSize(new Dimension(300, 200));
splitPane.setEnabled(false);
frame.getContentPane().add(splitPane);
frame.setVisible(true);
frame.pack();
splitPane.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
splitPane.setDividerLocation(splitPane.getHeight()-panel2.getPreferredSize().height);
}
});
# 8
> The top component is dynamic in size.
So you use a ComponentListener to reset the minimum size as the parent size changes (as is demonstrated above). If you had posted a SSCCE like you where asked I might have even given you the code, but I'm not going to create a test program to see how it works because you are too lazy to post the SSCCE as you've been asked many times before.
Or if you read the API description like I suggested earlier you would also see a solution that allows you to control which component gets resized when the split pane is resized.
# 9
Setting the top component minimum size does not help when the component changes size.
I guess the divider location calculation does not use this parameter.
camickr, you are right about the SSCCE, MartinHilpert just enjoyed the fact that I'm on a sick day and have nothing better to do...