JavaFX how to show read-only text?
In FXML, add editable="false" to your TextField tag. Or uncheck the "Editable" checkbox in Scene Builder.
I needed read-only text fields, but setting the disabled property used styling which was not correct in context (in my scenario, the text field was an input to a search function where a value could be fixed in some cases - disabling the text field implied that it was not part of the search, rather than the value was fixed).
In the end I landed up using:
txtInput.setEditable(false);
txtInput.setMouseTransparent(true);
txtInput.setFocusTraversable(false);
This results in a normal looking text field that is non-reactive to the user.
Maybe just a text would serve the purpose.
Or if you want to show the text in a text field, then:
tf.setDisable(true)
I did it like this:
@FXML
private TextArea myText;
and then added the following code to initialise()
:
myText.setEditable(false);