0

This might be an odd question, so I beg you guys to bear with me on this.

I want to present information to a JFrame. Particularly, a String.

Here's the catch, it's a very very long String. Like, miles. Think several dozen lines of information in total.

It's a little like a phone-book, with more names than you can possibly fit on a page, and you only have that one page to use.

So I'm thinking scrollbars.

I tried:

    JTextArea area = new JTextArea(veryLongText.toString());
    JScrollPane scrollPane = new JScrollPane(area);

But that didn't work, as I couldn't scroll down until I added more text. Which wasn't what I was going for.

To put it simply, I want a text box that I can scroll in if the window is too small to fit all the text inside it.

How would I go about doing this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ViRALiC
  • 1,419
  • 4
  • 18
  • 46

5 Answers5

5

Try setting it up to allow for line wrapping...

JTextArea area = new JTextArea(veryLongText.toString());
area.setLineWrap(true);

Sets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped. A PropertyChange event ("lineWrap") is fired when the policy is changed. By default this property is false.

Can even wrap on word boundaries by using...

area.setWrapStyleWord(true);

Sets the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundaries. By default this property is false.

Generally, I suggest both

Take a look at the JTextArea JavaDocs and How to use text areas for more details...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
4

It's a little like a phone-book, with more names than you can possibly fit on a page

I'd recommend a JList or JTable in a JScrollPane.

See 'how to use X' in your favorite search engine, where 'x' is any of the above three, for more details.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I am disappointed you deleted your comment as it was a relevant link, especially for anyone who decides to use the oracle tutorials. – MirroredFate Feb 19 '14 at 00:29
  • @AndrewThompson Sorry, the comment on my answer. I commented here before you commented on my answer again. – MirroredFate Feb 19 '14 at 00:33
3

You are on the right track with JScrollPane. I highly suggested you look at the JScrollPane tutorial

In a nutshell, you will want to use the following piece of code:

//In a container that uses a BorderLayout:
textArea = new JTextArea(5, 30);
...
JScrollPane scrollPane = new JScrollPane(textArea);
...
add(scrollPane, BorderLayout.CENTER);

This means that everything in your textArea will be scrollable. You can replace that with a JPanel if it is more appropriate.

MirroredFate
  • 12,396
  • 14
  • 68
  • 100
  • @AndrewThompson That was literally a copy-paste from the Oracle docs. But I will remove it because I agree. – MirroredFate Feb 19 '14 at 00:25
  • The code snippet above corrects a problem in the official tutorial it refers to. +1 See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Feb 19 '14 at 00:50
3

It sounds like you didn't have enough text to make it scroll. Your solution seems correct.

Maybe you want the scrollbar to be always there, use the other constructor: JScrollPane(Component, int, int) with constants from ScrollPaneConstants.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • *"Maybe you want the scrollbar to be always there.."* E.G. as seen in [this answer](http://stackoverflow.com/a/21849351/418556) which declares horizontal bar always, vertical bar never. – Andrew Thompson Feb 19 '14 at 00:27
1

I have a feeling your string is one very very long line. Does it have any new line characters in it? ie "\n"?

Anyhoo, here's an example that works

//
//  Test ScrollPane with very very long string
//
package testjscrollpane;
import javax.swing.*;

public class TestJScrollPane extends JFrame
{
    public TestJScrollPane()
    {
        //Create a long long string
        String s = "";
        int n = 0;
        for (int i=0; i<10000; i++)
        {
            s += Integer.toString(i)+"  ";

            //this next part breaks the string up into lots of lines
            //if commentted out, the whole string will appear on 1 line
            //with no vertical scrollbar
//            n++;
//            if(n==100)
//            {
//                s += "\n";
//                n = 0;
//            }
        }

        this.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
        this.setSize(400, 400);

        JTextArea area = new JTextArea(s);
        JScrollPane scrollpane = new JScrollPane(area);
        this.add(scrollpane);

        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        TestJScrollPane myFrame = new TestJScrollPane();
    }

}

Good luck!

Roy
  • 974
  • 6
  • 11
  • 2
    Support for line wrapping and word wrapping is provide by `JTextArea` already...which takes into consideration things like font metrics automatically... – MadProgrammer Feb 19 '14 at 00:36
  • I'll take your word for it, but when I experimented no wordwrap happened. I'm guessing it doesn't by default, and the 2 lines the OP gave us had no mention of turning them on. I'm still betting his scroll pane shows 1 really long line because it doesn't have any new line characters in it. – Roy Feb 19 '14 at 05:23
  • Line wrapping shouldn't (from my pathetic memory) association with white space... – MadProgrammer Feb 19 '14 at 05:52