Results 1 to 3 of 3
  1. #1
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default JEditorPane and line wrapping

    My JEditorPane doesn't wrap lines.
    I try WordUtils.wrap(String text,int wrapLength) method. WordUtils (Commons Lang 2.5 API)

    My problem is that I can't find wrapLength.
    I try scrollPane.getViewport().getSize().width method to find wrapLength without results.
    How can I find the wrapLength?
    Is there another way for line wrapping?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: JEditorPane and line wrapping

    Since your question appears to be related to Apache Commons, I've moved the thread here from New to Java.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Of course, only those members who have the Apache Commons package will be in a position to help.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: JEditorPane and line wrapping

    Quote Originally Posted by DarrylBurke View Post
    Since your question appears to be related to Apache Commons, I've moved the thread here from New to Java.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Of course, only those members who have the Apache Commons package will be in a position to help.

    db
    @DarrylBurke
    I am novice and I suppose that "New To Java" category is the best place to post.
    Also I want to listen another solution except Appache Commons WordUtils.
    This is not a complaint , I always respect the moderators, please do not get me wrong.

    Anyway, I hoped that WordUtils.wrap(text) method will solve my problem but I was wrong.
    This is my code and I put my own value to widthLength.
    I press button and WordUtils.wrap(String text, int widthLength) is called but the text isn't wrapped correctly.

    Sorry for my bad english.
    Java Code:
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import org.apache.commons.lang3.text.WordUtils;
    
    public class LineWrap extends JFrame{
    
        String text="A JScrollPane manages a viewport, optional vertical and horizontal"+
                " scroll bars, and optional row and column heading"+
                " viewports. You can find task-oriented documentation of JScrollPane"+
                "in How to Use Scroll Panes, a section in The Java Tutorial";
                
        JEditorPane editorPane=new JEditorPane();
        
        JScrollPane scrollPane=new JScrollPane(editorPane);
        JButton button=new JButton("Wrap");
    
        public LineWrap() {
            
            button.addActionListener(new MyActionListener());
            
            Container contentPane = getContentPane();
            
            contentPane.setLayout(new FlowLayout());
            
            scrollPane.setPreferredSize(new Dimension(300, 100));
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
           
            editorPane.setText(text); 
    
            contentPane.add(scrollPane);
            contentPane.add(button);
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new LineWrap();
        }
        
        class MyActionListener implements ActionListener{
    
            public void actionPerformed(ActionEvent ae) {
                
                int wrappedLength=50; // How can I find wrappedLength value????
                
                String wrappedText=WordUtils.wrap(text, wrappedLength);
                editorPane.setText(wrappedText);
            }        
        }
    }

Similar Threads

  1. Line wrapping on a JTextPane
    By danoc93 in forum AWT / Swing
    Replies: 7
    Last Post: 05-20-2012, 08:22 AM
  2. Turning on/off Word Wrapping in jEditorPane
    By Pluto in forum AWT / Swing
    Replies: 1
    Last Post: 03-24-2012, 10:33 AM
  3. how to place cursor at end of each line in JEditorPane
    By prasad.vara in forum AWT / Swing
    Replies: 2
    Last Post: 10-25-2010, 01:32 PM
  4. 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
  •