FreeCode

Donate HTML/CSS JavaFX Java Home
JavaFX Starter Template JavaFX Labels JavaFX Buttons JavaFX Toggle Buttons JavaFX Radio Buttons and Toggle Groups JavaFX Checkboxes JavaFX Choice Boxes JavaFX Combo Boxes JavaFX Text Areas JavaFX Text Fields JavaFX List Views JavaFX Image Views JavaFX Color Pickers JavaFX Date Pickers JavaFX Resizable Split Pane Layouts JavaFX VBox (Vertical Box Layouts) JavaFX HBox (Horizontal Box Layouts) JavaFX Flow Pane Layouts JavaFX Tile Pane Layouts JavaFX Web Views

JavaFX Date Pickers

Code Description:

The code provided below shows how to create an awesome Date Picker with a single line of code. You can use this to extract a LocalDate object and gather the month, day, and year from it.

Date Picker Syntax:

DatePicker datePicker = new DatePicker();

Runnable JavaFX Date Picker Example Code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Demo extends Application {

    public static void main(String[] args) {
	launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
		
	VBox root = new VBox();
		
        DatePicker datePicker = new DatePicker();
		
	root.getChildren().add(datePicker);

	primaryStage.setTitle("Date Picker Demo");
	primaryStage.setScene(new Scene(root, 200, 200));
	primaryStage.show();
		
    }

}

How to Run This Code:

Make sure that your Class name is the same as ours. In this case, we are using the Class name of "Demo". Alternatively, you can change the Class name in the provided code to match yours.

Note: You will NOT be able to copy and paste this code into your IDE unless you first set up JavaFX for your project. Check out this link if you need help setting up JavaFX:

JavaFX Comprehensive Setup Guide

Output:

JavaFX Date Picker Output Image

Important Methods:

  • setValue() - Used to set the date in the date picker. EX: " datePicker.setValue(LocalDate.now());"
  • getValue() - Used to get the currently selected date value which can be stored in a LocalDate object.
  • setShowWeekNumbers() - Updates the date picker popup to show the number of the week such as 1 - 52.
  • setDisable() - Used to make the date picker grayed out and unclickable. EX: "datePicker.setDisable(true);"
  • setPrefSize() - Set the preferred width and height for the date picker.
  • setTooltip() - Set a message that displays when a user hovers over the datePicker control for a certain amount of time. EX: "datePicker.setTooltip(new Tooltip("Pick a date!"));"
  • setVisible() - Show or hide a date picker from the scene. EX: "datePicker.setVisible(false);"

Related Links:

JavaFX Color Pickers