caret on not editable JTextPane

N

ndrli

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?

Any information would be appreciated. Thanks in advance.
 
A

Andrew Thompson

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
 
N

ndrli

Why? The user can search dwon/up from the caret. If the caret is not
visible,
It is not clear from which positon to start your search.
 
A

Andrew Thompson


Why what? Please place comments after the text to which
you are replying, and trim any stuff to which you are
not replying.
..The user can search dwon/up from the caret. If the caret is not
visible,
It is not clear from which positon to start your search.

Try offering the search from 'start of selected text'(?).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,065
Members
48,770
Latest member
ElysaD

Latest Threads

Top