No necesita crear su propio componente.
Esto se puede hacer (como en I've done) utilizando un DocumentFilter personalizado.
Puede obtener el documento de textPane.getDocument()
y establecer un filtro en él por document.setFilter()
. Dentro del filtro, puede verificar la posición de la solicitud y solo permitir modificaciones si la posición es posterior al aviso.
Por ejemplo:
private class Filter extends DocumentFilter {
public void insertString(final FilterBypass fb, final int offset, final String string, final AttributeSet attr)
throws BadLocationException {
if (offset >= promptPosition) {
super.insertString(fb, offset, string, attr);
}
}
public void remove(final FilterBypass fb, final int offset, final int length) throws BadLocationException {
if (offset >= promptPosition) {
super.remove(fb, offset, length);
}
}
public void replace(final FilterBypass fb, final int offset, final int length, final String text, final AttributeSet attrs)
throws BadLocationException {
if (offset >= promptPosition) {
super.replace(fb, offset, length, text, attrs);
}
}
}
Sin embargo, esto le impide insertar mediante programación contenidos en la sección de salida (no editable) del terminal. Lo que puede hacer en su lugar es un indicador de paso en su filtro que usted configura cuando está a punto de agregar el resultado, o (lo que hice) establecer el filtro de documento en nulo antes de anexar la salida, y luego reiniciarlo cuando ' re hecho.
((AbstractDocument) jta.getDocument()). SetDocumentFilter (dfilter); –