Using JSplitPane with JTextArea

In my application, I got a frame containing JSplitPane

. The parameter JSplitPane

is split horizontally. On the left side, there is a panel containing some components that don't matter. On the right, I got JPanel

( BoxLayout

) subclass JTextArea

inside a JScrollPane

, a JTextPane

inside a JScrollPane

and a JButton

.

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

I've tried different settings, also setting preferredSize and MaximumSize, but none of these work. The components remain in their preferred format. If I try to make them smaller, the scrollPane works (that's fine), but when I try to make them larger, they stay at their preferred size.

+3


source to share


3 answers


Most likely your problem is with the BoxLayout

one used in your right pane. Copy-paste from javadoc class:

BoxLayout tries to arrange components according to their preferred width (for horizontal layout) or height (for vertical layout). For horizontal layout, if not all components are the same height, BoxLayout tries to make all components as tall as the tallest component. If this is not possible for a specific component, then BoxLayout aligns that component vertically, according to the component's Y alignment. By default, the 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 ones. centers of other components with 0.5 Y alignment.

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



Use a different layout that scales the internal components. For example, area CENTER

a BorderLayout

has this behavior.

+2


source


Personally, I have used GridBagLayout for everything I need for smart scaling. The GridBagLayout may seem intimidating at first, but once you get it, it's not too bad. You can also try MiGLayout , which may be easier to find out, but can be as verbose in some cases as GridBagLayout.



If you don't want to learn all the nuances of GridBagLayout or MiGLayout, you can install the WindowBuilder plugin for Eclipse and use the WindowBuilder Swing Designer wizard and WYSIWYG editor to customize your layouts.

0


source


For me, just 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 BoxLayout.

jTextPane.setMinimumSize(new Dimension(50, 50));

      

0


source







All Articles