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弹出日期控件是这种环境中用于用户界面交互的一个重要组件,它提供了一种方便的方式来选择日期,增强了用户体验。 在RCP系统中,弹出式日期控件通常是为了简化用户在日期输入时的操作。这种控件不仅允许用户通过...
nebula项目的日期控件,提供rcp视图内嵌功能
在SWT中,虽然内建了一些基本的控件,但并没有内置日期选择器(date picker)组件。因此,为了在SWT应用中实现日期选择功能,通常会采用第三方库或插件来扩展这一功能,如"SWT 第三方 date picker RCP"。 这个描述...
3. **控件**:Xpages提供了丰富的控件库,包括输入控件(如文本框、日期选择器)、显示控件(如表格、列表视图)、导航控件(如按钮、链接)等。通过控件,用户能够与应用进行交互。 4. **事件处理**:Xpages支持...
它支持单元格级别的编辑,包括文本、日期、数字等类型的编辑器,并且可以自定义编辑器类型以适应各种需求。 5. **性能优化** 由于GridViewer是针对大量数据设计的,它有良好的性能优化。例如,它可以实现虚拟化...
- RCP(Rich Client Platform):Eclipse的RCP框架可以构建复杂的桌面应用,如果需要更强大的功能,可以考虑使用RCP来构建图书管理系统。 综上所述,使用SWT开发图书管理系统涉及到了GUI设计、数据库操作、事件...
SWT 是一个用于Java的本机GUI库,它为开发者提供了与操作系统直接交互的能力,如窗口、按钮、文本框等基本控件。SWT的设计目标是提供高效、原生的外观和感觉,这意味着在Windows、Linux或Mac OS X等不同平台上运行时...
它可以处理资源的本地化,包括字符串、日期格式、货币符号等,使得应用程序能够适应全球不同地区的用户需求。 6. **GUI控件**: GWT提供了一系列预定义的用户界面控件,如按钮、文本框、面板、对话框等,方便...
它们可以自定义数据的显示格式,如日期、货币等。 5. **编辑支持** 如果需要使表格单元格可编辑,可以实现`ITableEditorProvider`并添加到`TableViewer`。`CellEditor`类提供了多种类型的编辑器,如文本、组合框等...
3. **对话框和视图**:JFace包含了一组预定义的对话框和视图,如颜色选择器、日期选择器等,可以快速创建常见的UI元素。 4. **表单构建**:JFace Form Builder允许开发者以声明式的方式创建表单,减少手动编写布局...
1. **创建TableViewer实例**:首先需要创建TableViewer实例,并将其绑定到SWT.Table控件上。 2. **设置内容器**:定义一个内容器,该内容器负责管理TableViewer的数据模型。 3. **设置标签器**:通过标签器来指定每...
2. 参数可以是输入框、下拉列表、日期选择器等不同控件,以方便用户交互。 3. 参数关联:一个参数的值可能影响其他参数的可用性或默认值,实现更灵活的交互逻辑。 4. 参数表达式:可以使用EL表达式或其他脚本语言,...
SWT 是一个用于构建本地 GUI 应用的 Java 库,它提供了更接近操作系统原生控件的用户体验。 5. **H2 内存数据库**:在开发和测试阶段,H2 作为一个轻量级的内存数据库被使用。H2 提供快速的数据存储和检索,对于...
- 功能:SWT提供了更多原生控件,如进度条、日期选择器等,而AWT和Swing可能需要额外的库来实现这些功能。 **学习和使用SWT**: 要有效利用SWT,开发者需要熟悉Java编程基础,理解面向对象的概念,以及掌握如何阅读...