scrollbars in JTextArea
java, swing
Solution
As Fredrik mentions in his answer, the simple way to achieve this is to place the `JTextArea` in a `JScrollPane`. This will allow scrolling of the view area of the `JTextArea`.
Just for the sake of completeness, the following is how it could be achieved:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta); // JTextArea is placed in a JScrollPane.
Once the `JTextArea` is included in the `JScrollPane`, the `JScrollPane` should be added to where the text area should be. In the following example, the text area with the scroll bars is added to a `JFrame`:
JFrame f = new JFrame();
f.getContentPane().add(sp);
Thank you kd304 for mentioning in the comments that one should add the `JScrollPane` to the container rather than the `JTextArea` -- I feel it's a common error to add the text area itself to the destination container rather than the scroll pane with text area.
The following articles from The Java Tutorials has more details:
- How to Use Scroll Panes
- How to Use Text Areas
Problem
How do I add scrollbars to a JTextArea?