JavaFX Date Picker

0

JavaFX Date Picker Tutorials:

Date Picker enables to enter a date as text or to select a date from a calendar.

The DatePicker consists of a date field and a date chooser.

Date Chooser is a popup window allows user to select a date.

Constructors :

  • DatePicker()Creates a default DatePicker instance with a null date value set.
  • DatePicker(LocalDate localDate)Creates a DatePicker instance and sets the value to the given date.

Creating Date Picker :

  • Creating a new datePicker ASDatePicker datePicker = new DatePicker();
  • And set date to the datePicker using setValue() ASdatePicker.setValue(LocalDate.of(2016, 1, 20));

Example of date picker:


package javafxtuts;
import java.time.LocalDate;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Javafxtuts extends Application {

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

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      DatePicker datePicker = new DatePicker();
      datePicker.setValue(LocalDate.of(2016, 1, 20));
      datePicker.setShowWeekNumbers(true);
      BorderPane root = new BorderPane();
      root.setTop(datePicker);
      root.setPadding(new Insets(10, 10, 10, 10));
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Output :

Explanation of date picker example :

  • Creating a new datePicker ASDatePicker datePicker = new DatePicker();
  • And set date to the datePicker using setValue() ASdatePicker.setValue(LocalDate.of(2016, 1, 20));

    So it set a date 20/1/2016 (Here type is LocalDate).

  • datePicker.setShowWeekNumbers(true);It enable the week no with date in first column of DateChooser.
  • At last we set datePicker in the top side of BorderPane Layout.
Share.

Leave A Reply