Hi,
I have a JTextPane (or JTextComponent) which works fine. When I call
setEditable(false), the caret is not visible. How can I make the caret
visible on
not editable JTextPane?
*Why* do you (think you) need to give the end user
a caret in a non-editable text area?
Since the caret indicates the place that has the *input*
focus, and there is no input for a non editable component,
it does not make that much sense. (And would be confusing).
Note that in this example, the text *selection* shows clearly,
that is probably all the user needs.
</sscce>
import java.awt.*;
import javax.swing.*;
public class CaretInText extends JPanel {
CaretInText() {
setLayout( new BorderLayout() );
JTextPane tp = new JTextPane();
tp.replaceSelection("Hi There!");
tp.setCaretPosition(0);
tp.moveCaretPosition(3);
tp.setEditable(false);
add( tp, BorderLayout.CENTER );
}
public static void main(String[] args) {
JFrame f = new JFrame("Caret In Text");
f.getContentPane().add( new CaretInText() );
f.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
f.pack();
f.setVisible(true);
}
}
</sscce>
HTH