Search code examples
javajtextareascrollable

How to make JTextArea scrollable to user input?


I have been working on a java application since a few days (I am a novice). I have to make use of JTextArea for user input(250 characters). I have used setPreferredSize() method to resize my JTextArea. However, as the user types, when the inserted words exceed the JTextArea height, I cannot see the rest of the input though the user types.

Is there a way to solve this problem?


Solution

  • Please post some code so we can see what you did already. What you can try is add a JScrollPane:

    JFrame frame = new JFrame ("Frame");
    JTextArea textArea = new JTextArea ("Textarea");
    
    JScrollPane scroll = new JScrollPane (textArea, 
       JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    
    frame.add(scroll);//add Scrollbar to frame