1

I'm trying to get JTextPane to word-wrap. I've searched this site and across the Internet and it seems that JTextPane is supposed to word-wrap by default- most trouble people have is with disabling the wrap or getting the wrap to work inside a JScrollPane. I've tried various combinations of TextPanes, ScrollPanes and JPanels, to no avail. Below is the simplest possible code tested that still has the problem (no wrap).

public class Looseleaf extends JFrame{

    public Looseleaf(){
        this.setSize(200,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        JTextPane txtPane = new JTextPane();
        this.add(txtPane);
        this.setVisible(true);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Maythe
  • 576
  • 4
  • 13
  • What exactly do you mean by "word-wrap"? Your example code seems to work for me: if I add a `main` method that simply creates a new `Looseleaf`, I get a small window with a text pane. When I type words into the text pane, the line is automatically broken when I get to the right border. – Thomas Jan 31 '13 at 22:28
  • It wraps with me, Java 7, Linux. – Joop Eggen Jan 31 '13 at 22:28
  • Thomas, I mean I'd like the JTextPane to move a word to the next line if some of its letters exceed the width of the window. Of course, at this point I'd be happy just to have a character-wrap. On my computer, the characters are extending off the screen without ever going to a new line. I'm running Java 7 on Windows 7. – Maythe Jan 31 '13 at 22:52

3 Answers3

2

Depending on you layout, JTextPane may or may not wrapped, based on what it perceves as it's available size.

Instead, add the JTextPane to a JScrollPane instead...

public class Looseleaf extends JFrame{
    public Looseleaf(){
        this.setSize(200,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        JTextPane txtPane = new JTextPane();
        this.add(new JScrollPane(txtPane)); // <-- Add the text pane to a scroll pane....
        this.setVisible(true);
    }
}

Updated with additional example

Try this instead. This worked for me.

public class TestTextPaneWrap {

    public static void main(String[] args) {
        new TestTextPaneWrap();
    }

    public TestTextPaneWrap() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JTextPane editor = new JTextPane();
            editor.setMinimumSize(new Dimension(0, 0));
            add(new JScrollPane(editor));
        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I tried your code, but it still does not wrap to a new line. The horizontal scroll bar just appears when the text goes outside the window. Thanks though! – Maythe Jan 31 '13 at 22:55
  • Unfortunately, that did not work either. It had the same effect of only scrolling horizontally forever. I'm about to boot up Linux to see if it works there. – Maythe Feb 01 '13 at 02:10
  • Text wrapping is dependent on the underlying editor. You could take a look at [this](http://java-sl.com/wrap.html) and [this](http://java-sl.com/tip_html_letter_wrap.html) for examples – MadProgrammer Feb 01 '13 at 02:13
  • Thanks! It was an editor problem, and I've got it working now. – Maythe Feb 01 '13 at 02:28
0

At least in my case, the following coding works. If you add the JTextPane into a JPane with border layout and add that pane into the JScrollPane, then the lines would not wrap.

0

I've used JTextArea instead of JTextPane

then used method:

textArea.setLineWrap(true);
Adir Dayan
  • 1,308
  • 13
  • 21