Question

I have a panel which is divided by two parts with BoxLayout.X_AXIS:

public TabsPanel() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        add(createLeftPanel());
        add(createRightPanel());
}

Each left and right panels have the following structure: an outer panel with BorderLayout, and an inner panel in BorderLayout.CENTER of the outer panel, which in its turn has BoxLayout.Y_AXIS and several components from top to bottom. The right panel has JTextArea with JScrollPane as one of its components:

protected JPanel createRightPanel() {
        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JTextArea label = createLabel();

        JScrollPane scroll = new JScrollPane(label);
        scroll.setMaximumSize(new Dimension(500, 200));
        panel.add(Box.createRigidArea(new Dimension(0,106)));
        panel.add(scroll);

        JPanel panel_buttons = new JPanel();
        panel_buttons.setLayout(new BoxLayout(panel_buttons, BoxLayout.LINE_AXIS));
        panel_buttons.setAlignmentX(Component.CENTER_ALIGNMENT);

        Font font_text = new Font("Georgia", Font.PLAIN, 20);

        JButton[] buttons = new JButton[2];
        buttons[0] = new JButton("Clear");
        buttons[1] = new JButton("Exit");
        for (int i = 0; i < buttons.length; i++) {
            buttons[i].setMaximumSize(new Dimension(120, 40));
            buttons[i].setFont(font_text);
            panel_buttons.add(buttons[i]);
            if (i == 0)
                panel_buttons.add(Box.createRigidArea(new Dimension(40, 0)));
            buttons[i].addActionListener(new TextActionListener(label)); 
        }

        panel.add(Box.createRigidArea(new Dimension(0,20)));
        panel.add(panel_buttons);

        pane.add(panel, BorderLayout.CENTER);
        return pane;
    }

When text goes beyond the borders, scroll bars appear and I can move them and read the text. Looks like everything is ok, but when I either click any place outside the scroll pane or even just move the pointer, the scroll pane moves to the left and grows down. It doesn't change its width, but it shifts to the left because the area between it and the right panel's borders increases. Accordingly, size of the left panel shrinks. When I clear the text area and again either click or move the pointer, it is back to its normal size.

What is the reason its height grows and its left and right margins increase? What am I doing wrong?

UPDATE. I've found the problem. The thing is that I didn't create JTextArea correctly. I initialized it without parameters:

JTextArea text = new JTextArea("Some initial text");

Now I have rewritten:

JTextArea text = new JTextArea(5,10);

It is now shifted to the left by about 5 mm and do not changes its height. Still not perfect, but looks like I am on the right track.

Thank you everybody for your help!

Was it helpful?

Solution 2

2 steps to correct:

  1. Set the size of the JTextArea: JTextArea text = new JTextArea(row, col);
  2. Still shifts to the left by the size of the vertical bar:

either add ChangeListener to adjust the size of the JScrollPane

scroll.getViewport().addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if (scroll.getVerticalScrollBar().isVisible())
                      scroll.setPreferredSize(480, 200);
                }
            }
});

or add scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top