31

I am trying to add a scroll bar to a JTextArea. Would someone please tell me what I did wrong with the code below?

JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");

JScrollPane scrollV = new JScrollPane (textArea);
JScrollPane scrollH = new JScrollPane (textArea);

scrollV.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollH.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setVisible (true);

Thank you in advance.

EDIT: I fixed the code with the Adel Boutros' advice below.

    //FRAME
JFrame frame = new JFrame ("Test");
frame.setSize(500,500);
frame.setResizable(false);
//

//TEXT AREA
JTextArea textArea = new JTextArea("TEST");
textArea.setSize(400,400);    

    textArea.setLineWrap(true);
    textArea.setEditable(false);
    textArea.setVisible(true);

    JScrollPane scroll = new JScrollPane (textArea);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
          scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    frame.add(scroll);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1
  • @AdelBoutros no, the OPs code is correct now - please update your answer to the OPs edit :-)
    – kleopatra
    Commented Jan 13, 2012 at 11:05

4 Answers 4

50

It doesn't work because you didn't attach the ScrollPane to the JFrame.

Also, you don't need 2 JScrollPanes:

JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");

JScrollPane scroll = new JScrollPane (textArea, 
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

frame.add(scroll);
frame.setVisible (true);
4
  • 1
    frame.add(scroll, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);? Where is this method?
    – JB Nizet
    Commented Jan 13, 2012 at 10:40
  • @kleopatra what do you mean by it wasn't the point? Can you please elaborate. Commented Jan 13, 2012 at 10:58
  • the method you are invoking is add(Component comp, Object constraint, int index) - guess that's not your intention ;-)
    – kleopatra
    Commented Jan 13, 2012 at 11:04
  • @kleopatra by the way, you can delete your comment on the question Commented Jan 13, 2012 at 11:07
13
  1. Open design view
  2. Right click to textArea
  3. open surround with option
  4. select "...JScrollPane".
1
  • 1
    Your are a king
    – adba
    Commented Feb 9 at 17:22
5

A scroll pane is a container which contains another component. You can't add your text area to two different scroll panes. The scroll pane takes care of the horizontal and vertical scroll bars.

And if you never add the scroll pane to the frame, it will never be visible.

Read the swing tutorial about scroll panes.

5

You don't need two JScrollPanes.

Example:

JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);  

// Add the scroll pane into the content pane
JFrame f = new JFrame();
f.getContentPane().add(sp);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.