Forum Stats

  • 3,758,404 Users
  • 2,251,383 Discussions
  • 7,870,182 Comments

Discussions

Disabling word wrap for JTextPane

13»

Comments

  • 843805
    843805 Member Posts: 49,999
    Using the above, I created a simple (not very efficient) version for HTML no wrap.

    Adding it here just to make it available.
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    
    public class NoWrapEditorKit extends HTMLEditorKit
    {
        public ViewFactory getViewFactory()
        {
            return new NoWrapViewFactory();
        }
    
        static class NoWrapViewFactory extends HTMLEditorKit.HTMLFactory
        {
            public View create(Element elem)
            {
                View v = super.create(elem);
                if (v instanceof InlineView )
                {
                    v = new NoWrapBoxView(elem);                
                }
                return( v );
            }
        }
    
        static class NoWrapBoxView extends InlineView
        {
            public NoWrapBoxView(Element elem)
            {
                super(elem);
            }
    
            public int getBreakWeight(int axis, float pos, float len)
            {
                return( BadBreakWeight );
            }
        }
    }
  • 843805
    843805 Member Posts: 49,999
    Woops! Cleanup didn't work use this version
  • 843805
    843805 Member Posts: 49,999
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    
    public class NoWrapEditorKit extends HTMLEditorKit
    {
        public ViewFactory getViewFactory()
        {
            return new NoWrapViewFactory();
        }
    
        static class NoWrapViewFactory extends HTMLEditorKit.HTMLFactory
        {
            public View create(Element elem)
            {
                Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);
    
                if (o instanceof HTML.Tag)
                {
                    HTML.Tag kind = (HTML.Tag)o;
                    if (kind == HTML.Tag.CONTENT)
                    {
                        return new NoWrapBoxView(elem);
                    }
                }
    
                return super.create(elem);
            }
        }
    
        static class NoWrapBoxView extends InlineView
        {
            public NoWrapBoxView(Element elem)
            {
                super(elem);
            }
    
            public int getBreakWeight(int axis, float pos, float len)
            {
                return BadBreakWeight;
            }
    
        }
    
    }
  • 843805
    843805 Member Posts: 49,999
    Just for information here are the changes I made to camickr's original code..I just made add flag to enable the word wrapping
    public class NoWrapEditorKit extends StyledEditorKit implements ViewFactory {
    
        private boolean shouldWrap = false;
    
        public void setWrap(boolean wrap) {
            this.shouldWrap = wrap;
        }
    
        private class NoWrapParagraphView extends ParagraphView {
            public NoWrapParagraphView(Element elem) {
                super(elem);
            }
    
            protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
    
                if (shouldWrap)
                    return super.calculateMinorAxisRequirements(axis, r);
    
                SizeRequirements req = super.calculateMinorAxisRequirements(axis, r);
                req.minimum = req.preferred;
                return req;
            }
    
            public int getFlowSpan(int index) {
    
                if (shouldWrap)
                    return super.getFlowSpan(index);
    
                return Integer.MAX_VALUE;
            }
        }
    
        public ViewFactory getViewFactory() {
            return this;
        }
        
        public View create(Element elem) {
            String kind = elem.getName();
            System.out.println("Creating view: " + kind);
            if (kind != null)
                if (kind.equals(AbstractDocument.ContentElementName)) {
                    return new LabelView(elem);
                } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
                    return new NoWrapParagraphView(elem);
                } else if (kind.equals(AbstractDocument.SectionElementName)) {
                    return new BoxView(elem, View.Y_AXIS);
                } else if (kind.equals(StyleConstants.ComponentElementName)) {
                    return new ComponentView(elem);
                } else if (kind.equals(StyleConstants.IconElementName)) {
                    return new IconView(elem);
                }
            return new LabelView(elem);
        }
    }
    This is how i use it in the application..word wrapping turned on properly displays the text pressed against the left side of the JTextPane as expected with the lines wrapping correctly. It's only when I turn off word wrapping that the text jumps backs and locks into the center of the JTextPane. The scrollbar lets me scroll back to the left side..I just can't move any of the pasted text there.
    /**
         * Turn on/off word wrapping
         * 
         * @param wrap
         */
        public void setWordWrap(boolean wrap) {
    
            // If we are an instance of our custom editor kit
            if (editor.getEditorKit() instanceof NoWrapEditorKit) {
    
                // Get the document
                Document doc = editor.getDocument();
    
                // Get the no wrap editor kit
                NoWrapEditorKit kit = (NoWrapEditorKit) editor.getEditorKit();
    
                // Turn on/off wrapping
                kit.setWrap(wrap);
    
                // Set the editor kit back into the compoment
                editor.setEditorKit(kit);
    
                // Set the contents back into the doc to repopulate the contents
                editor.setDocument(doc);
    
            }
        }
    Any ideas? It's a strange intermittedent issue..
    - Tim
  • 843806
    843806 Member Posts: 49,998
    Oh my god!!!
    I was reading this thread because I also wanted just now how to stop my JTextPane from wrapping, and was horrified to see just how complicated this could be.
    This just can't be!!! (I thought to myself).
    And while I didn't find an obvious and clean solution, like the function call that should be
    void setTextWrap(boolean wrap)
    I did find information on another thread, on another website.
    I found it useful, and since this website is more likely to be read by other users like myself, looking for a solution to this silly problem, i thought i'd post the solution here.

    First, I extend JTextPane, and override one function call.
        class MyTextPane extends JTextPane
        {
        	public MyTextPane()
        	{
        		super();
        		
        	}
        	
        	public boolean getScrollableTracksViewportWidth()
        	{
        		return false;
        	}
        	
        }
    And then, as I am placing this TextPane in a JScrollPane, to prevent the colouring problem that one has (don't implement this line to see what I mean) I simply set the colour of the background of the ScrollPane:
    MyTextPane area1 = new MyTextPane ();
    JScrollPane spane1 = new JScrollPane(area1);
    spane1.getViewport().setBackground(Color.white );
    Is there even a better solution?
    I know this is an old thread, but I hope it helps someone else.
  • 843807
    843807 Member Posts: 46,582
    svaens,
    i almost went with one of the proposed solutions until i clicked next to view responses on page 2.
    your posted solution is the best!!!
    thank you!!!
  • darrylburke
    darrylburke Member Posts: 18,007
    anonimuds, please don't post in threads that are long dead. When you have a question, start your own topic. Feel free to provide a link to an old post that may be relevant to your problem.

    I'm locking this thread now.

    db
This discussion has been closed.