In this JavaFX tutorial, we will see how to use the JavaFX DatePicker control to select and display the date.
A JavaFX DatePicker control enables the user to enter a date or choose a date from a wizard-like popup dialog. The popup dialog shows only valid dates, so this is an easier way for users to choose a date and ensure that both the date and date format entered in the date picker text field is valid.The JavaFX DatePicker is represented by the class javafx.scene.control.DatePicker.
JavaFX DatePicker Example
The example uses a DatePicker control to select and display the date. The date is shown in a label control.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
initUI(stage);
}
private void initUI(Stage stage) {
var root = new VBox(15);
root.setPadding(new Insets(10));
var lbl = new Label("...");
var datePicker = new DatePicker();
datePicker.setOnAction(e -> {
var date = datePicker.getValue();
lbl.setText(date.toString());
});
root.getChildren().addAll(datePicker, lbl);
var scene = new Scene(root, 350, 200);
stage.setTitle("Date picker");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output:
Related JavaFX Examples
- JavaFX GridPane Example
- JavaFX ColorPicker Example
- JavaFX DatePicker Example
- JavaFX MenuBar Example
- JavaFX Radio Button Example
- JavaFX TabPane Example
- JavaFX Accordion Example
- JavaFX Login Form Validation Example
- JavaFX Form Validation - Registration Form Validation Example
- JavaFX Line Chart Example
- JavaFX Area Chart Example
- JavaFX Scatter Chart Example
- JavaFX Bar Chart Example
- JavaFX Pie Chart Example
- JavaFX Select and Multi-Select Example
- JavaFX Check Box Example
- Java Calculator Project
Comments
Post a Comment
Leave Comment