1 2 Previous Next 27 Replies Latest reply on Sep 19, 2009 3:06 PM by darrylburke

    Disabling word wrap for JTextPane

    843804
      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.
        • 1. Re: Disabling word wrap for JTextPane
          camickr
          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".
          • 2. Re: Disabling word wrap for JTextPane
            843804
            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?
            • 3. Re: Disabling word wrap for JTextPane
              camickr
              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.
              • 4. Re: Disabling word wrap for JTextPane
                843804
                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
                • 5. Re: Disabling word wrap for JTextPane
                  843804
                  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
                  • 6. Re: Disabling word wrap for JTextPane
                    843804
                    The view is different for different EditorKits.
                    For HTMLEditorKit it's BlockView
                    For StyledEditorKit it's just BoxView with Y_AXIS.

                    regards,
                    Stas
                    • 7. Re: Disabling word wrap for JTextPane
                      camickr
                      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?
                      • 8. Re: Disabling word wrap for JTextPane
                        843804
                        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
                        • 9. Re: Disabling word wrap for JTextPane
                          843804
                          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;
                               }
                          }
                          • 10. Re: Disabling word wrap for JTextPane
                            843804
                            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);
                                    }
                                }
                            • 11. Re: Disabling word wrap for JTextPane
                              camickr
                              I tried adding the getMinimumSpan code to the class I posted above. It still doesn't work 100 percent.

                              Type in the following data

                              a) two tab characters
                              b) "one two three four..." until the scroll bar appears
                              c) then use the enter key to add a new line (the scrollbar disappears)
                              d) start typing on the new line and the scrollbar will reappear

                              The view has problems when tab characters are found in a preceeding line.

                              This is the same basic problem with the suggestion I provided 3 years ago.
                              • 12. Re: Disabling word wrap for JTextPane
                                843804
                                After a grueling 3 days of looking at swing code, I've finally found the solution!!!
                                check the following code out. No need to set the bounds to any specific size, will work for any line length, well, reasonablly long. No problems with tabs or newlines either. The only thing I can't think of solving is the caret not being visible when editing the right most part of the text area. Any suggestions?



                                //NoWrapTextPane.java

                                import java.awt.Dimension;

                                import javax.swing.JTextPane;
                                import javax.swing.text.EditorKit;

                                public class NoWrapTextPane extends JTextPane {
                                public boolean getScrollableTracksViewportWidth() {
                                //should not allow text to be wrapped
                                return false;
                                }

                                public void setSize(Dimension d) {
                                //dont let the Textpane get sized smaller than its parent
                                Dimension pSize = getParent().getSize();
                                if (d.width < pSize.width) {
                                super.setSize(pSize.width, d.height);
                                } else {
                                super.setSize(d);
                                }
                                }

                                protected EditorKit createDefaultEditorKit() {
                                return new MyEditorKit();
                                }

                                }


                                //MyEditorKit.java

                                import javax.swing.text.*;

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


                                public static class MyViewFactory implements ViewFactory {
                                public View create(Element elem) {
                                String kind = elem.getName();
                                if (kind != null) {
                                if (kind.equals(AbstractDocument.ContentElementName)) {
                                return new MyView(elem);
                                } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
                                return new ParagraphView(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);
                                }
                                }

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


                                public static class MyView extends LabelView {
                                public MyView(Element e) {
                                super(e);
                                }

                                public float getTabbedSpan(float x, TabExpander e) {
                                float result = super.getTabbedSpan(x, e);
                                this.preferenceChanged(this, true, false);
                                return result;
                                }

                                }
                                }
                                • 13. Re: Disabling word wrap for JTextPane
                                  843804
                                  To camickr.

                                  I investigated this a little more. Somehow the TabExpander is null but in all editor kits ParagraphView implements TabExpander. So I added a check if the expander==null use parent ParagraphView as expander.

                                  That's a simple implementation of LabelView. I used 0 instead of real X of label (last parameter). We can also override gettabExpander() of LabelView to return parent paragraph if the expander isn't set.

                                  regards,
                                  Stas
                                      static class MyLabelView extends LabelView {
                                          public MyLabelView(Element elem) {
                                              super(elem);
                                          }
                                          public float getPreferredSpan(int axis) {
                                              float span=0;
                                              if (axis==View.X_AXIS) {
                                                  int p0 = getStartOffset();
                                                  int p1 = getEndOffset();
                                                  checkPainter();
                                                  TabExpander ex=getTabExpander();
                                                  if (ex==null) {
                                                      //paragraph implements TabExpander
                                                      ex=(TabExpander)this.getParent().getParent();
                                                  }
                                                  span = getGlyphPainter().getSpan(this, p0, p1, ex, 0);
                                                  return Math.max(span, 1);
                                              }
                                              else {
                                                  span=super.getPreferredSpan(axis);
                                              }
                                              return span;
                                          }
                                      }
                                  • 14. Re: Disabling word wrap for JTextPane
                                    843804
                                    To battbot.
                                    That's really bad to call preferenceChanged in the getting tabbed span.
                                    That means every time when you measure your label you relayout all content. I think the component will be really slow.

                                    regards,
                                    Stas
                                    1 2 Previous Next