In my application, I'm using two Tab
s. In the first one I placed a HtmlEditor
and in the second one I placed a TextArea
. HTML tab is default and when user is creating HTML input, he can switch to TextArea
in order to see or change the HTML source code directly. I've added a listener to get the htmlText
from the HtmlEditor
and set it as text in TextArea
, so user can easily switch between HTML and source mode. Here's my listener:
@FXML
private Tab htmlTab;
@FXML
private Tab sourceTab;
@FXML
private HTMLEditor htmlEditor;
@FXML
private TextArea textEditor;
htmlTab.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (htmlTab.isSelected()) {
htmlEditor.setHtmlText(textEditor.getText());
}
}
});
sourceTab.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (sourceTab.isSelected()) {
textEditor.setText(htmlEditor.getHtmlText());
}
}
});
It works fine, but HtmlEditor
is breaking text into lines automatically. When I switch to TextArea
, it's all in one line.
I thought about making a helper method which takes TextArea
length attribute to count number of chars and adds new line character every "n" characters, but maybe there is a better solution?
To add line breaks to a textarea, use the addition (+) operator and add the \r\n string at the place where you want to add a line break, e.g. 'line one' + '\r\n' + 'line two' . The combination of the \r and \n characters is used as a newline character.
To preserve line breaks when getting text from a textarea with JavaScript, we can replace whitespace characters with '<br>\n' . const post = document. createElement("p"); post. textContent = postText; post.
In FXML, add editable="false" to your TextField tag. Or uncheck the "Editable" checkbox in Scene Builder.
If you just want the text to wrap, use
textEditor.setWrapText(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With