3
A
回答
3
SWT DateTime控件根本不支持这一点。
我建议CalendarCombo来自Eclipse Nebula项目。
1
你将不得不手动设置实例的字段为0或空什么是适当的。你也可以实现你自己的NoDateTime对象(使用null对象模式)来完成同样的事情。虽然我会试图表示没有时间,但是有没有理由不能这样做?
3
如果这仍然对任何人都有用 - 我遇到同样的问题,这意味着UI上的字段必须显示日期或空值:因为没有选择的日期也是有效的输入。 虽然SWT DateTime必须显示某种日期,但通过简单地制作标签和按钮来引入另一级别的间接方式并不是问题 - 它看起来像DateTime:Then按钮在单独的模式窗口中调用DateTime。在用户做出选择之后,我们将结果写回到应用程序窗口中的标签。您还可以在模态窗口中添加另一个按钮并将其称为没有。如果用户单击NONE,则清除应用程序中的标签字段。 您将看到我首先从标签中删除当前日期值,以便可以在模式对话框中初始化DateTime控件。这样它就像一个新的复合控件一样,尽管我承认如果你需要多次复制它会有点尴尬。例如:
private Button buttonDeadlineDate;
private Label labelDeadlineDate;
// ... then define your "composite" control:
lblNewLabel_5 = new Label(group_2, SWT.NONE);
lblNewLabel_5.setBounds(10, 14, 50, 17);
lblNewLabel_5.setText("Deadline:");
// We make our own composite date control out of a label and a button
// and we call a modal dialog box with the SWT DateTime and
// some buttons.
labelDeadlineDate = new Label(group_2, SWT.BORDER | SWT.CENTER);
labelDeadlineDate.setBounds(62, 10, 76, 20);
// Note that I use the strange font DokChampa because this was the only way to get a margin at the top.
labelDeadlineDate.setFont(SWTResourceManager.getFont("DokChampa", 8, SWT.NORMAL));
labelDeadlineDate.setBackground(SWTResourceManager.getColor(255, 255, 255)); // so it does appear editable
buttonDeadlineDate = new Button (group_2, SWT.NONE);
buttonDeadlineDate.setBounds(136, 11, 20, 20); // x - add 74, y - add 1 with respect to label
// ... And later we have the call-back from the listener on the little button above:
//========================================
// Deadline Date
//========================================
buttonDeadlineDate.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// Define the dialog shell.
// Note: DIALOG_TRIM = TITLE | CLOSE | BORDER (a typical application dialog shell)
final Shell dialog = new Shell (shlTaskScheduler, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
dialog.setText("Enter deadline date (NONE for none)");
//========================================
// Position and size the dialog (relative to the application).
// could have probably also used a single call to dialog.setBounds()
// instead of calling setLocation() and setSize().
//========================================
Point myPoint = new Point(0,0);
myPoint = shlTaskScheduler.getLocation();
myPoint.x +=80; // myPoint.x +=30;
myPoint.y +=320; // myPoint.y +=350;
dialog.setLocation(myPoint);
dialog.setSize(270, 220);
dialog.setLayout (null);
//========================================
// Define dialog contents
//========================================
// Make controls final they it can be accessed from the listener.
final DateTime DTDeadlineDate;
DTDeadlineDate = new DateTime(dialog, SWT.BORDER | SWT.CALENDAR | SWT.DROP_DOWN);
DTDeadlineDate.setBounds(10, 10, 175, 175);
final Button buttonNone = new Button (dialog, SWT.PUSH);
buttonNone.setText ("NONE");
buttonNone.setBounds(200, 35, 55, 25);
final Button buttonOK = new Button (dialog, SWT.PUSH);
buttonOK.setText ("OK");
buttonOK.setBounds(200, 85, 55, 25);
//========================================
// Initialize the DateTime control to
// the date displayed on the button or today's date.
//========================================
// Get the deadline from the main application window
String newDeadlineDateString = (labelDeadlineDate.getText().toString());
myLogger.i (className, "got deadline from main application window as " + newDeadlineDateString);
// If deadline date found, use it to initialize the DateTime control
// else the DateTime control will initialize itself to the current date automatically.
if ((newDeadlineDateString.length() == 10) // probably unnecessary test
&& (isThisDateValid(newDeadlineDateString, "yyyy-MM-dd"))) {
// parse and extract components
try {
String tmpYearString= newDeadlineDateString.substring(0,4);
String tmpMoString = newDeadlineDateString.substring(5,7);
String tmpDayString = newDeadlineDateString.substring(8,10);
int tmpYearInt = Integer.parseInt(tmpYearString);
int tmpMoInt = Integer.parseInt(tmpMoString);
int tmpDayInt = Integer.parseInt(tmpDayString);
DTDeadlineDate.setYear(tmpYearInt);
DTDeadlineDate.setMonth(tmpMoInt - 1); // the control counts the months beginning with 0! - like the calendar
DTDeadlineDate.setDay(tmpDayInt);
} catch(NumberFormatException f) {
// this should not happen because we have a legal date
myScreenMessage.e(className, "Error extracting deadline date from screen <" + newDeadlineDateString + ">. Ignoring");
}
} else if (newDeadlineDateString.length() > 0) {
myLogger.w (className, "Illegal current deadline date value or format <" + newDeadlineDateString + ">. Ignoring.");
// no need to do anything, as the control will initialize itself to the current date
} else {
// no need to do anything, as the control will initialize itself to the current date
}
//========================================
// Set up the listener and assign it to the OK and None buttons.
// Note that the dialog has not been opened yet, but this seems OK.
//
// Note that we define a generic listener and then associate it with a control.
// Thus we need to check in the listener, which control we happen to be in.
// This is a valid way of doing it, as an alternative to using
// addListener() or
// addSelectionListener()
// for specific controls.
//========================================
Listener listener = new Listener() {
public void handleEvent (Event event) {
if (event.widget == buttonOK) {
int newDeadlineDay = DTDeadlineDate.getDay();
int newDeadlineMonth = DTDeadlineDate.getMonth() + 1; // the returned month will start at 0
int newDeadlineYear = DTDeadlineDate.getYear();
String selectedDeadlineDate = String.format ("%04d-%02d-%02d", newDeadlineYear, newDeadlineMonth, newDeadlineDay);
if (isThisDateValid(selectedDeadlineDate, "yyyy-MM-dd")) {
labelDeadlineDate.setText(selectedDeadlineDate);
} else {
// This is strange as the widget should only return valid dates...
myScreenMessage.e(className, "Illegal deadline date selected: resetting to empty date");
labelDeadlineDate.setText("");
}
} else if (event.widget == buttonNone) {
// an empty date is also an important value
labelDeadlineDate.setText("");
} else {
// this should not happen as there are no other buttons on the dialog
myLogger.e(className, "Unexpected widget state: ignoring");
}
// once a button is pressed, we close the dialog
dialog.close();
}
};
// Still need to assign the listener to the buttons
buttonOK.addListener (SWT.Selection, listener);
buttonNone.addListener (SWT.Selection, listener);
//========================================
// Display the date dialog.
//========================================
dialog.open();
//========================================
// If you need to do this - you can wait for user selection before returning from this listener.
// Note that this wait is not necessary so that the above button listeners
// can capture events, but rather so that we do not continue execution and end this
// function call before the user has made a date selection clicked on a button.
// Otherwise we would just go on.
while (!dialog.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
...
}
});
相关问题
- 1. 默认日期
- 2. 如何在Material-Datetime-Picker中设置自己的默认日期
- 3. WP7默认日期
- 4. 如何将默认UTC日期值分配给DateTime列?
- 5. 日期选择器与默认日期
- 6. SSRS日期参数默认
- 7. Matplotlib默认日期格式?
- 8. jQuery-UI datepicker默认日期
- 9. jquery datepicker的默认日期
- 10. Activeadmin设置默认日期
- 11. 日期的默认格式
- 12. JQuery Datepicker - 无默认日期
- 13. Jsp默认日期格式
- 14. JSP日期默认值
- 15. 默认日期值休眠
- 16. Datepicker bootstrap默认日期
- 17. jQuery Datepicker没有任何预定义的“默认”日期?
- 18. Rails i18n.default_locale和日期格式...似乎没有像默认
- 19. ArcGIS中日期的默认值
- 20. 在DataBox中设置默认日期
- 21. iOS中UIDatePicker的默认日期
- 22. 在EXCEL/VBA中设置默认日期
- 23. 有没有办法在CakePHP的日期格式输入中设置默认日期?
- 24. 如何控制UltraWebGrid控件中日期列的默认日期?
- 25. MySQL的默认DATETIME值
- 26. Sharepoint 2007 DateTime列默认值?
- 27. Sensenet DateTime字段默认值
- 28. 有没有一个选项,当您打开datebox时设置默认日期jquerymobile
- 29. 有没有办法来改变默认的日期输入格式
- 30. DateTime格式日期