In my application, I'm using two Tabs. 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?