JTextPane is not wrapping text
Asked Answered
S

2

3

I am running into one weird problem. I have a JtextPane inside a JscrollPane which is showing large string in term of distribution list and it is properly wrapping code when I am running the program using eclipse but when I am running the same program in using java webstart it stopped wrapping the text.

Here is my code:

   private JScrollPane displayResults(String distributionList) {
// TODO Auto-generated method stub
  JTextPane textArea = new JTextPane();
  textArea.setText(distributionList);
  textArea.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(textArea);  
  scrollPane.setPreferredSize( new Dimension( 500, 500 ) );
  return scrollPane;
}
Scutum answered 8/1, 2013 at 15:6 Comment(0)
J
5

The reason could be java version.

See https://forums.oracle.com/forums/thread.jspa?messageID=10690405 where it is discussed and a workaround is provided

Jujitsu answered 8/1, 2013 at 15:14 Comment(4)
ok I tried the solution mentioned but run into one exception. Exception in thread "Thread-40" java.lang.ClassCastException: javax.swing.text.DefaultStyledDocument incompatible with javax.swing.text.html.HTMLDocumentScutum
this isn't / couldn't be valid for all of Java7 versions, @StanislavL, can you please to answering this question too, and pleeeease with your code workaroundChrystalchryste
@Chrystalchryste I will try to investigate but not sure when I will have enough timeJujitsu
@Jujitsu my bad I miss_reading JTextPane instead of JTextAreaChrystalchryste
S
2

well for me this work

      textArea.setEditorKit(new HTMLEditorKit(){ 
     @Override 
     public ViewFactory getViewFactory(){ 

         return new HTMLFactory(){ 
             public View create(Element e){ 
                View v = super.create(e); 
                if(v instanceof InlineView){ 
                    return new InlineView(e){ 
                        public int getBreakWeight(int axis, float pos, float len) { 
                            return GoodBreakWeight; 
                        } 
                        public View breakView(int axis, int p0, float pos, float len) { 
                            if(axis == View.X_AXIS) { 
                                checkPainter(); 
                                int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len); 
                                if(p0 == getStartOffset() && p1 == getEndOffset()) { 
                                    return this; 
                                } 
                                return createFragment(p0, p1); 
                            } 
                            return this; 
                          } 
                      }; 
                } 
                else if (v instanceof ParagraphView) { 
                    return new ParagraphView(e) { 
                        protected javax.swing.SizeRequirements calculateMinorAxisRequirements(int axis, javax.swing.SizeRequirements r) { 
                            if (r == null) { 
                                  r = new javax.swing.SizeRequirements(); 
                            } 
                            float pref = layoutPool.getPreferredSpan(axis); 
                            float min = layoutPool.getMinimumSpan(axis); 
                            // Don't include insets, Box.getXXXSpan will include them. 
                              r.minimum = (int)min; 
                              r.preferred = Math.max(r.minimum, (int) pref); 
                              r.maximum = Integer.MAX_VALUE; 
                              r.alignment = 0.5f; 
                            return r; 
                          } 

                      }; 
                  } 
                return v; 
              } 
          }; 
      } 
  }); 
Scutum answered 8/1, 2013 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.