7.1. Questions | |
Is it possible to auto fill missing fields with default values when editing a date? | |
Yes, it is possible. When a date is edited but not all its fields contain a value, then the edited date is rejected because it is not valid. If a date edit component (JDatePicker or JDateField) uses the date format "MM/dd/yyyy HH:mm" and the hour and minute fields are not filled then the date will be rejected. To fix this, you should override the commitEdit() method and fill those fields with some default values before the date is validated. The following example shows how to set a default value of zero for the hour and minute fields. JDatePicker editComponent = new JDatePicker() {
public void commitEdit() {
if (!dateModel.isSet(Calendar.HOUR)) {
dateModel.set(Calendar.HOUR, 0);
}
if (!dateModel.isSet(Calendar.MINUTE)) {
dateModel.set(Calendar.MINUTE, 0);
}
super.commitEdit();
}
}; The dateModel field is a protected field in JDateEditComponent that gives you access to the DateModel that keeps information about the edited date. All you have to do is to use the set(int, int) method to specify valid values for the missing fields. | |
How can JDatePicker be used with String dates? | |
JDatePicker only handles java.util.Date objects because this is the standard format for dates in Java. So, if you either want to set or to get a date from JDatePicker, you must deal with a java.util.Date object. But real-world situations show that dates can be stored in various formats, most frequently as String objects. This means that you cannot use such dates with JDatePicker directly. Let's take a small example to see how this problem can be solved. Let's suppose that you have a database with dates represented as Strings of the form yyyy-MM-dd. You want to load these dates into your application and after editing, you want to save them back to your database. The first step is to load the String date in JDatePicker for editing: String stringDate = "2002-02-22"; Date date = convertStringToDate(stringDate, "yyyy-MM-dd"); datePicker.setSelectedDate(date); The final step is to get the date from JDatePicker to save it: Date date = datePicker.getSelectedDate(); String stringDate = convertDateToString(date, "yyyy-MM-dd"); As you can see, we use two conversion methods to help us in the process. A possible implementation for these methods is: private Date convertStringToDate(String stringDate, String pattern) {
Date date = null;
if (stringDate != null) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
date = sdf.parse(stringDate);
} catch (ParseException e) {
}
}
return date;
}private String convertDateToString(Date date, String pattern) {
String stringDate = null;
if (date != null) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
stringDate = sdf.format(date);
}
return stringDate;
} | |
How can JDatePicker be used with java.sql.Date dates? | |
Please see the above section to understand why JDatePicker handles java.util.Date dates only. The following code shows how to use sql dates with JDatePicker. The first step is to load the java.sql.Date date in JDatePicker for editing: java.sql.Date sqlDate = myDate; java.util.Date date = convertSqlDateToUtilDate(sqlDate); datePicker.setSelectedDate(date); The final step is to get the date from JDatePicker: java.util.Date date = datePicker.getSelectedDate(); java.sql.Date sqlDate = convertUtilDateToSqlDate(date); As you can see, we use two conversion methods to help us in the process. A possible implementation for these methods is: private java.util.Date convertSqlDateToUtilDate(java.sql.Date sqlDate) {
java.util.Date date = null;
if (sqlDate != null) {
date = new java.util.Date(sqlDate.getTime());
}
return date;
}private java.sql.Date convertUtilDateToSqlDate(java.util.Date date) {
java.sql.Date sqlDate = null;
if (date != null) {
sqlDate = new java.sql.Date(date.getTime());
}
return sqlDate;
} | |
Is there a way to access the editing part of JDatePicker or JDateField? | |
Both JDatePicker and JDateField use a JTextField object to edit the dates. But this text field is not accessible through the API in a meaningful way. If you want to register a focus listener or do some text operations, then you could use the following code to access the text field: //for a JDateField JTextField textField = (JTextField)dateField.getComponent(0); //for a JDatePicker JComboBox comboBox = (JComboBox)datePicker.getComponent(0); JTextField textField = (JTextField)comboBox.getEditor().getEditorComponent(); | |