Skip to content
Advertisement

How can I add a scroll bar to a text area?

Please, anyone, tell me how to add the scrollbar to a JTextArea. I tried out many things. but still not able to get it. I copied some codes related to the text area.

JavaScript

Advertisement

Answer

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Netbeans section.

As Andrew said, you have to place the JTextArea inside of a JScrollPane, then place the JScrollPane inside of a JPanel with a Swing layout. I used a BorderLayout.

Here’s the GUI after I typed some lines.

enter image description here

Here’s the complete runnable code.

JavaScript
Advertisement