0

The scroll bar doesn't show. I've tried most of the codes people replied with in previous questions like this one.

    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    textArea_2 = new JTextArea();
    textArea_2.setRows(200);
    textArea_2.setBounds(0, 22, 434, 120);
    textArea_2.setEditable(false);
    JScrollPane scrollv2 = new JScrollPane (textArea_2);
    frame.add(scrollv2);
    frame.getContentPane().add(textArea_2);
    scrollv2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    frame.setVisible (true);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hadya
  • 35
  • 3
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jun 04 '14 at 02:35

2 Answers2

3

You are adding your JTextArea to your content pane where you should be actually adding your JScrollPane to the content pane of your JFrame. You should add the JTextArea to the content pane of the JScrollPane. Below is an example of the visible scroll bar in action:

Vertical scroll bar on a JScrollPane

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class SimpleScrollBars extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SimpleScrollBars frame = new SimpleScrollBars();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public SimpleScrollBars() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        contentPane.add(scrollPane, BorderLayout.CENTER);

        JTextArea textArea = new JTextArea(5, 15);
        scrollPane.setViewportView(textArea);

        pack();
    }

}
David Yee
  • 3,515
  • 25
  • 45
  • 1
    +1 for adding the scroll pane to the content pane. A Swing component can only have a single parent, so you add the text area to the viewport of the scroll pane and then add the scroll pane to the frame. – camickr Jun 03 '14 at 20:38
  • 1
    @AndrewThompson Wow thanks for clarifying! Glad to learn something new. :) – David Yee Jun 04 '14 at 07:26
2

The scrollbars will appear automatically when the preferred size of the text area is greater than the size of the scroll pane.

Your text area doesn't have any text to display, therefore its preferred size is (0, 0).

Also:

  1. Don't use textArea_2.setBounds(...). This does nothing. The scrollpane has its own layout manager and will determine the appropriate size for the text area.

  2. Don't use textArea_2.setRows(200). The 200 represents rows of text, not pixels. Your monitor can't display 200 rows of text. So use a reasonable value, like 10 or 20 depending on your application requirements.

camickr
  • 321,443
  • 19
  • 166
  • 288