JDK-5102369 : JEditorPane sets its caret position into section in HTML doc after initia
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: x86
  • Submitted: 2004-09-15
  • Updated: 2017-05-19
  • Resolved: 2004-11-23
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 6
6Resolved
Related Reports
Duplicate :  
Description
Name: ap153225			Date: 15/09/2004


FULL PRODUCT VERSION :
java version "1.5.0"

ADDITIONAL OS VERSION INFORMATION :
Generic

A DESCRIPTION OF THE PROBLEM :
Default caret's position in JEditorPane is 0
this is unacceptable for HTML documents, cause there is invisible <head> element in zero position
and in this case caret goes invisible too.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the provided code, 
and observe caret hiding

EXPECTED VERSUS ACTUAL BEHAVIOR :
The caret must be visible when JEditorPane is focused
ACTUAL -
After initialization and after setDocument() invocation for HTML documents
caret goes invisible

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JEditorPane_bugdemo {
    static JEditorPane editorPane;

    public static void main(String[] args) {
        editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        editorPane.setEditable(true);

        editorPane.setText("<html><body>Hello from editor pane!</body></html>");

        JFrame frame = new JFrame("The mystical 0th position - part 2");
        
        frame.getContentPane().add(editorPane);
        frame.getContentPane().add(getButtonPanel(), BorderLayout.SOUTH);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(300, 300));
        frame.setVisible(true);
        editorPane.requestFocus();
    }
    
    private static JPanel getButtonPanel() {
        JPanel panel = new JPanel();
        JButton button = new JButton("Set text");
        panel.add(button, BorderLayout.SOUTH);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editorPane.setText("Another text");
                editorPane.requestFocus();
            }
        });
        JButton button2 = new JButton("Set doc");
        panel.add(button2);
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editorPane.setDocument(getTestPane().getDocument());
                editorPane.requestFocus();
            }
        });
        JButton button3 = new JButton("Alter doc");
        panel.add(button3);
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Document doc = editorPane.getDocument();
                try {
                    doc.remove(0, doc.getLength());
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
                editorPane.requestFocus();
            }
        });
        return panel;
    }

    private static JEditorPane getTestPane() {
        JEditorPane testPane = new JEditorPane();
        testPane.setContentType("text/html");
        testPane.setText("<html><body>Hello from test pane </body></html>");
        return testPane;
    }
}
---------- END SOURCE ----------

======================================================================
###@###.### 2004-09-15
###@###.### 2004-09-15

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
25-09-2004

EVALUATION It is happends cause after setDocument() invocation caret's position resets to zero. Should be set inside the <body> element instead ###@###.### 2004-09-15 Investigation shows that fixing this bug will lead to backward incompatibility The main problem is that on one hand having caret in zero posintion is incorrect for HTML documents and we should move it to the first visible position but on the other hand developer could set caret to zero position intentionally, so it is undesirable to move it the second problem is with setText(String text) method when we set text for the first time we should set the caret to the first visible position but this behaviour may be unexpected to some existed application closed as will not fix ###@###.### 11/1/04 16:36 GMT reopen because of ###@###.### request ###@###.### 11/2/04 16:36 GMT
01-11-0004