Skip to content
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

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 and Validator
  • 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, 2023April 15, 2023 By Admin 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?
  • 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

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.

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.

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

How to convert excel to PDF using java Java Codes
Java util date to String Java Codes
Java dice roll program Java Codes
Series program in java Java Codes
String to Integer Conversion in Java | Java convert string to int Java Codes
Java Convert int to String – 5+ Methods with Examples Java Codes

Leave a Reply Cancel reply

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

Recent Posts

  • Top 50 Java Coding Interview Questions and Answers (2025 Updated)
  • Java Record Class Explained: Simple, Immutable Data Carriers
  • Java Convert int to String – 5+ Methods with Examples
  • String to Integer Conversion in Java | Java convert string to int
  • PDF to JSON Convertor

Recent Comments

No comments to show.

Copyright © 2025 Simplified Learning Blog.

Powered by PressBook Green WordPress theme