1 2 Previous Next 27 Replies Latest reply on Sep 19, 2009 3:06 PM by darrylburke Go to original post
      • 15. Re: Disabling word wrap for JTextPane
        843804
        I think your solution is better Stas. Thanks alot!! Now if you can also help me figure out how to make the cursor show up when it's all the way to the right, then we will have a workable JTextPane on our hands.

        Frank
        • 16. Re: Disabling word wrap for JTextPane
          843804
          Oh btw Stas, in which cases are the tab expansion based on the x position? Will setting the x position to 0 rather than it's true position cause any unwanted side-effects?

          Frank
          • 17. Re: Disabling word wrap for JTextPane
            camickr
            Now if you can also help me figure out how to make the cursor show up when it's all the way to the right,
            This is also the default behaviour when you use a JTextArea that does not wrap, so it may not be as easy to fix.

            But I'm thinking you would play with the span of every line and add a few pixels to the calculated amount to allow for the caret to be painted.
            • 18. Re: Disabling word wrap for JTextPane
              843804
              It's better init tab expander and use super method.

              Like this.

              As for cursor If you scroll slightly to the right the caret becomes visible. At first glance we can just scroll one-two pixels to the right...

              Need more investigations... ot you can try to add a few pixels to right inset of SectionView.

              regards,
              Stas
                      public float getPreferredSpan(int axis) {
                          if (axis==View.X_AXIS) {
                              TabExpander ex=getTabExpander();
                              if (ex==null) {
                                  //paragraph implements TabExpander
                                  ex=(TabExpander)this.getParent().getParent();
                                  getTabbedSpan(0, ex);
                              }
                          }
                          return super.getPreferredSpan(axis);
                      }
              • 19. Re: Disabling word wrap for JTextPane
                843804
                Thanks for all the responses Stas and Camickr. You get the duke dollars Stas, thnx for the help.

                Frank
                • 20. Re: Disabling word wrap for JTextPane
                  843804
                  Hi.

                  Is there a way to overwrite the layout method in BlockView class without overwriting the create method in ViewFactory class?

                  Thnx.
                  • 21. Re: Disabling word wrap for JTextPane
                    843805
                    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 );
                            }
                        }
                    }
                    • 22. Re: Disabling word wrap for JTextPane
                      843805
                      Woops! Cleanup didn't work use this version
                      • 23. Re: Disabling word wrap for JTextPane
                        843805
                        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;
                                }
                        
                            }
                        
                        }
                        • 24. Re: Disabling word wrap for JTextPane
                          843805
                          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
                          • 25. Re: Disabling word wrap for JTextPane
                            843806
                            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.
                            • 26. Re: Disabling word wrap for JTextPane
                              843807
                              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!!!
                              • 27. Re: Disabling word wrap for JTextPane
                                darrylburke
                                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
                                1 2 Previous Next