JDK-6340106 : REGRESSION: JTextPane - Selection for empty lines is not visible
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-10-21
  • Updated: 2014-10-25
  • Resolved: 2011-03-07
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.
JDK 7
7 b15Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

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

A DESCRIPTION OF THE PROBLEM :
Users cannot see what lines were selected if the lines consist of line separators('\n')  only.
The behaviour was changed by fixing another bug (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4855860). Width for a child view '\n' is set to 0 therefore the selection is not visible.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run. You don't see anything selected on 1.5.0_05. Users could be
confused from this behaviour. Line separators should have non zero width
while selecting text(for empty lines).


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Selection painter should handle this.
ACTUAL -
Selection for empty these lines is not visible.

REPRODUCIBILITY :
This bug can be reproduced always.

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

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

/**
 * Bug Preview for empty selection - selection of empty lines
 */
public class BugPreview {
    final static String endOfLineProperty =
System.getProperty("line.separator", "\n");

    private static void addEmptyLine(JTextComponent c) throws
BadLocationException {
        final Document document = c.getDocument();
        document.insertString(document.getLength(), endOfLineProperty,
null);
    }

    public static void main(String[] args) {
        JTextComponent editor = new JTextPane();
        try {
            addEmptyLine(editor);//a few empty lines
            addEmptyLine(editor);
            addEmptyLine(editor);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
        editor.selectAll(); // now we select all text, but empty lines are not marked as selected on 1.5.0_05
        JFrame frame = new JFrame();
        frame.setTitle("Demo");
        frame.setSize(200, 150);
        frame.getContentPane().add(editor);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

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

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

Comments
SUGGESTED FIX Webrevs: http://javaweb.sfbay/jcg/1.7.0-dolphin/swing/6340106/
21-05-2007

EVALUATION Please note that this behaviour depends on the zero-width View being painted AT ALL, see 6427770. Thanks Igor.
06-03-2007

EVALUATION The javax.swing.text.DefaultHighlighter.DefaultHighlightPainter.paintLayer() should paint a visible region even if the view to be highlighted is happened to be of zero width. This covers the views containing "\n" as the sole content (the primary case for this CR), the combining-accents-only views, and so on. Views should care to (preferably) not call into the Highlighter if nothing is supposed to be highlighted for the given model range.
27-02-2007

EVALUATION While 6340106 [REGRESSION: JTextPane - Selection for empty lines is not visible] technically is a regression it is not a serious/critical one. It is caused by a fix (and the fix does everything right) which is more important than this bug. Unfortunately due to the swing-text design it is tricky to get that bug and 6340106 fixed at the same time.
08-08-2006

EVALUATION The behavior was indeed changed after the fix for 4855860 I think we can make rows in paragraph to have non zero width always. This way '\n' continue to have 0 width and 4855860 remains fixed.
28-10-2005

EVALUATION This seems valid. My text editor shows a non-zero width for '\n'. Perhaps we want zero width only for the LAST '\n' in a document.
21-10-2005