1、版面调用
text_17 = new Text(composite_2, SWT.BORDER );
FormData fd_text_17 = new FormData();
fd_text_17.top = new FormAttachment(combo_4,0,SWT.TOP);
fd_text_17.bottom = new FormAttachment(combo_4,0,SWT.BOTTOM);
fd_text_17.left = new FormAttachment(label_23, 2);
fd_text_17.right = new FormAttachment(label_23, 146,SWT.RIGHT);
text_17.setLayoutData(fd_text_17);
//text_17.setEnabled(false);
text_17.addMouseListener(new MouseAdapter(){
@Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseUp(e);
OpenDateToText.getDateToText(e, shell);
}
});
2、公共代码
public class OpenDateToText {
public static void getDateToText(MouseEvent e,Shell shell){
Text bt = (Text) e.widget;
Point er = bt.getLocation();
DateDialog dl = new DateDialog(shell);
int x = er.x+20;
int y = er.y+177; //定位
dl.open(x,y,bt.getText());
bt.setText(dl.selectedDate);
}
}
3、日期控件代码
public class DateDialog extends Dialog implements MouseListener {
private static String ID = "com.ss.sfm.util.DateDialog";
private Display display = null;
private Date nowDate = null; // current date
public String selectedDate = ""; // selected date
public Shell shell = null;
public Shell parent = null;
private GridLayout gridLayout = null;
private GridData gridData = null;
private CLabel sunday = null;
private CLabel monday = null;
private CLabel tuesday = null;
private CLabel wednesday = null;
private CLabel thursday = null;
private CLabel friday = null;
private CLabel saturday = null;
private Button yearUp = null;
private Button yearNext = null;
private Button monthUp = null;
private Button monthNext = null;
private CLabel nowLabel = null;
private CLabel[] days = new CLabel[42];
private boolean hasChanged = false;
public DateDialog(Shell parent, int style) {
super(parent, style);
}
public DateDialog(Shell parent) {
this(parent, 0);
}
private int getLastDayOfMonth(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 (isLeapYear(year)) {
return 29;
} else {
return 28;
}
}
return 0;
}
public boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
private void moveTo(int type, int value) {
Calendar now = Calendar.getInstance(); // get current Calendar object
now.setTime(nowDate); // set current date
now.add(type, value); // add to spec time.
nowDate = new Date(now.getTimeInMillis()); // result
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");// format
// date
nowLabel.setText(formatter.format(nowDate)); // set to label
setDayForDisplay(now,"");
}
private void setDayForDisplay(Calendar now,String sDate){
int currentDay =now.get(Calendar.DATE);
//System.out.println("currentDay="+currentDay);
if(!DealStrNull.judgeStrNull(sDate)){
if(sDate.length()>10){
}else if(sDate.length()==10){
currentDay = Integer.parseInt(sDate.substring(8,sDate.length()));
}else if(sDate.length()==9){
currentDay = Integer.parseInt(sDate.substring(8,sDate.length()));
}
}
//System.out.println("currentDay==="+currentDay);
now.add(Calendar.DAY_OF_MONTH, -(now.get(Calendar.DATE) - 1)); //
int startIndex = now.get(Calendar.DAY_OF_WEEK) - 1; //
int year = now.get(Calendar.YEAR); //
int month = now.get(Calendar.MONTH) + 1; //
int lastDay = this.getLastDayOfMonth(year, month); //
int endIndex = startIndex + lastDay - 1; //
int startday = 1;
for (int i = 0; i < 42; i++) {
Color temp = days[i].getBackground();
if (temp.equals(display.getSystemColor(SWT.COLOR_BLUE))) {
days[i].setBackground(display.getSystemColor(SWT.COLOR_WHITE));
}
}
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_BLUE)); //
}
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 mouseDoubleClick(MouseEvent e) {
}
public void mouseDown(MouseEvent e) {
CLabel day = (CLabel) e.getSource();
if (!day.getText().equals("")) {
String str = day.getText().length()==2?day.getText():"0"+day.getText();
this.selectedDate = nowLabel.getText() + "-"+str;
// System.out.println("日期====="+this.selectedDate);
this.shell.close();
}
hasChanged = true;
}
public void mouseUp(MouseEvent e) {
}
public void open(int x, int y,String date) {
parent = getParent();
display = Display.getDefault();
shell = new Shell(parent,SWT.TITLE|SWT.RESIZE);
shell.setBounds(x, y, 230, 220);
hasChanged = false;
gridLayout = new GridLayout();
gridLayout.numColumns = 7;
shell.setLayout(gridLayout);
gridData = new GridData(GridData.FILL_HORIZONTAL);
yearUp = new Button(shell, SWT.PUSH | SWT.FLAT);
yearUp.setText("<");
gridData.widthHint = 30;
gridData.heightHint = 24;
yearUp.setLayoutData(gridData);
yearUp.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
previousYear();
}
});
gridData = new GridData(GridData.FILL_HORIZONTAL);
monthUp = new Button(shell, SWT.PUSH | SWT.FLAT);
monthUp.setText("<<");
gridData.widthHint = 30;
gridData.heightHint = 24;
monthUp.setLayoutData(gridData);
monthUp.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
previousMonth();
}
});
nowLabel = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
gridData.widthHint = 90;
gridData.heightHint = 24;
nowLabel.setLayoutData(gridData);
gridData = new GridData(GridData.FILL_HORIZONTAL);
monthNext = new Button(shell, SWT.PUSH | SWT.FLAT);
monthNext.setText(">>");
gridData.widthHint = 30;
gridData.heightHint = 24;
monthNext.setLayoutData(gridData);
monthNext.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
nextMonth();
}
});
gridData = new GridData(GridData.FILL_HORIZONTAL);
yearNext = new Button(shell, SWT.PUSH | SWT.FLAT);
yearNext.setText(">");
gridData.widthHint = 32;
gridData.heightHint = 24;
yearNext.setLayoutData(gridData);
yearNext.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
nextYear();
}
});
sunday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL);
gridData.widthHint = 20;
gridData.heightHint = 24;
sunday.setLayoutData(gridData);
sunday.setText("Su");
monday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL);
gridData.widthHint = 20;
gridData.heightHint = 24;
monday.setLayoutData(gridData);
monday.setText("M");
tuesday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL);
gridData.widthHint = 20;
gridData.heightHint = 24;
tuesday.setLayoutData(gridData);
tuesday.setText("Tu");
wednesday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL);
gridData.widthHint = 20;
gridData.heightHint = 24;
wednesday.setLayoutData(gridData);
wednesday.setText("W");
thursday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL);
gridData.widthHint = 20;
gridData.heightHint = 24;
thursday.setLayoutData(gridData);
thursday.setText("Th");
friday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL);
gridData.widthHint = 20;
gridData.heightHint = 24;
friday.setLayoutData(gridData);
friday.setText("F");
saturday = new CLabel(shell, SWT.CENTER | SWT.SHADOW_OUT);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL);
gridData.widthHint = 20;
gridData.heightHint = 24;
saturday.setLayoutData(gridData);
saturday.setText("Sa");
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));
days[i].addMouseListener(this);
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
if(DealStrNull.judgeStrNull(date)){
nowLabel.setText(formatter.format(new Date()));
Calendar now = Calendar.getInstance(); //
nowDate = new Date(now.getTimeInMillis());
setDayForDisplay(now,"");
}else{
nowLabel.setText(formatter.format(java.sql.Date.valueOf(date)));
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date2 = null;
try {
date2 = sdf.parse(date);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date2);
nowDate = new Date(calendar.getTimeInMillis());
setDayForDisplay(calendar,"");
}
shell.open();
disposed();
}
public void disposed(){
Display display = parent.getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()){
display.sleep();
}
}
}
public boolean isChanged() {
return hasChanged;
}
public String getDateText() {
return selectedDate.toString();
}
public static void main(String[] args) {
DateDialog dl = new DateDialog(new Shell());
dl.open(80, 90,"2016-10-19");
System.out.println("finish==="+dl.selectedDate);
}
}
//WT.BORDER //建立一个有边框但没有标题栏的窗口
//SWT.CLOSE //建立一个只有关闭按钮的窗口
//SWT.MIN //建立一个不能最大化的窗口
//SWT.MAX, //建立一个可以最大化最小化的窗口
//SWT.NO_TRIM //建立一个没有任何边界和标题栏的窗口
//SWT.RESIZE //建立一个可以改变大小的窗口
//SWT.TITLE //建立一个没有标题栏图标,没有关闭按钮的窗口
//SWT.ON_TOP //建立一个总是在上的窗口,注意:此属性最好与CLOSE、MIN、MAX一起使用。
//SWT.TOOL //建立一个类似工具栏的窗口
//SWT.APPLICATION_MODAL //建立一个APPLICATION模态窗口
//SWT.MODELESS //建立一个非模态窗口
//SWT.PRIMARY_MODAL //建立一个PRIMARY模态窗口
//SWT.SYSTEM_MODAL //建立一个SYSTEM模态窗口 还有两个快捷属性来建立窗口
//SHELL_TRIM //建立一个标准模式的窗口,相当于属性设置为CLOSE | TITLE | MIN | MAX | RESIZE
//DIALOG_TRIM //建立一个对话框模式的窗口,相当于属性设置为TITLE | CLOSE | BORDER
相关推荐
RCP系统使用的弹出式日期控件,可切换上月下月,前一年后一年,可选择年、月
nebula项目的日期控件,提供rcp视图内嵌功能
RCP开发,调用河北腾翔电子签章示例。适应于电子签章集成开发;适应于RCP学习。
RCP&OSGI;
RCP程序设计主要组件的文档,可以适用于初学RCP程序设计的同学。
RCP在线升级RCP在线升级RCP在线升级RCP在线升级RCP在线升级
c#常用控件的讲解
RCP导出EXE 老大难问题。 RCp 程序导出步骤,保证能够导出~
此源代码进行了针对用户控件里的文档可拖动的演示操作。
Eclipse RCP允许开发者使用eclipse结构风格设计弹性的可扩展的应用程序,可重用Eclipse中已存在的方法和编码模式。俗话说,万事开头难。Eclipse RCP入手可能会比较困难、费时。接下来我们将主要讲述如何让RCP工作. ...
clipse RCP允许开发者使用eclipse结构风格设计弹性的可扩展的应用程序,可重用Eclipse中已存在的方法和编码模式。俗话说,万事开头难。Eclipse RCP入手可能会比较困难、费时。接下来我们将主要讲述如何让RCP工作 ...
SWT 第三方 date picker RCP
有关rcp的内容 是rcp入门的好帮手,rcp自学的好帮手
RCP 常见的扩展点指南 RCP最精华的部分之一,详细分析了RCP如何建立扩展点的。
RCP项目的登录窗体RCP项目的登录窗体RCP项目的登录窗体RCP项目的登录窗体
Eclipse RCP开发教程,RCP入门教程,教你如何使用SWT Eclipse RCP开发教程,RCP入门教程,教你如何使用SWT Eclipse RCP开发教程,RCP入门教程,教你如何使用SWT Eclipse RCP开发教程,RCP入门教程,教你如何使用SWT
Eclipse RCP富客户端平台,基于Eclipse开发的。
RCP中文入门教程,RCP中文入门教程,RCP中文入门教程,RCP中文入门教程
RCP源码例子 RCP源码例子 RCP源码例子
索尼 sony rcp 1500 中文操作手册,非常好,是中文的,希望大家多多下载 谢谢谢谢,让我多赚些积分