Question

On my application I got the frame containing a JSplitPane. The JSplitPane is set to split horizontally. On left side there is a panel containing some components that do not matter. On the right I got a JPanel(BoxLayout) containing subclass of JTextArea inside a JScrollPane,a JTextPane inside a JScrollPane, and a JButton.

My problem is how I get the components(mainly on the right) to resize, based on interaction with JSplitPane. So when the JSplitPane is dragged to the left, my JTextArea and JTextPane get wider.

I have tried different setups, also setting the preferredSize and MaximumSize but none of this seems to work. Components stay at their preferredSize. If I try to make them smaller the scrollPane works (which is fine) but when I try to get them bigger they stay at preferred size.

Was it helpful?

Solution

Most likely your problem is due to the BoxLayout used on your right panel. Copy-paste from the class javadoc:

BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout). For a horizontal layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component. If that's not possible for a particular component, then BoxLayout aligns that component vertically, according to the component's Y alignment. By default, a component has a Y alignment of 0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with 0.5 Y alignment.

Similarly, for a vertical layout, BoxLayout attempts to make all components in the column as wide as the widest component. If that fails, it aligns them horizontally according to their X alignments. For PAGE_AXIS layout, horizontal alignment is done based on the leading edge of the component. In other words, an X alignment value of 0.0 means the left edge of a component if the container's ComponentOrientation is left to right and it means the right edge of the component otherwise

Use another layout which scales the inner components. For example the CENTER area of a BorderLayout has such behavior.

OTHER TIPS

Personally, I would use a GridBagLayout for anything that needs to scale intelligently. GridBagLayout can seem intimidating at first, but once you get the hang of it, it's not so bad. You could also try MiGLayout, which might be easier to learn but can be as verbose as GridBagLayout in some cases.

If you don't want to learn all the nuances of GridBagLayout or MiGLayout, you can install the WindowBuilder plug-in for Eclipse and use WindowBuilder's Swing Designer wizard and WYSIWYG editor to set up your layouts.

For me, simply setting a non-0 minimum size on the JTextArea solved the issue of not being able to resize the JSplitPane when using the JTextArea with a BoxLayout.

jTextPane.setMinimumSize(new Dimension(50, 50));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top