Documente Academic
Documente Profesional
Documente Cultură
Calendar Example
Latest Tutorials | Questions and Answers | Ask Questions? | Site Map
Home
Tutorials
Swt
Calendar Example
Questions:Ask | Latest
Search
Calendar Example
Format Java Learn Java Example Web Java
Share on Google+
In this section, you will study how the calendar shows the previous year and next year dates.
Advertisement
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;
Top Tutorials
Java
import org.eclipse.swt.custom.CLabel;
public class AnotherCalendarExample extends Dialog { Display display; Date date; String selectedDate;
www.roseindia.net/tutorials/swt/calendar.shtml
1/7
12/24/13
Calendar Example
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(""); } }
Struts 2 Home Architecture of Struts 2 How Struts 2 Framework works? Struts 2 Hello World Example Struts 2 Features Struts 2 Tutorial Struts version 2.3.15.1
JPA
Programming Tutorials
Tutorials Directory Android Tutorials Core Java JSP Tutorials Servlets Tutorials Database Tutorials JDBC Tutorials Struts Tutorials Hibernate Tutorials Spring Framework JPA Tutorials JSF Tutorials Enterprise Java JEE Technologies J2ME Tutorials XML Tutorials Web Services Tutorials Web Development PHP Tutorial Tutorials Web Graphics Designing Technology Tutorials Cloud Computing Vehicle Tracking Technology Software Technology Tutorial JSF Tutorials & Examples Software Project Management Programming Questions Maven2 Tutorial JavaScript Careers Guide >>>More Tutorials...
www.roseindia.net/tutorials/swt/calendar.shtml
2/7
12/24/13
} 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();
Calendar Example
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();
www.roseindia.net/tutorials/swt/calendar.shtml
3/7
12/24/13
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) {
Home Java Frameworks nextMonth(); Database Technology
Calendar Example
Web Development
Build/Test Tools
Servers
PHP
Related Tutorials
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);
X label4.setText("Wed");
39 follow ers 0
Share on Google+
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");
www.roseindia.net/tutorials/swt/calendar.shtml
4/7
12/24/13
label7.setText("Sat");
Calendar Example
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(); } }
Advertisement
Previous
Index
Next
Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with
www.roseindia.net/tutorials/swt/calendar.shtml
the recent trends of Java and other open source platforms. Connect Me on Google+
5/7
12/24/13
Calendar Example
the recent trends of Java and other open source platforms. Connect Me on Google+
Like
Share
Tw eet
Share
+1
Roseindia
Like
Popular
Latest
Frameworks
Categories
Ask Questions?
Your Em ail :
Subject (*):
S UBMI T
COMMENTS
Services
Software Solutions
JSF Development
Website Development
Web Designing
Content Development
Content Development
E-Commerce Solutions
CRM
www.roseindia.net/tutorials/swt/calendar.shtml
6/7
12/24/13
JSF Development Outsourcing ERP M-Commerce Flex Development Web Designing Web Redesigning Web Development Logo Design Web Design Packages Domain Registration
Calendar Example
Services
SEO Services Search Eng. Optimization Search Eng. Submission SEO Tips SEO Portfolio Web Promotion Plans Content Development Article Writing Blog Writing New s Writing SEO Copyw riting Technical Documentation Article Marketing ASP.NET Hosting Unix Hosting E-Commerce Hosting Window s Hosting Hosting Plan CRM
Home
Privacy Policy
www.roseindia.net/tutorials/swt/calendar.shtml
7/7