JDK-5003294 : REGRESSION: unselected text is painted using the selected text color
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-02-25
  • Updated: 2004-06-14
  • Resolved: 2004-06-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
5.0 b56Fixed
Related Reports
Relates :  
Relates :  
Description

Name: rmT116609			Date: 02/25/2004


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


ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
Under the Microsoft Windows Look and Feel, by default unselected text is painted black and selected text is painted white.

In tiger-beta JTextPanes, this behavior has changed from selected vs. unselected text to highlighted vs. unhighlighted text.

If you create a JTextPane and attach a highlight to a portion of its text, that text will be painted in the selected text color even if it is not selected.  Since the selected text color and the text pane background both default to white, this results in the highlighted text seemingly disappearing.

I have traced the problem to a change in GlyphView.paint.  The paint method was deliberately modified to look for highlights instead of selection, but I am not sure why.  If this was an intentional change in behavior (I hope not), it should be applied to all text components and not just JTextPane, for consistency.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the attached test case on a Microsoft Windows machine.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Both the JTextArea and the JTextPane should display selected text in white and unselected text in black (assuming default color choices).
ACTUAL -
Under tiger-beta, the JTextPane displays the underlined text in white instead of black.  The JTextArea behaves properly.

Under previous versions of Java, both components display correctly.



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.text.*;

public class HighlightTest {
    private static class TestHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
        public TestHighlightPainter() {
            super(Color.RED);
        }
        
        
        public Shape paintLayer(Graphics gc, int offs0, int offs1,
                                Shape bounds, JTextComponent c, View view) {
            try {
                Graphics2D g = (Graphics2D) gc;
                Shape shape = view.modelToView(offs0, Position.Bias.Forward,
                                               offs1, Position.Bias.Backward,
                                               bounds);
                Rectangle r = (shape instanceof Rectangle) ?
                                  (Rectangle) shape : shape.getBounds();
                g.setColor(Color.RED);
                g.fillRect(r.x, r.y + r.height - 3, r.width, 1);
                return r;
            }
            catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    
    
    public static void main(String[] arg) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        JTextPane textPane = new JTextPane();
        textPane.setText("Example Text Pane");
        textPane.getHighlighter().addHighlight(1, 4, new TestHighlightPainter());
        textPane.setBackground(new Color(192, 192, 255));

        JTextArea textArea = new JTextArea();
        textArea.setText("Example Text Area");
        textArea.getHighlighter().addHighlight(1, 4, new TestHighlightPainter());
        textArea.setBackground(new Color(192, 192, 255));

        JFrame f = new JFrame();
        f.getContentPane().setLayout(new GridLayout(2, 1));
        f.getContentPane().add(new JScrollPane(textArea));
        f.getContentPane().add(new JScrollPane(textPane));
        f.setSize(200, 200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.show();
    }
}
---------- END SOURCE ----------

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

(Incident Review ID: 240223) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-rc FIXED IN: tiger-rc INTEGRATED IN: tiger-b56 tiger-rc
08-07-2004

EVALUATION Name: sh120115 Date: 02/25/2004 This looks like a regression introduced by 4532590. I don't beleive having a highlighter on text should cause it to be painted in the selected text color. ###@###.### 2004-02-25 ====================================================================== Name: anR10225 Date: 03/04/2004 This was introduced by the fix for 4761990. Highlighted text was made to be painted with the selectedTextColor, because it was impossible to use the default highlighter functionality under windows with the default color scheme. The solution could be to add an interface (e.g. called ColoredHighlightPainter) which extends HighlightPainter and exports color for painting text foreground. For example : public interface ColoredHighlightPainter extends HighlightPainter{ /** * If null then the default selectedForegroundColor is used. */ Color getForegroundColor() ; } ====================================================================== Name: anR10225 Date: 04/26/2004 We could fix this in Tiger without any API changes. We should paint the highlighted text with SelectedTextColor if and only if the HighlightPainter is a swing package painter (i.e. inner class of javax.swing.text.DefaultHighlighter or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color is null or equals to the selection color of the text component. This solution allows us to fix this bug and not to rollback the fix for 4761990. ======================================================================
08-07-2004