JTextArea显示Caret,而setEditable为false

Tor*_*ike 8 java swing jtextarea

在禁用setEditable时如何将Caret放入JTextArea?

当我需要Caret可见时的示例代码:

public void run(){
    JFrame frame = new JFrame();
    JTextArea text = new JTextArea();
    text.setEditable(false);
    String line = "added line";
    text.append(line);
    text.setCaretPosition(text.getCaretPosition() + line.length());

    frame.getContentPane().add(text);
    frame.setSize(300,300);
    frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)

我想要实现的是,当用户在TextArea中键入时,不能显示字符.键入的字符被重定向到OutputStream,并且接收到适当的InputStream,它将显示在TextArea中.这很好,但由于setEditable(false)隐藏了Caret.

Sta*_*avL 12

text.getCaret().setVisible(true) 和/或 text.getCaret().setSelectionVisible(true)