Forum Stats

  • 3,767,913 Users
  • 2,252,729 Discussions
  • 7,874,379 Comments

Discussions

Disabling word wrap for JTextPane

843804
843804 Member Posts: 45,751
edited Sep 19, 2009 11:06AM in Swing
I'm working on an extension of JTextPane, and I need to disable word wrap for it, is there anyway to do it? Please don't say just use JTextArea, it's not an option available to me. Any reply would be VERY appreciated.
«13

Comments

  • camickr
    camickr Member Posts: 24,931
    Please don't say just use JTextArea
    Ok. Then how about trying to search the forum first before posting questions. The following keywords, taken from your topic title, will lead to some hits "jtextpane word wrap".
  • 843804
    843804 Member Posts: 45,751
    Ok, so I've looked at all the suggested solutions for disabling wrapping for JTextPane. These are the general solutions I found:
    1. Putting TextPane inside a JPanel, then adding the JPanel inside a JScrollPane
    2. Extending the TextPane class and override the getScrollableTracksViewportWidth() and getSize() methods.

    However, both solutions doesn't handle the case where you tab several times, then type text with occational spaces until the horizontal scrollbar show up, then press return. Either the text will wrap as soon as you press return, or wrap when you keep on typing. Is there any 'perfect' solutions?
  • camickr
    camickr Member Posts: 24,931
    Is there any 'perfect' solutions?
    I created this posting 3 years ago and still haven't seen a better answer posted:

    http://forum.java.sun.com/thread.jspa?forumID=57&messageID=1322943

    I hope you find a better solution. If you do don't forget to post the code so we can all benefit.
  • 843804
    843804 Member Posts: 45,751
    I've posted the solution several times. Override EditorKit you use. Replace the layout() method of SectionView (view for root element) with something like this:
    public void layout(int width, int height) {
    super.layout(Integer.MAX_VALUE,height);
    }

    and you'll never see word wrap.

    regards,
    Stas
  • 843804
    843804 Member Posts: 45,751
    Thanks for replying, StanislavL. I'm not sure what you meant by SectionView, i can't seem to find it in the java class list. Can you give some specifc code? I appreciate your help!

    Frank
  • 843804
    843804 Member Posts: 45,751
    The view is different for different EditorKits.
    For HTMLEditorKit it's BlockView
    For StyledEditorKit it's just BoxView with Y_AXIS.

    regards,
    Stas
  • camickr
    camickr Member Posts: 24,931
    Stas,

    I think you are the only one in the forum who understands how views are painted. Any way, I tried to implement your suggeston using code examples I've found from you in the past and came up with the following:
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class NoWrapEditorKit extends StyledEditorKit
    {
    	public ViewFactory getViewFactory()
    	{
    			return new StyledViewFactory();
    	}
    
    	static class StyledViewFactory implements ViewFactory
    	{
    		public View create(Element elem)
    		{
    			String kind = elem.getName();
    
    			if (kind != null)
    			{
    				if (kind.equals(AbstractDocument.ContentElementName))
    				{
    					return new LabelView(elem);
    				}
    				else if (kind.equals(AbstractDocument.ParagraphElementName))
    				{
    					return new ParagraphView(elem);
    				}
    				else if (kind.equals(AbstractDocument.SectionElementName))
    				{
    					return new NoWrapBoxView(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);
    		}
    	}
    
    	static class NoWrapBoxView extends BoxView
    	{
    		public NoWrapBoxView(Element elem, int axis)
    		{
    			super(elem, axis);
    		}
    
    		public void layout(int width, int height)
    		{
    			super.layout(1024, height);
    		}
    	}
    
        public static void main(String[] args) throws Exception
        {
    	    JTextPane textPane = new JTextPane();
    		textPane.setEditorKit( new NoWrapEditorKit() );
    
    		JFrame frame = new TextPaneNoWrapView();
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    frame.getContentPane().add( new JScrollPane(textPane) );
    	    frame.setSize(200, 200);
    	    frame.setLocationRelativeTo(null);
    	    frame.setVisible(true);
        }
    }
    Now, enter two "tabs" and then start typing "one two three four five...". The text never wraps (good), but the horizontal scrollbar never appears either (bad). So I guess the question becomes, how to you prevent the text from wrapping and show the horizontal scrollbar when required.

    Next, I tried to retest the suggestions I gave in the above link.

    Start by clearing all the text from the text pane.
    Now, enter two "tabs" and then start typing "one two three four five...". The text won't wrap and the horizontal scrollbar appears (good).
    Now, use the enter key to create a new line, the text wraps (bad).
    Enter text on the new line, the text unwraps (good)

    Why does the text wrap/unwrap when a new line is added.

    Next I tried adding the NoWrapEditorKit to my example code:

    Start by clearing all the text from the text pane.
    Now, enter two "tabs" and then start typing "one two three four five...". The text won't wrap and the horizontal scrollbar appears (good).
    Now, use the enter key to create a new line, the horizontal scroll bar disappears (bad).
    Enter text on the new line, the horizontal scrollbar reappears (good)

    Again, why does the scrollbar disappear/reappear?
  • 843804
    843804 Member Posts: 45,751
    I tried doing what you suggested. Here's the code i wrote..

    import javax.swing.text.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.BasicTextUI;

    public class MyEditorKit extends StyledEditorKit {
    private static final ViewFactory defaultFactory = new MyViewFactory();
    public ViewFactory getViewFactory() {
    return defaultFactory;

    }

    static class MyViewFactory implements ViewFactory {
    public View create(Element elem) {
    String kind = elem.getName();
    if (kind != null) {
    if (kind.equals(AbstractDocument.ContentElementName)) {
    return new LabelView(elem);
    } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
    return new ParagraphView(elem);
    } else if (kind.equals(AbstractDocument.SectionElementName)) {
    return new MyView(elem, View.Y_AXIS);
    } else if (kind.equals(StyleConstants.ComponentElementName)) {
    return new ComponentView(elem);
    } else if (kind.equals(StyleConstants.IconElementName)) {
    return new IconView(elem);
    }
    }

    // default to text display
    return new LabelView(elem);
    }
    }

    static class MyView extends BoxView {
    public MyView(Element elem, int axis) {
    super(elem, axis);
    }

    public void layout(int width, int height) {
    super.layout(Integer.MAX_VALUE,height);
    }
    }

    public static void main(String args[]) {
    JFrame f = new JFrame();
    JTextPane t = new JTextPane();
    t.setEditorKit(new MyEditorKit());
    JScrollPane l = new JScrollPane(t);
    f.setSize(600, 400);
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(l);
    f.setVisible(true);
    }
    }

    What comes up is a JFrame with an editorpane that doesn't respond to anything i do. If i comment out the layout() method in MyView, then everything works like a regular editorpane again, so I'm sure it's the layout method that's screwing things up. Any suggestions?

    Frank
  • 843804
    843804 Member Posts: 45,751
    One way to make the horizontal srcrollbar appear is to force the minimum span of the X axis of NoWrapBoxView to certain value, i.e.
    public float getMinimumSpan(int axis) {
    	if (axis == View.Y_AXIS) {
    		return super.getMinimumSpan(axis);
    	} else {
    		return 1024f;
    	}
    }
  • 843804
    843804 Member Posts: 45,751
    The scrollpane gets size according to minimum span.
    Actually I don't know why it fails when size more than 32768... I expected that to work with Integer.MaxValue.

    but I think that's enough for most cases.

    regards,
    Stas
        static class NoWrapBoxView extends BoxView {
            public NoWrapBoxView(Element elem, int axis) {
                super(elem, axis);
            }
    
            public void layout(int width, int height) {
                super.layout(32768, height);
            }
            public float getMinimumSpan(int axis) {
                return super.getPreferredSpan(axis);
            }
        }
This discussion has been closed.