Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By shall
  • 1 Post By shall

Thread: Line wrapping on a JTextPane

  1. #1
    danoc93 is offline Member
    Join Date
    Apr 2012
    Posts
    37
    Rep Power
    0

    Default Line wrapping on a JTextPane

    Hello, I am using a JTextPane with a StyledDocument in order to format the strings I add to the pane...

    The problem is that I have a problem when the string sent to the pane is too long, the scrolling bar appears and the user has to go all the way to the right in order to get to read all the line...

    I got to disable the horizontal scrolling, but the text still goes in one line... I'd like somehelp with the wrapping, I need the words to go to another line if they don't fit in the pane...

    How could I achieve it??

    Lets say I have

    I am a java student and i need to get the words here in another line.

    So I need it to be
    I am a java student and i need to
    get the words here in another line.

    Apparently it is easy with a JTextArea but not with this pane...

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Line wrapping on a JTextPane

    Check this out: java - Toggling text wrap in a JTextpane - Stack Overflow. Check out the code at the bottom of that page.
    danoc93 likes this.

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Line wrapping on a JTextPane

    It seems like it should go in this order:

    Java Code:
    textPane = new JTextPane();
    noWrapPanel = new JPanel( new BorderLayout() );
    noWrapPanel.add( textPane );
    scrollPane = new JScrollPane( noWrapPanel );
    scrollPane.setViewportView(textPane); // creates a wrapped scroll pane using the text pane as a viewport.
    danoc93 likes this.

  4. #4
    danoc93 is offline Member
    Join Date
    Apr 2012
    Posts
    37
    Rep Power
    0

    Default Re: Line wrapping on a JTextPane

    scrollPane.setViewportView(textPane)

    This was the line I needed, thank you, <3.

  5. #5
    danoc93 is offline Member
    Join Date
    Apr 2012
    Posts
    37
    Rep Power
    0

    Default Re: Line wrapping on a JTextPane

    I just tried to use that last line, doesn't work :(... and does anybody else know how to get the textpane to autoscroll as soon as a line goes out of the main view?

  6. #6
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Line wrapping on a JTextPane

    Quote Originally Posted by danoc93 View Post
    I just tried to use that last line, doesn't work :(... and does anybody else know how to get the textpane to autoscroll as soon as a line goes out of the main view?
    Do you mean it doesn't create a wrapped scroll pane?

  7. #7
    danoc93 is offline Member
    Join Date
    Apr 2012
    Posts
    37
    Rep Power
    0

    Default Re: Line wrapping on a JTextPane

    It does not... Text is still on the same line going alllll the way to the right :s...

  8. #8
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Line wrapping on a JTextPane

    I got it to work with the following program (using Java version 1.7.0_04):

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    
    public class MyScrollPaneFrame extends JFrame {
    
        private static JPanel noWrapPanel;
        private static JScrollPane scrollPane;
        private static JTextPane textPane;
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            MyScrollPaneFrame scrollPaneFrame = new MyScrollPaneFrame();
            textPane = new JTextPane();
            noWrapPanel = new JPanel(new BorderLayout());
            noWrapPanel.setPreferredSize(new Dimension(200,200));
            noWrapPanel.add(textPane);
            scrollPane = new JScrollPane(noWrapPanel);
            scrollPane.setPreferredSize(new Dimension(200,200));
            scrollPane.setViewportView(textPane); // creates a wrapped scroll pane using the text pane as a viewport.
            
            scrollPaneFrame.add(scrollPane);
            scrollPaneFrame.setPreferredSize(new Dimension(200,200));
            scrollPaneFrame.pack();
            scrollPaneFrame.setVisible(true);
        }
    }

Similar Threads

  1. AES+RSA Wrapping
    By hanseichel in forum Advanced Java
    Replies: 0
    Last Post: 12-11-2011, 01:42 AM
  2. Tableviewer Wrapping
    By Daniel38 in forum SWT / JFace
    Replies: 0
    Last Post: 02-03-2011, 10:59 AM
  3. Line Wrapping
    By Lil_Aziz1 in forum Eclipse
    Replies: 9
    Last Post: 06-25-2010, 03:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •