Skip to content
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions
  • Author Profile: Govind

Simplified Learning Blog

Learning made easy

  • Java
    • Core Java Tutorial
    • Java 8
    • What is Rest API in java
    • Spring Framework
    • Type Casting in Java | 2 types Implicit and explicit casting
    • Spring Boot Tutorial
      • Spring Boot Rest API Example complete guide
    • Top 50 Java Interview Questions
    • JUnit 5 Tutorial
      • Assertall in JUnit 5
      • Assertions in JUnit 5
    • Java Thread Tutorials
      • How to create thread in Java
      • Multithreading in java
      • Daemon Thread in Java | How to create daemon thread in java
      • Top 40+ Multithreading interview questions
  • AWS
    • What is AWS (Amazon Web Services)
    • AWS IAM (Identity and Access Management)
    • AWS SNS | What is SNS
    • What is SQS | AWS SQS (Simple Queue Service)
    • What is AWS Lambda
    • Top 10 AWS Lambda interview questions
  • Java Codes
  • Software Architecture
    • Software Architecture Performance
    • Performance Principles of Software Architecture
    • System Performance Objective
  • Spring Boot Tutorial
  • Tools
    • JSON Formatter & Debugging Guide for Spring Boot Developers
    • Free Character Counter Tool: The Ultimate Guide to Counting Characters, Words, and Text Statistics
  • Tech Blogs
    • Java 21 New Features
    • Is Java Dead? Is java dead, 2023 ?
    • New Features in Java 17
  • Toggle search form

jcalendar in java swing example

Posted on April 15, 2023November 22, 2025 By Govind No Comments on jcalendar in java swing example

In this blog, we will see how to create a calendar which will show the current date using jcalendar in java swing example.

Table of Contents

Toggle
  • JCalendar
  • What Is JCalendar?
  • Key Components of the JCalendar Library
    • 1. JCalendar (The Core Component)
    • 2. JDateChooser (The Date Picker)
    • 3. JMonthChooser and JYearChooser
  • Implementation Steps (Using JDateChooser)
    • Step 1: Include the Library
    • Step 2: Basic Code Setup
  • Key Features and Customization
    • 1. Date Retrieval (The Crucial Step)
    • 2. Formatting
    • 3. Date Restrictions
    • 4. Customizing Look and Feel
  • Code for jcalendar in java swing example
  • Code Explaination
  • Complete Jcalendar code
  • Customizing JCalendar
  • Changing the Date Selection Mode
  • JCalendar in Real-World Applications
  • Conclusion
  • FAQ
  • JCalendar FAQs
    • 1. What is the licensing for JCalendar? Is it free to use?
    • 2. How can I get the selected date in a custom format (e.g., “dd-MMM-yyyy”)?
    • 3. Does JCalendar support the modern Java 8 Date/Time API (java.time)?
    • 4. How can I change the language (locale) of the calendar component?
    • 5. What is the recommended way to select a date range using JCalendar?

Let’s first look at the Swing component

JCalendar

JCalendar is a Java Swing component designed for selecting dates. It provides a highly configurable user interface and supports various date selection modes.

What Is JCalendar?

JCalendar is a Java Swing component designed to let users select dates on the swing created UI. Its highly configurable, JCalendar supports various date selection modes like single date selection, multiple dates selection and date range selection. JCalendar was built on top of Java’s DateChooserPanel to provide an intuitive user interface when selecting dates.

JCalendar is not a standard component built into the core Java Swing library (javax.swing). Instead, it is a highly popular, external, open-source component that extends Swing’s capabilities by providing a visual, interactive calendar and date-picker interface. It is part of the JCalendar library developed by Kaiser.

The primary goal of the JCalendar library is to simplify date input and selection for Java desktop applications.


Key Components of the JCalendar Library

The JCalendar library provides several classes, each designed for a specific date-related function:

1. JCalendar (The Core Component)

This is the main class representing the entire calendar display.

  • Function: Displays a full month view (like a wall calendar) allowing the user to browse months and years.
  • Purpose: Often used for static display or within a dedicated panel where the user needs a constant view of the calendar grid.
  • Interface: It implements a PropertyChangeListener mechanism, allowing developers to listen for changes when a user selects a new date.

2. JDateChooser (The Date Picker)

This is the most frequently used component for modern user interfaces.

  • Function: Provides a standard text field combined with a button that launches a pop-up calendar (similar to those found on websites).
  • Purpose: Ideal for forms where space is limited and the user needs to enter a single date efficiently.
  • Advantage: It handles date formatting and parsing automatically, simplifying the process of getting a valid java.util.Date object from the user.

3. JMonthChooser and JYearChooser

These smaller components are used internally by JCalendar and can also be used independently.

  • Function: Provides a dropdown list for selecting a month (JMonthChooser) and spinner controls for selecting a year (JYearChooser).
  • Purpose: Useful for building custom date range selectors or other date-specific filters.

Implementation Steps (Using JDateChooser)

To use JCalendar, you must first include the necessary library.

Step 1: Include the Library

Since JCalendar is external, you must download the JAR file (typically jcalendar-x.x.x.jar) and add it to your project’s build path. If using Maven, add the dependency:

XML

<dependency>
    <groupId>com.toedter</groupId>
    <artifactId>jcalendar</artifactId>
    <version>1.4</version> </dependency>

Step 2: Basic Code Setup

The following snippet shows how to instantiate and use a JDateChooser within a standard Swing application:

Java

import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import java.awt.*;
import java.util.Date;

public class CalendarExample extends JFrame {

    public CalendarExample() {
        // 1. Setup the JFrame
        setTitle("JCalendar Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        
        // 2. Instantiate JDateChooser
        JDateChooser dateChooser = new JDateChooser();
        dateChooser.setPreferredSize(new Dimension(150, 30)); 
        
        // Optional: Set a default date
        dateChooser.setDate(new Date()); 
        
        // 3. Add to the JFrame
        add(new JLabel("Select Date:"));
        add(dateChooser);
        
        // 4. Add a button to retrieve the selected date
        JButton getButton = new JButton("Get Date");
        getButton.addActionListener(e -> {
            // Retrieve the selected date as a java.util.Date object
            Date selectedDate = dateChooser.getDate();
            if (selectedDate != null) {
                JOptionPane.showMessageDialog(this, 
                    "Selected Date: " + selectedDate.toString());
            } else {
                 JOptionPane.showMessageDialog(this, "No date selected!");
            }
        });
        add(getButton);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
}

Key Features and Customization

1. Date Retrieval (The Crucial Step)

The most important method is JDateChooser.getDate(), which returns the selected date as a standard java.util.Date object. This simplifies integration with databases or other Java date/time operations.

2. Formatting

By default, the JDateChooser uses the system’s default locale and date format. You can customize the displayed format using the setDateFormatString() method:

Java

dateChooser.setDateFormatString("yyyy-MM-dd"); // Example: 2025-11-22

3. Date Restrictions

You can restrict the dates a user can select, which is crucial for applications dealing with historical data or future appointments:

  • Minimum Date: dateChooser.setMinSelectableDate(Date minDate)
  • Maximum Date: dateChooser.setMaxSelectableDate(Date maxDate)

4. Customizing Look and Feel

Since JCalendar components are based on Swing, they automatically inherit the current Look and Feel (L&F) of the application, ensuring visual consistency with other Swing components.

Code for jcalendar in java swing example

import com.toedter.calendar.JCalendar;
import javax.swing.*;

public class JCalendarSwingExampleSLB {
    public static void main(String[] args) {
        //create a JFrame object and add the name to it
        JFrame frame = new JFrame("JCalendar Swing Example");
        //set frame size as 500 * 500
        frame.setSize(500, 500);
        // create a JCalendar object using new 
        JCalendar calendar = new JCalendar();
        // Now, add this  JCalendar object to the frame
        frame.add(calendar);
        // set frame visibility to true so that it can be visisble
        frame.setVisible(true);
        // set default close operation on close click
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Code Explaination

Code for JFrame includes JCalendar and JFrame classes as required imports, while also specifying a main method with specific steps for their implementation;

An JFrame object with the specified title and size (500 by 500 pixels) is created. Once created, an instance of JCalendar class is instantiated and added to the frame – its visibility being set to true for optimal viewing results.
When executed, this program will show a window containing a calendar which the user can navigate easily.

Complete Jcalendar code

import com.toedter.calendar.JCalendar;
import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class JCalendarOperationsSLB {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JCalendar Operations");
        frame.setSize(500, 500);
        frame.setLayout(null);

        JCalendar calendar = new JCalendar();
        calendar.setBounds(50, 50, 300, 200);
        frame.add(calendar);

        JLabel label = new JLabel("Selected Date: ");
        label.setBounds(50, 270, 150, 25);
        frame.add(label);

        JDateChooser dateChooser = new JDateChooser();
        dateChooser.setBounds(50, 310, 200, 25);
        frame.add(dateChooser);

        // create a JButton to get the selected date
        JButton button1 = new JButton("Get Selected Date");
        button1.setBounds(50, 360, 200, 25);
        frame.add(button1);
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Date date = calendar.getDate();
                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
                String dateString = sdf.format(date);
                label.setText("Selected Date: " + dateString);
            }
        });

        JButton button2 = new JButton("Set to Current Date");
        button2.setBounds(270, 360, 200, 25);
        frame.add(button2);
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                calendar.setDate(Calendar.getInstance().getTime());
            }
        });

        JButton button3 = new JButton("Set to Current Date");
        button3.setBounds(270, 310, 200, 25);
        frame.add(button3);
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dateChooser.setDate(Calendar.getInstance().getTime());
            }
        });

        JButton button4 = new JButton("Clear Selected Date");
        button4.setBounds(270, 410, 200, 25);
        frame.add(button4);
        button4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                calendar.setDate(null);
                dateChooser.setDate(null);
                label.setText("Selected Date: ");
            }
        });

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Customizing JCalendar

JCalendar can be used to do a wide range of customization options to tailor its appearance and behavior to your needs. Here are some of the most commonly used customization options:

Changing the Locale
JCalendar uses the default locale to display dates. You can change the locale by calling the setLocale() method:

calendar.setLocale(Locale.US);

Changing the Date Selection Mode

JCalendar supports different date selection modes such as single date, multiple dates, and date range. we can set the date selections mode by calling the setSelectionMode() method:

calendar.setCalendarSelectionMode(CalendarSelectionModel.SINGLE_SELECTION);

JCalendar in Real-World Applications

JCalendar is an indispensable component for numerous Java applications, such as booking systems, event schedulers and project management tools. With its customizable user interface and robust set of features, it makes an excellent choice for applications requiring date selection.

Conclusion

In this blog, we have seen that using jcalendar and swing we can create java application with user interface that can display calendar.

FAQ

JCalendar FAQs

1. What is the licensing for JCalendar? Is it free to use?

JCalendar is open-source and typically released under the GNU Lesser General Public License (LGPL). This means you can use the library freely in commercial applications without having to release your own source code, provided you adhere to the LGPL terms for the JCalendar library itself.

2. How can I get the selected date in a custom format (e.g., “dd-MMM-yyyy”)?

You can directly control the date format displayed in the JDateChooser‘s text field using the setDateFormatString() method.

Java

// Example to set the format to DD-Month-YYYY
dateChooser.setDateFormatString("dd-MMM-yyyy");

However, when you retrieve the date using dateChooser.getDate(), it returns a standard java.util.Date object. To format this object into a string for display or logging, you must use the standard Java class SimpleDateFormat:

Java

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = formatter.format(dateChooser.getDate());

3. Does JCalendar support the modern Java 8 Date/Time API (java.time)?

No, the core JCalendar library components like JDateChooser and JCalendar are based on the older java.util.Date and java.util.Calendar classes, reflecting the time when the library was developed.

If your application uses the modern java.time API (like LocalDate or ZonedDateTime), you must perform a conversion after retrieval:

Java

import java.time.LocalDate;
import java.time.ZoneId;

// 1. Get the old Date object
java.util.Date oldDate = dateChooser.getDate(); 

// 2. Convert to modern LocalDate
LocalDate localDate = oldDate.toInstant()
                             .atZone(ZoneId.systemDefault())
                             .toLocalDate();

4. How can I change the language (locale) of the calendar component?

You can set the locale for the entire calendar display when creating the component or later using the setLocale() method. This affects the names of the months and days displayed in the pop-up:

Java

// Set the locale to French
dateChooser.setLocale(new Locale("fr", "FR"));

// If you want the month names to be displayed in Spanish (Spain)
// JCalendar will automatically update its labels.
dateChooser.setLocale(new Locale("es", "ES"));

5. What is the recommended way to select a date range using JCalendar?

The standard JDateChooser only allows selecting a single date. To implement a date range selector, the common pattern is to use two separate JDateChooser components:

  1. One for the Start Date.
  2. One for the End Date.

You would then add logic (e.g., using PropertyChangeListener or a simple action listener) to ensure the End Date cannot be earlier than the Start Date using the setMaxSelectableDate() and setMinSelectableDate() methods on the respective components for validation.

Govind

For over 15 years, I have worked as a hands-on Java Architect and Senior Engineer, specializing in building and scaling high-performance, enterprise-level applications. My career has been focused primarily within the FinTech, Telecommunications, or E-commerce sector, where I’ve led teams in designing systems that handle millions of transactions per day.

Checkout my profile here : AUTHOR https://simplifiedlearningblog.com/author/

Related

Java Codes Tags:jcalendar, jcalendar in java swing example, swing

Post navigation

Previous Post: Series program in java
Next Post: How to convert excel to PDF using java

More Related Articles

String to Integer Conversion in Java | Java convert string to int Java Codes
How to read Excel File using jxl Java Codes
How to convert excel to PDF using java Java Codes
Series program in java Java Codes
Converting PDF to JSON in Java Top 3 ways to code: Java Codes
Java dice roll program Java Codes

Leave a Reply Cancel reply

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

Recent Posts

  • Java Virtual Threads (Project Loom) in Real Enterprise Applications
  • Free Character Counter Tool: The Ultimate Guide to Counting Characters, Words, and Text Statistics
  • Understanding Java Sealed Classes
  • Top 50 Java Coding Interview Questions and Answers (2025 Updated)
  • Java Record Class Explained: Simple, Immutable Data Carriers

Recent Comments

  1. Gajanan Pise on Performance Principles of Software Architecture

Copyright © 2025 Simplified Learning Blog.

Powered by PressBook Green WordPress theme