33

How can I add the scroll bar to my text area. I have tried with this code but it's not working.

middlePanel=new JPanel();
middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Display Area"));

// create the middle panel components
display = new JTextArea(16, 58);
display.setEditable(false); // set textArea non-editable
scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

//Add Textarea in to middle panel
middlePanel.add(scroll);
middlePanel.add(display);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ravi
  • 401
  • 2
  • 8
  • 12

4 Answers4

58

After adding JTextArea into JScrollPane here:

scroll = new JScrollPane(display);

You don't need to add it again into other container like you do:

middlePanel.add(display);

Just remove that last line of code and it will work fine. Like this:

    middlePanel=new JPanel();
    middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Display Area"));

    // create the middle panel components

    display = new JTextArea(16, 58);
    display.setEditable(false); // set textArea non-editable
    scroll = new JScrollPane(display);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    //Add Textarea in to middle panel
    middlePanel.add(scroll);

JScrollPane is just another container that places scrollbars around your component when its needed and also has its own layout. All you need to do when you want to wrap anything into a scroll just pass it into JScrollPane constructor:

new JScrollPane( myComponent ) 

or set view like this:

JScrollPane pane = new JScrollPane ();
pane.getViewport ().setView ( myComponent );

Additional:

Here is fully working example since you still did not get it working:

public static void main ( String[] args )
{
    JPanel middlePanel = new JPanel ();
    middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );

    // create the middle panel components

    JTextArea display = new JTextArea ( 16, 58 );
    display.setEditable ( false ); // set textArea non-editable
    JScrollPane scroll = new JScrollPane ( display );
    scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );

    //Add Textarea in to middle panel
    middlePanel.add ( scroll );

    // My code
    JFrame frame = new JFrame ();
    frame.add ( middlePanel );
    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setVisible ( true );
}

And here is what you get: enter image description here

Mikle Garin
  • 10,083
  • 37
  • 59
  • Added the code you should use. Maybe you are trying to add it somwhere else aswell? If you do so that will get JTextArea out of the scroll pane and put on the new place. – Mikle Garin Apr 16 '12 at 15:50
  • i am not using anywhere else. – Ravi Apr 16 '12 at 15:52
  • 1
    It should work. Seems you have some additional code that affects either container or scroll pane. Check the edited answer - there is a full working example. Or maybe this is not what you want to get? – Mikle Garin Apr 16 '12 at 16:11
  • yes its working now . i was adding textarea in to panel instead of middlePanel.add(scroll) but i just replace that and got working thanks :) – Ravi Apr 16 '12 at 18:02
  • As @LihO pointed out in his answer, the `setBounds` call for the `scroll` object is what makes it work for me too. – CarlosRos Mar 01 '16 at 09:54
  • That is true only for null layout (he is setting `null` as content pane layout at the start) - but my example doesn't use it therefore it doesn't require `setBounds` call. You really need to check out the [Swing layouts guide](https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html) to understand how components are being placed within containers, that would solve a lot of possible problems for you in future :) – Mikle Garin Mar 01 '16 at 14:50
8

My naive assumption was that the size of scroll pane will be determined automatically...

The only solution that actually worked for me was explicitly seeting bounds of JScrollPane:

import javax.swing.*;

public class MyFrame extends JFrame {

    public MyFrame()
    {
        setBounds(100, 100, 491, 310);
        getContentPane().setLayout(null);

        JTextArea textField = new JTextArea();
        textField.setEditable(false);

        String str = "";
        for (int i = 0; i < 50; ++i)
            str += "Some text\n";
        textField.setText(str);

        JScrollPane scroll = new JScrollPane(textField);
        scroll.setBounds(10, 11, 455, 249);                     // <-- THIS

        getContentPane().add(scroll);
        setLocationRelativeTo ( null );
    }
}

Maybe it will help some future visitors :)

LihO
  • 41,190
  • 11
  • 99
  • 167
  • Just a small note - scroll pane size and placement would be determined automatically, but only if you provide any layout to the container you are adding it into. Right now in your example you set `null` layout for the content pane: `getContentPane().setLayout(null);` - that is why you need to specify bounds manually. Otherwise if you set any layout - for example `new BorderLayout()` - it would calculate and set bounds for your scroll pane automatically. – Mikle Garin Mar 01 '16 at 14:52
5

Try adding these two lines to your code. I hope it will work. It worked for me :)

display.setLineWrap(true);
display.setWrapStyleWord(true);

Picture of output is shown below

enter image description here

farhangdon
  • 2,003
  • 2
  • 21
  • 30
2

The Easiest way to implement scrollbar using java swing is as below :

  1. Navigate to Design view
  2. right click on textArea
  3. Select surround with JScrollPane