-1

Can anyone tell me why a scroll bar will not display in this piece of code ? Instead the text area just keeps expanding as i enter more and more text.

JFrame displayframe = new JFrame("All Details");
displayframe.setLayout(new FlowLayout());

JTextArea text = new JTextArea(allDetails);
text.setSize(350,200);
text.setEditable(false);

JScrollPane scroll = new JScrollPane(text);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

displayframe.getContentPane().add(text,scroll);
displayframe.setSize(400, 600);
displayframe.setVisible(true);

Thanks, Jessica

null
  • 2,146
  • 3
  • 21
  • 36
Jessicam93
  • 31
  • 6
  • Providing a [SSCCE](http://sscce.org) would be help you get faster and better help sooner. – Jonathan Drapeau Nov 28 '13 at 21:03
  • Basically, you never add the scroll pane to the content. Because your adding text, it is removed the scroll pane automatically.try using displayframe.getContentPane().add(text); instead... – MadProgrammer Nov 28 '13 at 21:39

3 Answers3

5

You're using the JScrollPane scroll as a constraint for the add method. Just add the component directly to the frame

displayframe.add(scroll);
Reimeus
  • 152,723
  • 12
  • 195
  • 261
2

By default, the line wrap property is set to false. You should set it to true. You should a

// JTextArea(allDetails, 20, 60); sets rows and columns. instead of setting 
// a size for the text area.
JTextArea text = new JTextArea(allDetails, 20, 60);
text.setEditable(false);

text.setLineWrap(true);
text.setWrapStyleWord(true);  // wrap style if by word instead of chararcter

... 

displayFrame.pack();

See Javadoc for JTextArea

You have already put the textArea in the ScrollPane. You only need to add the ScrollPane to the frame.

displayframe.add(scroll); 

See this running example

 import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TestTextArea {
    private JScrollPane jsp;
    private JFrame frame = new JFrame();
    public TestTextArea() {
        String hello = "hello hello hello hello hello hello hello "  
                     + "hello hello hello hello hello hello hello " 
                     + "hello hello hello hello hello hello hello "
                     + "hello hello hello hello hello hello hello "
                     + "hello hello hello hello hello hello hello "; 

        JTextArea text = new JTextArea(hello, 10, 50);
        text.setLineWrap(true);
        text.setWrapStyleWord(true);
        jsp = new JScrollPane(text);

        frame.add(jsp);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestTextArea();

            }
        });
    }  
}
Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
1

First, don't set the text in the constructor of JTextArea, instead, tell it how many rows and columns you want to show. That will restrict it size, which makes the setSize line uneeded. Then you can either use setText or append to add your text. This sample code will do, I think, what you're looking for. Using Reimeus answer to add the JScrollPane to the JFrame.

JFrame displayframe = new JFrame("All Details");
displayframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
displayframe.setLayout(new FlowLayout());

JTextArea text = new JTextArea(30,30);
text.setEditable(false);
text.append("22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs\n22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs\n22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs\n22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline123");

JScrollPane scroll = new JScrollPane(text);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

displayframe.getContentPane().add(scroll);
displayframe.setSize(400, 600);
displayframe.setVisible(true);
Jonathan Drapeau
  • 2,578
  • 2
  • 24
  • 32
  • *"tell it how many rows and columns you want to show. That will restrict it size"* - Not really. It will set the preferred size of the component (and may effect the scrollable preferred size but I haven't verified this). Based on the OPs code, it will still expand to fit the text. You are right in that it makes the need for setSize redundant (and should be avoid ;)) – MadProgrammer Nov 28 '13 at 21:37
  • @jonathan Thank you, this fixed the problem. It didnt work for me because I didnt set the col and row size ? Thanks again :) – Jessicam93 Nov 28 '13 at 21:40