JavaFX DatePicker

107 篇文章 6 订阅

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 .

The DatePicker is a subclass of the ComboBox class, and thus shares some similarities with this class.

Creating a DatePicker

You create a DatePicker control via the constructor of the DatePicker class. Here is a JavaFX DatePicker instantiation example:

DatePicker datePicker = new DatePicker();

Adding a DatePicker to the Scene Graph

To make a DatePicker visible it must be added to the JavaFX scene graph. This means adding it to a Scene object, or to a layout component which is added to a Scene object.

Here is an example showing how to add a JavaFX DatePicker to the scene graph:

package com.jenkov.javafx.controls;

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

public class DatePickerExperiments extends Application  {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Button Experiment 1");

        DatePicker datePicker = new DatePicker();

        HBox hbox = new HBox(datePicker);

        Scene scene = new Scene(hbox, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

The application resulting from running this example would look similar to this:

 

Reading the Selected Date

Reading the date selected in the DatePicker can be done using its getValue() method. Here is an example of reading the selected date from a DatePicker:

LocalDate value = datePicker.getValue();

The getValue() returns a LocalDate object representing the date selected in the DatePicker.

Here is a full example with a button added to extract the selected date in the DatePicker when the button is clicked:

package com.jenkov.javafx.controls;

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

import java.time.LocalDate;

public class DatePickerExperiments extends Application  {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("DatePicker Experiment 1");

        DatePicker datePicker = new DatePicker();

        Button button = new Button("Read Date");

        button.setOnAction(action -> {
            LocalDate value = datePicker.getValue();
        });

        HBox hbox = new HBox(datePicker);

        Scene scene = new Scene(hbox, 300, 240);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值