2011-12-19 21 views
5

Estoy usando java, y estoy tratando de hacer un JTextArea que no sea editable pero que aún tenga el cursor en el campo. En otras palabras, un área de texto que no muestra los caracteres escritos por el usuario, pero que todavía tiene el símbolo intermitente parpadeante (es decir, el foco).Mantener el cursor en TextArea cuando no se puede editar

Honestamente me quedé con este problema. He intentado hurgar con setEditable, pero no hay manera de mantener el cursor. También intenté eliminar el carácter que el usuario ingresa tan pronto como lo escriben, pero no puedo dejar de parpadear en la pantalla.

+0

cuidado: usted puede confundir a sus usuarios - un _caret_ intermitente (que es el término técnico para el marcador :-) es la pista visual estándar para ser editables – kleopatra

Respuesta

8

creo que el siguiente le ayudará a:

textArea.getCaret().setVisible(true); 

o

textArea.getCaret().setSelectionVisible(true); 
+0

Cheers, funciona perfectamente –

+1

En caso de que desee restaurar el cursor del mouse también, intente 'textArea.setCursor (Cursor.getPredefinedCursor (Cursor.TEXT_CURSOR));' – gd1

1

En cuanto a las respuestas anteriores

textArea.getCaret().setVisible(true); 

no siempre funciona a la perfección, si el TextArea o EditorPane pierde el foco, digamos que haces clic en un marco diferente o algo así, cuando vuelves, el cursor volverá a ser invisible.

He tenido los mismos problemas, parece que la solución es agregar un oyente de enfoque y configurarlo visible cada vez que el editor obtenga el foco.

text.addFocusListener(new FocusListener() { 
    public void focusLost(FocusEvent e) { 
    return; 
    } 

    public void focusGained(FocusEvent e) { 
    text.getCaret().setVisible(true); // show the caret anyway 
    } 
}); 
+0

Tuve que agregar 'text.getCaret(). setVisible (true);' en mi oyente caret porque solo en el oyente de enfoque no siempre funcionó (aunque ayudó). Esto fue con Java 8 (jdk 1.8.0_25)/Mac OS X MountainLion. – nyholku

Cuestiones relacionadas