Java Reference
In-Depth Information
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class CustomComponent extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
VBox vBox = new VBox(10);
vBox.setPadding(new Insets(10, 10, 10, 10));
vBox.setAlignment(Pos.BASELINE_CENTER);
final Label prodIdLabel = new Label("Enter Product Id:");
final ProdId prodId = new ProdId();
final Label label = new Label();
label.setFont(Font.font(48));
label.textProperty().bind(prodId.prodIdProperty());
HBox hBox = new HBox(10);
hBox.setPadding(new Insets(10, 10, 10, 10));
hBox.setAlignment(Pos.BASELINE_LEFT);
hBox.getChildren().addAll(prodIdLabel, prodId);
vBox.getChildren().addAll(hBox, label);
Scene scene = new Scene(vBox);
primaryStage.setTitle("Custom Component Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Notice that in the main program CustomComponent class, we did not load any FXML files. We simply instantiated
ProdId , and proceed to use it as if it is a native JavaFX node. The FXML file simply put two TextField s and a Label
in an HBox type fx:root . No fx:controller is set because we want to set it in the constructor of the ProdId class. In
addition to the two injected TextField s, we have another StringProperty field called prodId , for which we defined a
getter getProdId() , a setter setProdId() , and a property getter prodIdProperty() .
private StringProperty prodId = new SimpleStringProperty();
public String getProdId() {
return prodId.get();
}
public StringProperty prodIdProperty() {
return prodId;
}
Search WWH ::




Custom Search