I have a custom component representing a form field. It has a label, a text field and an error message that may be shown after input validation.
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="109.0" prefWidth="512.0" spacing="10.0" styleClass="vbox" type="VBox" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="fieldLabel" text="Lorem ipsum dolor sit amet"></Label>
<TextField fx:id="textField" promptText="Lorem ipsum dolor sit amet"></TextField>
<Label fx:id="errorLabel" text=""></Label>
</children>
</fx:root>
public class FormField extends VBox {
@FXML private TextField textField;
@FXML private Label fieldLabel;
@FXML private Label errorLabel;
private String fieldLabelText;
private String promptText;
public FormField(@NamedArg("fieldLabelText") String fieldLabelText,
@NamedArg("promptText") String promptText,
@NamedArg("isPasswordField") boolean isPasswordField) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("../../resources/fxml/form-field.fxml"));
loader.setRoot(this);
loader.setController(this);
this.fieldLabelText = fieldLabelText;
this.promptText = promptText;
try {
loader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
@FXML
public void initialize() {
this.fieldLabel.setText(fieldLabelText);
this.textField.setPromptText(promptText);
}
Now what I want to know is how would I go about making an extension of this component that has a PasswordField
instead of a TextField
? Or passing an argument such as boolean isPasswordField
that let's FormField
decide if it should render a TextField
or a PasswordField
? If TextField
had an obscureText(true)
method in it's API it would be good enough since this is all I'm looking for, but I couldn't find any.
All I could find about JavaFX inheritance was in the sense of "extending" the object by adding new components to it, not by changing it's existing elements.