Wednesday, April 9 2025

Header Ads

How to Develop Java Calendar using Swing


As Java Developer, you had learned about Swing which is a lightweight GUI toolkit. It is used for building window-based applications. It is used for building applications like Calendar, Alarm Clock, and many more. So here we are going to discuss how to build Calendar using Java Swing.
I have developed a Calendar  using Swing which looks like this : 





This program consists of two panels in which one consists of all buttons, labels .etc and the other contains Textarea which we will see in code. In this, we have used GregorianCalendar which is a concrete subclass of  Calendar and it provides the standard calendar system used by most of the world. In this Calendar, it is programmed like that it can move 100 years forward and 100 years backward, which you can edit it according to your requirement. So let move on to code :


/**
* Java Swing Calendar Project
* @author : Harval Techzone
* @since 29-October-2020
* @version 1.0
*/
package Calendar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.TableView.TableCell;
public class Calendar
{
// Declaration of static variables //
static JFrame f;
static Container pane;
static JPanel p1;
static JButton next, prev, today;
static JComboBox cmbyear;
static JLabel month, year;
static JTable table;
static DefaultTableModel calendar;
static JScrollPane s1;
static int realYear, realMonth, realDay, currentYear, currentMonth;
public static void main(String[] args)
{
// Creation of frame and container //
f = new JFrame("Calendar");
f.setSize(480, 580);
f.setVisible(true);
f.setResizable(false);
pane = f.getContentPane();
pane.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p2 = new JPanel();
p2.setBounds(0, 0, 265, 537);
p2.setLayout(null);
JTextArea t1 = new JTextArea();
t1.setBounds(0, 0, 250, 535);
p2.setBackground(Color.BLACK);
p2.setBorder(BorderFactory.createTitledBorder("calendar"));
t1.setBackground(Color.gray);
t1.setForeground(Color.red);
t1.setFont(new Font("monaco", Font.CENTER_BASELINE, 18));
JScrollPane sp = new JScrollPane(t1);
sp.setHorizontalScrollBarPolicy(sp.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setVerticalScrollBarPolicy(sp.VERTICAL_SCROLLBAR_AS_NEEDED);
sp.setViewportBorder(new LineBorder(Color.black));
sp.getViewport().add(t1, null);
p2.add(sp, BorderLayout.CENTER);
t1.setToolTipText("To-do-List");
// Create Controls //
p1 = new JPanel(null);
Icon icon1 = new ImageIcon("C:\\Users\\Admin\\Downloads\\next1.png");
next = new JButton(icon1);
next.setToolTipText("Go forward a month");
Icon icon2 = new ImageIcon("C:\\Users\\Admin\\Downloads\\prev1.png");
prev = new JButton(icon2);
prev.setToolTipText("Go back a month");
today = new JButton("Today");
today.setToolTipText("Current Date");
cmbyear = new JComboBox();
cmbyear.setToolTipText("Change year");
month = new JLabel("January");
month.setToolTipText("Current month");
month.setFont(new Font("Arial", Font.BOLD, 20));
year = new JLabel("Change year:");
year.setFont(new Font("Arial", Font.BOLD, 20));
calendar = new DefaultTableModel()
{
public boolean isCellEditable(int rowIndex, int mColIndex)
{
return false;
}
};
table = new JTable(calendar);
table.setFont(new Font("Arial", Font.BOLD, 15));
table.setBackground(Color.LIGHT_GRAY);
s1 = new JScrollPane(table);
// Register action listner //
next.addActionListener(new next_Action());
prev.addActionListener(new prev_Action());
today.addActionListener(new today_action());
cmbyear.addActionListener(new cmyear_Action());
// Add to controls //
pane.add(p1);
p1.add(next);
p1.add(today);
p1.add(cmbyear);
p1.add(prev);
p1.add(month);
p1.add(year);
p1.add(s1);
// Set Bounds //
p1.setBounds(7, 0, 455, 537);
cmbyear.setBounds(335, 490, 100, 30);
prev.setBounds(10, 20, 65, 35);
next.setBounds(90, 20, 65, 35);
today.setBounds(353, 20, 85, 35);
month.setBounds(220 - month.getPreferredSize().width / 2, 25, 150, 25);
year.setBounds(10, 410, 180, 200);
s1.setBounds(10, 55, 430, 420);
GregorianCalendar cal = new GregorianCalendar();
realDay = cal.get(GregorianCalendar.DAY_OF_MONTH);
realMonth = cal.get(GregorianCalendar.MONTH);
realYear = cal.get(GregorianCalendar.YEAR);
currentMonth = realMonth;
currentYear = realYear;
// Add headers //
String[] headers = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
for (int i = 0; i < 7; i++)
{
calendar.addColumn(headers[i]);
}
// No resize/reorder //
table.getTableHeader().setResizingAllowed(false);
table.getTableHeader().setReorderingAllowed(false);
// Single cell selection //
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Set row/column count //
table.setRowHeight(66);
calendar.setColumnCount(7);
calendar.setRowCount(6);
// Combo box of year //
for (int i = realYear - 100; i <= realYear + 100; i++)
{
cmbyear.addItem(String.valueOf(i));
}
// Refresh calendar //
refreshCalendar(realMonth, realYear);
}
private static void refreshCalendar(int rmonth, int ryear)
{
String[] months = { "January", "February", "March",
"April", "May", "June", "July", "August",
"September","October", "November", "December" };
int nod, som; // Number Of Days, Start Of Month
prev.setEnabled(true);
next.setEnabled(true);
today.setEnabled(true);
if (rmonth == 0 && ryear <= realYear - 10) {
prev.setEnabled(false);
}
if (rmonth == 11 && ryear >= realYear + 100) {
next.setEnabled(false);
}
if (rmonth == realMonth && ryear == realYear) {
today.setEnabled(false);
}
month.setText(months[rmonth]);
month.setBounds(220 - month.getPreferredSize().width / 2, 25, 150, 25);
cmbyear.setSelectedItem(String.valueOf(ryear));
// Clear table //
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
calendar.setValueAt(null, i, j);
}
}
// Get first day of month and number of days //
GregorianCalendar cal = new GregorianCalendar(ryear, rmonth, 1);
nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
som = cal.get(GregorianCalendar.DAY_OF_WEEK);
// Draw calendar //
for (int i = 1; i <= nod; i++)
{
int row = new Integer((i + som - 2) / 7);
int column = (i + som - 2) % 7;
calendar.setValueAt(i, row, column);
}
// Apply renderers //
table.setDefaultRenderer(table.getColumnClass(0), new tblCalendarRenderer());
}
static class tblCalendarRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value,
boolean selected, boolean focused,int row, int column)
{
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
if (column == 0 || column == 6)
{ // Week-end
setBackground(new Color(255, 220, 220));
}
else
{ // Week
setBackground(new Color(255, 255, 255));
}
if (value != null)
{
if (Integer.parseInt(value.toString()) == realDay &&
currentMonth == realMonth && currentYear == realYear)
{ // Today
setBackground(new Color(220, 220, 255));
}
}
setBorder(null);
setForeground(Color.black);
return this;
}
}
static class prev_Action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (currentMonth == 0)
{
// Back one year //
currentMonth = 11;
currentYear -= 1;
}
else
{
// Back one month //
currentMonth -= 1;
}
refreshCalendar(currentMonth, currentYear);
}
}
static class next_Action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (currentMonth == 11)
{ // Foward one year
currentMonth = 0;
currentYear += 1;
}
else
{ // Foward one month
currentMonth += 1;
}
refreshCalendar(currentMonth, currentYear);
}
}
static class cmyear_Action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (cmbyear.getSelectedItem() != null) {
String b = cmbyear.getSelectedItem().toString();
currentYear = Integer.parseInt(b);
refreshCalendar(currentMonth, currentYear);
}
}
}
static class today_action extends DefaultTableCellRenderer implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)
{
currentMonth = realMonth;
currentYear = realYear;
refreshCalendar(currentMonth, currentYear);
}
}
}
view raw Calendar.java hosted with ❤ by GitHub





















Leave a Comment

No comments:

Powered by Blogger.