More Tutorials| Bioinformatics| Open Source| Photoshop| Questions? | Software Development
 

Draw Calendar in SWT

This section illustrates you how to draw a calendar in SWT, showing current date.

Draw Calendar in SWT

                         

This section illustrates you how to draw  a calendar in SWT, showing current date.

SWT allows to create Calendar by using the java.util.Calendar class. The class CLabel draws the aligned text with different border styles. We are using this class to write the day and the days of week. The class GridLayout provides the configuration of GridData in the layout and the GridData sets the configuration fields.

For getting the number of days in the month, we have create a condition in the method getMonthDays(). If the month is 1, 3, 5, 7, 8, 10 or 12, it will return 31 days and if the month is 4, 6, 9 or 11, it will return 30 days. If the month is 2, then check if the month is leap, return 29 days otherwise return 28. To find whether the year is leap, we have create a method isLeap().

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 CalendarExample.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 CalendarExample extends Dialog{
   Display display;
     Date date;
     Shell shell;
     GridLayout gridLayout;
     GridData gridData;
     CLabel label1, label2, label3, label4, label5, label6, label7;
     CLabel label;
     CLabel[] days = new CLabel[42];

    public CalendarExample(Shell parent, int style) {
        super(parent, style);
    }
    public CalendarExample(Shell parent) {
        this(parent, 0);
    }
    private int getMonthDays(int year, int month) {
        if (month == ||month == ||month == ||month == ||
            month == ||month == 10 ||month == 12) {
            return 31;
        }
        if (month == ||month == ||month == ||month == 11) {
            return 30;
        }
        if (month == 2) {
            if (isLeap(year)) {
                return 29;
            else {
                return 28;
            }
        }
        return 0;
    }
    public boolean isLeap(int year) {
        return (year % == && year % 100 != 0) || (year % 400 == 0);
    }
    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 color = 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 open() {
        Shell parent = getParent();
        display = Display.getDefault();
        shell = new Shell(parent,  SWT.TITLE |SWT.PRIMARY_MODAL);
        shell.setText("Calendar");
        shell.setSize(350250);

        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("EEE,d MMM yyyy HH:mm:ss");
        label.setText(dateFormat.format(new Date()));
    
        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(); 
        setDisplayDay(calendar);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
      public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        CalendarExample calendarExample= new CalendarExample(shell);
        calendarExample.open();
    }
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: c class list orm text select form console button io swt size method sed display sets toolbar ole this location

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.