2

the jscrollpane that I am adding doesnt appearin my textarea

textArea = new JTextArea();
 scroll = new JScrollPane(textArea);
          scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

         this.add(textArea);
         this.add(scroll);

          this.setSize(1000, 600);
       this.setLayout(new BorderLayout());


        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Christian
  • 259
  • 3
  • 7
  • 21

2 Answers2

4
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
//this.add(textArea); // get rid of this
this.add(scroll);

You create the scrollpane with the text area, but then the next statement removes the text area from the scrollpane because a component can only have a single parent.

Get rid of that statement and just add the scrollpane to the frame.

Then scrollbars will appear automatically as you add data to the text area.

Also you should create the text area using something like:

textArea = new JTextArea(5, 20);

to give a suggestion on how big to make the text area.

I did what you said but still nothing happens

Another problem is that you need to set the layout manager BEFORE you start adding components to the frame (or panel).

camickr
  • 321,443
  • 19
  • 166
  • 288
4

Remove this.add(textArea); and add scroll.setSize( 100, 100 ); will also work for you.

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
  • @Christian, You should NOT be setting the size of the scroll pane.The layout manager will set the size based on the rules of the layout manager. – camickr Nov 26 '15 at 05:49