Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • 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
334
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • 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

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

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
586
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 *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
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
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
JavaFX 17

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

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

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

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

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

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

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    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! I'm Kent. 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.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

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

© 2024 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.