Kensoft PH
  • Download
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to use the DatePicker in JavaFX | 100% Free Tutorial

August 30, 2022 - Updated on November 2, 2022
in Java
Reading Time: 3 mins read
0
JavaFX DatePicker Website
90
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

  • DatePicker in JavaFX Tutorial
  • How to use the DatePicker in JavaFX
    • Create the JavaFX DatePicker
    • Add to the Scene Graph
      • Output
    • Get the selected date
      • Output
    • Editable DatePicker
    • Styling with CSS
  • YouTube Video

DatePicker in JavaFX Tutorial

DatePicker in JavaFX is the same as the JavaFX ComboBox and JavaFX ColorPicker because this node or control uses the ComboBoxBase. The DatePicker is a ComboBox style control. When a user clicks the DatePicker, it will pop up a calendar. The DatePicker also allows the user to enter a date or select a date from a calendar.

How to use the DatePicker in JavaFX

From the word date picker, the user will select a date from the calendar and the date will display if you want to display it. The DatePicker is very easy to create and use; you just simply create an instance of the DatePicker constructor. In this tutorial, I will walk you through creating the DatePicker. We will also get the selected date and display it in the application.

You can create the date picker using its constructor. You need to create an instance of the DatePicker constructor, and you can also pass an initial date using the LocaDate. The following example below will show you how to create the DatePicker in JavaFX and will show more examples below.

Create the JavaFX DatePicker

Use its default constructor to create the date picker. You can also create the Date picker easily using the Scene Builder if you are developing an FXML-based application. See the example code below to create without using Scene Builder.

// Create a DatePicker with null as its initial value
DatePicker datePicker = new DatePicker();

//Create a DatePicker with an initial date
DatePicker datePicker = new DatePicker(LocalDate.of(2022, 8, 19));

Add to the Scene Graph

Once you have created the DatePicker in JavaFX, what you are going to do next is to add it to the scene graph. If you are not using the scene builder, you need to know how the layout in JavaFX works. Please see the following example below.

// create the date picker
DatePicker datePicker = new DatePicker();

// create the layout
StackPane layout = new StackPane();
Scene scene = new Scene(layout, 400, 400);
layout.getChildren().add(datePicker);
stage.setScene(scene);
stage.show();

Output

DatePicker in JavaFX

Get the selected date

When you select a date from the JavaFX DatePicker and wonder how you can get the selected date and display it in your application. You need to use the getValue() method to get the selected date. See the following example code below.

DatePicker datePicker = new DatePicker();
datePicker.getValue();

If you want your selected date to be something like August 29, 2022, you should use the DateTimeFormatter. Create an Action Event for your DatePicker and follow the example code below.

private void datePicker(ActionEvent evt){
    LocalDate localDate = datePicker.getValue();
    String pattern = "MMMM dd, yyyy";
    String datePattern = localDate.format(DateTimeFormatter.ofPattern(pattern));
    selectedDate.setText("Selected Date: "+datePattern);
}

Output

JavaFX DatePicker

Editable DatePicker

The JavaFX DatePicker can also be editable. When you enable this feature, you can enter a date manually in the JavaFX DatePicker. Otherwise, you can’t enter a date manually when you disable this feature. See the following code below to learn more.

DatePicker datePicker = new DatePicker();
// users can't enter a date
datePicker.setEditable(false);

Styling with CSS

JavaFX has also the ability to allow Cascading Style Sheets (CSS). You can add an inline style or an external CSS file. If you don’t know how to add JavaFX CSS to your JavaFX application. You can click the given link to learn more about JavaFX CSS. The following example code below will show you how to add CSS codes to your CSS file.

/* Display current day numbers in bolder font */
.date-picker-popup > * > .today {
 -fx-font-weight: bolder;
}
/* Display all day numbers in blue */
.date-picker-popup > * > .day-cell {
 -fx-text-fill: blue;
}

YouTube Video

Previous Post

How to use the ColorPicker in JavaFX | 100% Perfect Tutorial

Next Post

6 JavaFX Text Field Examples Perfect for Beginners

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide
Java

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

March 26, 2023
2
File Chooser in JavaFX Tutorial
Java

File Chooser in JavaFX: 100% Perfect Step-by-Step Guide

March 4, 2023
18
JavaFX TabPane Tutorial
Java

How to use the TabPane in JavaFX | 100% Perfect Tutorial

March 2, 2023
11
Next Post
JavaFX TextField

6 JavaFX Text Field Examples Perfect for Beginners

JavaFX PasswordField

4 examples of PasswordField in JavaFX | Free for beginners

JavaFX TextArea

6 examples of Text Area in JavaFX | Perfect for beginners

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Maximum upload file size: 20 MB. You can upload: image, audio, video, document, text. Drop files here

  • Trending
  • Comments
  • Latest
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
change the stage icon in JavaFX

How to change the stage icon in JavaFX | 100% best for beginners

May 23, 2021 - Updated on September 24, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

send SMS in java

How to send SMS in Java | 100% best example

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

March 26, 2023
File Chooser in JavaFX Tutorial

File Chooser in JavaFX: 100% Perfect Step-by-Step Guide

March 4, 2023
JavaFX TabPane Tutorial

How to use the TabPane in JavaFX | 100% Perfect Tutorial

March 2, 2023
How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023

Latest Tutorials

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

March 26, 2023
File Chooser in JavaFX Tutorial

File Chooser in JavaFX: 100% Perfect Step-by-Step Guide

March 4, 2023
JavaFX TabPane Tutorial

How to use the TabPane in JavaFX | 100% Perfect Tutorial

March 2, 2023

Popular Tutorials

  • Failed to automatically set up a JavaFX Platform

    Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Website

Privacy Policy

Terms and Condition

Sitemap

Services

Guest Post

Fiverr

Freelancer

Upwork

Categories

Website Status

Check the status

Latest Tutorials

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

JavaFX DirectoryChooser: 100% Perfect Step-By-Step Guide

March 26, 2023
File Chooser in JavaFX Tutorial

File Chooser in JavaFX: 100% Perfect Step-by-Step Guide

March 4, 2023
JavaFX TabPane Tutorial

How to use the TabPane in JavaFX | 100% Perfect Tutorial

March 2, 2023

© 2023 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
  • Java Quiz
  • Contact
  • About

© 2023 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.