United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6340106 : REGRESSION: JTextPane - Selection for empty lines is not visible

Details
Type:
Bug
Submit Date:
2005-10-21
Status:
Closed
Updated Date:
2014-10-25
Project Name:
JDK
Resolved Date:
2011-03-07
Component:
client-libs
OS:
windows_xp
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:

Related Reports
Relates:
Relates:

Sub Tasks

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/
                                     
2007-05-21
EVALUATION

Please note that this behaviour depends on the zero-width View being painted AT ALL, see 6427770.  Thanks Igor.
                                     
2007-03-06
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.
                                     
2007-02-27
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.
                                     
2006-08-08
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.
                                     
2005-10-28
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.
                                     
2005-10-21



Hardware and Software, Engineered to Work Together