Calendar Example
In this section, you will study how the calendar shows the previous year and next year dates.
SWT allows to create Calendar by using the java.util.Calendar class. The class CLabel allows to write the day and the days of week. The class GridLayout provides the configuration of GridData in the layout. The class GridData sets the configuration fields.
We have created the method getMonthDays() that will return the number of days of the month. The Button class of package org.eclipse.swt.widgets allows to create a button. We have created four buttons for performing action to display the next year, previous year, next month and previous month by calling SelectionAdapter class.
The gridLayout.numColumns specifies the number of columns in the layout. The method setLayoutData() sets the layout data. The gridData.horizontalSpan specifies the number of columns that the control will take up. The gridData.widthHint specifies the minimum width for the column and gridData.heightWidth specifies the minimum height for the row.
Following code displays the date format:
SimpleDateFormat dateFormat = new
SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss"); label.setText(dateFormat.format(new Date())); |
To display the current day, we have create a method setDisplayDay(). This method is called by the object of the class java.util.Calender. The method getInstance() gets the calendar with default time and locale.
Here is the code of AnotherCalendarExample.java
import java.text.*; import java.util.*; import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; import org.eclipse.swt.events.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.custom.CLabel; public class AnotherCalendarExample extends Dialog { Display display; Date date; String selectedDate; Shell shell; GridLayout gridLayout; GridData gridData; CLabel label1, label2, label3, label4, label5, label6, label7; Button button1,button2,button3,button4 ; CLabel label, labelText; CLabel[] days = new CLabel[42]; public AnotherCalendarExample(Shell parent, int style) { super(parent, style); } public AnotherCalendarExample(Shell parent) { this(parent, 0); } private int getMonthDays(int year, int month) { if (month == 1 ||month == 3 ||month == 5 ||month == 7 || month == 8 ||month == 10 ||month == 12) { return 31; } if (month == 4 ||month == 6 ||month == 9 ||month == 11) { return 30; } if (month == 2) { if (isLeap(year)) { return 29; } else { return 28; } } return 0; } public boolean isLeap(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } private void moveTo(int type, int value) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(type, value); date = new Date(cal.getTimeInMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); label.setText(dateFormat.format(date)); setDisplayDay(cal); } private void setDisplayDay(Calendar cal) { int currentDay = cal.get(Calendar.DATE); cal.add(Calendar.DAY_OF_MONTH, -(cal.get(Calendar.DATE) - 1)); int startIndex = cal.get(Calendar.DAY_OF_WEEK) - 1; int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int lastDay = this.getMonthDays(year, month); int endIndex = startIndex + lastDay - 1; int startday = 1; for (int i = 0; i < 42; i++) { Color temp = days[i].getBackground(); days[i].setBackground(display.getSystemColor(SWT.COLOR_CYAN)); } for (int i = 0; i < 42; i++) { if (i >= startIndex && i <= endIndex) { days[i].setText("" + startday); if (startday == currentDay) { days[i].setBackground(display.getSystemColor(SWT.COLOR_RED)); } startday++; } else { days[i].setText(""); } } } public void previousYear() { moveTo(Calendar.YEAR, -1); } public void nextYear() { moveTo(Calendar.YEAR, 1); } public void nextMonth() { moveTo(Calendar.MONTH, 1); } public void previousMonth() { moveTo(Calendar.MONTH, -1); } public void mouseDown(MouseEvent e) {} public void mouseUp(MouseEvent e) {} public Object open() { Shell parent = getParent(); display = Display.getDefault(); shell = new Shell(parent, SWT.TITLE |SWT.PRIMARY_MODAL); shell.setText("Calendar"); shell.setSize(370, 250); gridLayout = new GridLayout(); gridLayout.numColumns = 7; shell.setLayout(gridLayout); label = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.horizontalSpan = 7; label.setLayoutData(gridData); SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); label.setText(dateFormat.format(new Date())); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.horizontalSpan = 1; button1 = new Button(shell, SWT.PUSH | SWT.FLAT); button1.setText("Y<"); button1.setLayoutData(gridData); button1.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { previousYear(); } }); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.horizontalSpan = 1; button2 = new Button(shell, SWT.PUSH | SWT.FLAT); button2.setText("Y>"); button2.setLayoutData(gridData); button2.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { nextYear(); } }); labelText = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.horizontalSpan = 3; labelText.setLayoutData(gridData); labelText.setText("*************"); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.horizontalSpan = 1; button3 = new Button(shell, SWT.PUSH | SWT.FLAT); button3.setText("M<"); button3.setLayoutData(gridData); button3.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { previousMonth(); } }); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.horizontalSpan = 1; button4 = new Button(shell, SWT.PUSH | SWT.FLAT); button4.setText("M>"); button4.setLayoutData(gridData); button4.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { nextMonth(); } }); label1 = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT); gridData = new GridData(GridData.FILL_HORIZONTAL |GridData.FILL_VERTICAL); gridData.widthHint = 22; gridData.heightHint = 22; label1.setLayoutData(gridData); label1.setText("Sun"); label2 = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT); gridData = new GridData(GridData.FILL_HORIZONTAL |GridData.FILL_VERTICAL); gridData.widthHint = 22; gridData.heightHint = 22; label2.setLayoutData(gridData); label2.setText("Mon"); label3 = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT); gridData = new GridData(GridData.FILL_HORIZONTAL |GridData.FILL_VERTICAL); gridData.widthHint = 22; gridData.heightHint = 22; label3.setLayoutData(gridData); label3.setText("Tue"); label4 = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT); gridData = new GridData(GridData.FILL_HORIZONTAL |GridData.FILL_VERTICAL); gridData.widthHint = 22; gridData.heightHint = 22; label4.setLayoutData(gridData); label4.setText("Wed"); label5 = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT); gridData = new GridData(GridData.FILL_HORIZONTAL |GridData.FILL_VERTICAL); gridData.widthHint = 22; gridData.heightHint = 22; label5.setLayoutData(gridData); label5.setText("Thu"); label6 = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT); gridData = new GridData(GridData.FILL_HORIZONTAL |GridData.FILL_VERTICAL); gridData.widthHint = 22; gridData.heightHint = 22; label6.setLayoutData(gridData); label6.setText("Fri"); label7 = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT); gridData = new GridData(GridData.FILL_HORIZONTAL |GridData.FILL_VERTICAL); gridData.widthHint = 22; gridData.heightHint = 22; label7.setLayoutData(gridData); label7.setText("Sat"); for (int i = 0; i < 42; i++) { days[i] = new CLabel(shell, SWT.FLAT | SWT.CENTER); gridData = new GridData(GridData.FILL_HORIZONTAL |GridData.FILL_VERTICAL); days[i].setLayoutData(gridData); days[i].setBackground(display.getSystemColor(SWT.COLOR_WHITE)); } Calendar calendar = Calendar.getInstance(); date = new Date(calendar.getTimeInMillis()); setDisplayDay(calendar); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } return selectedDate; } public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setText(""); AnotherCalendarExample calendarExample = new AnotherCalendarExample(shell); calendarExample.open(); } } |
Output will be displayed as