I'd like to achieve the following:
+------------------Other container(s)-----------------+
| +------JScrollPane (vertical)-------+ |
| | JTextField | |
| | Box.createRigidArea (vertical) | |
| | JTextArea | |
| | { etc.. any other J-component } | |
| | | |
| | | |
| | | |
| | | |
| +-----------------------------------+ |
+-----------------------------------------------------+
The closest I'm able to get is with the following (pseudo) code:
JPanel container = new JPanel(new BorderLayout());
JPanel innerContainer = new JPanel();
innerContainer.setLayout(new BoxLayout(_innerContainer, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(innerContainer);
container.add(scrollPane, BorderLayout.NORTH);
Whenever I'd like to add components they're added to the inner container:
innerContainer.add(new JTextField());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
innerContainer.add(new JTextArea());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
innerContainer.add(new JLabel());
.... etc
I'm facing the following problems:
I let the layout managers take care of all UI element size. So preferable I do not use any .setxxSize()-method. A JTextArea, occupied with alot of text, will use all the required space (vertical and horizontal). Which is totally fine. Whenever the frame is resized the JTextArea will grow, which is still totally fine. But whenever I shrink the frame, the JTextArea does not adjust accordingly. Therefore a horizontal scrollbar appears, but this I'd like to avoid. I want users only to scroll vertically.
Does anyone have some good tips? Thanks!
EDIT:
SSCCE:
import java.awt.*;
import javax.swing.*;
public class VerticalStackPanel extends JFrame {
public static void main(String[] args) {
JFrame frame = new VerticalStackPanel();
frame.setSize(800, 600);
frame.setVisible(true);
}
public VerticalStackPanel() {
super();
JScrollPane scrollPane = new JScrollPane(createVerticalStackPanel());
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
getContentPane().add(scrollPane);
}
private Component createVerticalStackPanel() {
JPanel container = new JPanel(new BorderLayout());
container.add(createInnerContainer(), BorderLayout.NORTH);
return container;
}
private Component createInnerContainer() {
JPanel innerContainer = new JPanel();
innerContainer.setLayout(new BoxLayout(innerContainer, BoxLayout.Y_AXIS));
innerContainer.add(createTextArea());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
innerContainer.add(createTextArea());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
innerContainer.add(createTextArea());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
innerContainer.add(createTextArea());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
innerContainer.add(createTextArea());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
innerContainer.add(createTextArea());
innerContainer.add(Box.createRigidArea(new Dimension(0, 10)));
return innerContainer;
}
private Component createTextArea() {
JTextArea textArea = new JTextArea();
textArea.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. In vitae diam nunc. Proin vulputate, odio consectetur tincidunt aliquet, metus ipsum rhoncus tellus, in dignissim ligula tortor at risus. Aenean rhoncus lorem a magna luctus molestie. Fusce consequat enim vel quam pharetra varius. Vivamus porta condimentum orci non ultrices. Duis magna arcu, fringilla ut eleifend a, volutpat ac elit. Sed ligula enim, dictum id fringilla vitae, ornare vel nulla. Proin lobortis suscipit lectus eget placerat. Morbi aliquam dolor quis lectus tincidunt eu volutpat risus fermentum. Nunc et sapien a nisl aliquet auctor. Phasellus nec sem tellus, et scelerisque sapien. In a nibh vestibulum velit convallis sodales. Vestibulum tempor fringilla vulputate. Duis hendrerit dolor id urna aliquam cursus auctor enim pulvinar. Nulla rutrum fringilla eros, id congue nisi mattis in. Pellentesque consectetur eleifend mauris, ut aliquam purus convallis ac. Vestibulum pretium, sem at congue faucibus, leo leo volutpat odio, ut feugiat nulla felis a diam. Praesent dignissim eros ac eros semper auctor. Phasellus eu sapien nibh. Quisque pulvinar tristique lectus, quis porttitor purus suscipit sed. Curabitur gravida, ipsum ut vehicula aliquet, erat ipsum tincidunt nulla, eget bibendum felis dolor vel risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Phasellus vitae nisl odio, blandit laoreet nibh.");
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
return textArea;
}
}
Whenever you'll resize the frame horizontally, text will dissapear whenever the frame becomes less big. A horizontal scrollbar appears, but since:
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Text just dissapears.. the JTextArea should adjust accordingly