JDK-4480453 : Layout problems with JTextPane and small fonts
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2001-07-17
  • Updated: 2002-02-20
  • Resolved: 2002-02-20
Related Reports
Duplicate :  
Description

Name: bsC130419			Date: 07/16/2001


java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)


When displaying text with a small font (e.g. 6 pt), JTextPane leaves extra
space between the last line and the rest of the text.  The example application
below illustrates the problem.  As an interesting note, the problem exists with
and without the "i18n" property being set to true.  I would have expected
different results because the property changes the GlyphPainter that is used.

public class Test {
    public static void main( String args[]) {
	try {
	    javax.swing.JFrame frame = new javax.swing.JFrame();
	    frame.getContentPane().setLayout( new java.awt.BorderLayout());
	    frame.setSize( 400, 300);
	    frame.addWindowListener( new java.awt.event.WindowAdapter() {
		    public void windowClosing( java.awt.event.WindowEvent
event) {
			System.exit(0);
		    }
		});

	    javax.swing.JTextPane textPane = new javax.swing.JTextPane();
	    javax.swing.text.MutableAttributeSet attributeSet = new
javax.swing.text.SimpleAttributeSet();
	    javax.swing.text.StyleConstants.setFontFamily(
attributeSet, "serif");
	    javax.swing.text.StyleConstants.setFontSize( attributeSet, 6);
	    textPane.getDocument().insertString( 0, "The Java language provides
special support for the string concatentation operator ( + ), and for
conversion of other objects to strings. String concatenation is implemented
through the StringBuffer class and its append method. String conversions are
implemented through the method toString, defined by Object and inherited by all
classes in Java. For additional information on string concatenation and
conversion, see Gosling, Joy, and Steele, The Java Language Specification.The
Java language provides special support for the string concatentation operator (
+ ), and for conversion of other objects to strings. String concatenation is
implemented through the StringBuffer class and its append method. String
conversions are implemented through the method toString, defined by Object and
inherited by all classes in Java. For additional information on string
concatenation and conversion, see Gosling, Joy, and Steele, The Java Language
Specification.The Java language provides special support for the string
concatentation operator ( + ), and for conversion of other objects to strings.
String concatenation is implemented through the StringBuffer class and its
append method. String conversions are implemented through the method toString,
defined by Object and inherited by all classes in Java. For additional
information on string concatenation and conversion, see Gosling, Joy, and
Steele, The Java Language Specification.", attributeSet);
	    frame.getContentPane().add( textPane, java.awt.BorderLayout.CENTER);

	    frame.setVisible( true);
	}
	catch( Exception ex) {
	    ex.printStackTrace();
	}
    }
}
(Review ID: 128133) 
======================================================================

Comments
WORK AROUND Name: bsC130419 Date: 07/16/2001 Date: Mon, 16 Jul 2001 20:59:46 -0700 From: "Mike Bresnahan" <###@###.###> I discovered the source of this bug and a workaround. The problem is that the newline at the end of the document is assigned the default font which is 12pt. Thus when the point size of the inserted text is less than 12pt, the last line is set down too low. To work around the problem I have done the following: javax.swing.text.Style style = ((javax.swing.text.DefaultStyledDocument)textPane.getDocument()).getStyle("d efault"); javax.swing.text.StyleConstants.setFontFamily( style, "serif"); javax.swing.text.StyleConstants.setFontSize( style, 6); textPane.replaceSelection("The Java language provides special support for the string concatentation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.The Java language provides special support for the string concatentation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.The Java language provides special support for the string concatentation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification."); Fixing the problem may involve ignoring the newling while creating the View hierarchy. For example, during my research I verified that modifying javax.swing.text.FlowView.layout and javax.swing.text.FlowView.layoutRow to ignore the last position in the document fixed my problem. Doing the same in loadChildren may also be required. I am unsure of side effects, however. Mike Bresnahan
11-06-2004

EVALUATION The information provided in the work-around is very valuable. The problem is indeed the result of the last newline character having a larger font. This bug was reported much earlier in 4280944. I am closing this as a duplicate and updating that bug. ###@###.### 2002-02-19
19-02-2002