In this blog, we will see how to create a calendar which will show the current date using jcalendar in java swing example.
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.