JDK-6431335 : REGRESSION: No caret is shown on empty Textpanes with Synth Look and Feel
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-05-29
  • Updated: 2011-02-16
  • Resolved: 2006-06-08
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
Version 1.5.0_05 or higher

EXTRA RELEVANT SYSTEM CONFIGURATION :
Appears only when a Synth Look and Feel is set.

A DESCRIPTION OF THE PROBLEM :
By using a Synth Look and Feel and an _empty_ JTextPane no caret will be displayed. This occurs only with JVM 1.5.0_05 or higher including Mustang. With JVM 1.5.0_04 the problem doesn't occur.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.plaf.synth.SynthContext;
import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.plaf.synth.SynthPainter;

public class TextPaneTest extends JFrame
{
  private static String synthXml = "<synth>" +
  "    <style id=\"all\">" +
  "      <object id=\"customPainter\" class=\"TextPaneTest$TextPanePainter\" />" +
  "      <font name=\"Verdana\" size=\"12\"/>" +
  "    </style>" +
  "    <bind style=\"all\" type=\"REGION\" key=\".*\"/>" +
  "    <style id=\"textPane\">" +
  "      <state>" +
  "        <color value=\"#FFFFFF\" type=\"BACKGROUND\"/>" +
  "        <painter method=\"textPaneBackground\" idref=\"customPainter\" />" +
  "      </state>" +
  "    </style>" +
  "    <bind style=\"textPane\" type=\"region\" key=\"TextPane\"/>" + 
  "</synth>";
 
  public static void main(String[] args) throws Exception
  {
    InputStream is = new ByteArrayInputStream(synthXml.getBytes("UTF8"));
    SynthLookAndFeel laf = new SynthLookAndFeel();
    laf.load(is, TextPaneTest.class);
    UIManager.setLookAndFeel(laf);   
    new TextPaneTest();   
  }

  public TextPaneTest()
  {
    JTextPane textPane = new JTextPane();
    textPane.setText("Delete this text - caret disappers!");
    add(textPane);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(400, 300));
    setLocationRelativeTo(null);
    setVisible(true); 
  }

  public static class TextPanePainter extends SynthPainter
  {
    public void paintTextPaneBackground(SynthContext sc, Graphics g, int 
x, int y, int w, int h)
    {
      //Also changes the caret color for empty textPanes with Java 1.5.0_05 or later!
      g.setColor(Color.YELLOW);
      g.fillRect(x, y, w, h);
    }
  }
   
} 

---------- END SOURCE ----------

Release Regression From : 5.0u4
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

Comments
EVALUATION As Igor says, this was relying on a particular quirk of the implementation. If you want a color for the caret, you need to specify it.
08-06-2006

EVALUATION CaretColor is not defined for the text component under Synth L&F with the xml file from the description. The value of textPane.getCaretColor is null. javax.swing.text.DefaultCaret uses color from textComponent.getCaretColor() to paint the caret. Because the color is null it uses the current color set for the graphics. <note> Interesting enough I do not see this behavior documented anywhere. Graphics.setColor implementation returns immediately if the color is null I have created 6436374 [Graphics.setColor(null) is not documented] for that. </note> Before the fix for 4855860 the current color happened to be black. (My guess is we painted GlyphView which we do not paint now because it is empty. That GlyphView set color to the color it needed and did not clean it. Thus the color happened to be black) Now the color is the same as background thus the caret is not visible. This bug either belongs to synth or a "user error". Reassigning to ###@###.###
08-06-2006

EVALUATION I verified this is a regression introduced between 1.5.0_04 and 1.5.0_5. Igor suggested this may be caused by 4855860. I verified the fix for 4855860 did in fact cause the problem. The interesting question is why this only happens with Synth and not the other look and feels. I thought it might be because of the insets, but that doesn't appear to be the case (I tried setting metal to have no insets/border and metal still worked). I'm reassigning to Igor for further investigation.
08-06-2006