JDK-4855860 : JTextPane returns too large preferredSize in some specific cases
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.1,5.0u4
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_9,windows_2000
  • CPU: x86,sparc
  • Submitted: 2003-04-29
  • Updated: 2004-10-13
  • Resolved: 2004-09-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u5Fixed 6 betaFixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
Name: jk109818			Date: 04/29/2003


FULL PRODUCT VERSION :
java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)


FULL OS VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
Most of the cases, the preferred size returned by JTextPane is correct. However it is incorrect for some specific strings that need to be displayed (occurs 20% of the time).  In these cases the height that is returned correspond to the height needed to actually display the document PLUS an extra (blank) line.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the program.

EXPECTED VERSUS ACTUAL BEHAVIOR :
As you can see during the execution of the program, the string "Test Cediti po poiii" could be displayed on a single line.  However the preferred size that is computed has height 34 instead of 18.

Run the program.  You get the following output:

___Resizing to: 104,123
___Resizing to: 104,123

setting field to value>> Test
preferred size: java.awt.Dimension[width=27,height=18]
___Resizing to: 104,123

setting field to value>> Test Cediti po poiii
preferred size: java.awt.Dimension[width=105,height=34]
___Resizing to: 104,123

setting field to value>> Test Cediti po poiiix
preferred size: java.awt.Dimension[width=110,height=34]
___Resizing to: 104,123

setting field to value>>
preferred size: java.awt.Dimension[width=3,height=18]


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DenisTest {
  public static void main(String args[]) {
    new DenisTest().bugWithTextPaneDemoPanel();
  }

  private Timer timer;
  static JTextPane textPane = new JTextPane() {
    public void setSize(Dimension d) {
      System.out.println("___Resizing to: " + d);
      super.setSize(d);
    }

    public void setBounds(int x, int y, int width, int height) {
      System.out.println("___Resizing to: " + width + "," + height);
      super.setBounds(x, y, width, height);
    }
  };
  static private transient StyledDocument document;
  static private transient Style style;

  private void bugWithTextPaneDemoPanel() {
    JFrame mainFrame = new JFrame("Test");
    mainFrame.setSize(50, 150);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container mainPanel = mainFrame.getContentPane();
    mainPanel.add(textPane);

    StyleContext context = new StyleContext();
    document = new DefaultStyledDocument(context);
    style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_CENTER);
    textPane.setDocument(document);
    textPane.setBackground(Color.ORANGE);
    textPane.setOpaque(true);
    textPane.setBorder(new LineBorder(Color.DARK_GRAY));

    //Create a timer.
    timer = new Timer(ONE_SECOND, new ActionListener() {
      String[] msgs = new String[]{"Test", "Test Cediti po poiii", "Test Cediti po poiiix", ""};
      int count = 0;

      public void actionPerformed(ActionEvent evt) {
        String msg=msgs[count++];
        testString(msg);
        if (count >= msgs.length)
          System.exit(0);
      }
    });

    mainFrame.setVisible(true);

    timer.start();
  }

  private void testString(String msg) {
    System.out.println("\nsetting field to value>> "+msg);
    try {
      document.remove(0, document.getLength());
      document.insertString(0, msg, style);
    } catch (BadLocationException e) {
      e.printStackTrace();
    }

    System.out.println("preferred size: " + textPane.getPreferredSize());
  }

  public final static int ONE_SECOND = 1000;
}

---------- END SOURCE ----------
(Review ID: 185011) 
======================================================================
###@###.### 10/13/04 17:55 GMT

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang FIXED IN: mustang INTEGRATED IN: mustang
14-09-2004

EVALUATION we have a number of size related bugs for text components. 4446522 4794439 4813831 4829215 we will work on these for the next release Name: anR10225 Date: 03/11/2004 Here is another reason of the bug. The line wraps because the width of the '\n' view which always exists as the last view is equal to 1. Zero width should be returned for this view. ======================================================================
14-09-2004

SUGGESTED FIX Name: anR10225 Date: 03/19/2004 *** /net/crown/export1/naa/dragon/webrev/4855860/src/share/classes/javax/swing/text/GlyphView.java- Thu Mar 11 15:34:38 2004 --- GlyphView.java Thu Mar 11 15:33:01 2004 *************** *** 525,532 **** if (skipWidth) { return 0; } ! float width = painter.getSpan(this, p0, p1, expander, this.x); ! return Math.max(width, 1); case View.Y_AXIS: float h = painter.getHeight(this); if (isSuperscript()) { --- 525,531 ---- if (skipWidth) { return 0; } ! return painter.getSpan(this, p0, p1, expander, this.x); case View.Y_AXIS: float h = painter.getHeight(this); if (isSuperscript()) { ======================================================================
14-09-2004