My final refinement (for the moment) is that, I want today’s date to be the default date. To do this I need to import two libararies:
import java.text.SimpleDateFormat; import java.util.Date;
I then need to get the current date:
Date today = new Date();
In order to get each part of the date (day, month, year), i will simply use the SimpleDateFormat library, and create three formatters:
SimpleDateFormat currentDay = new SimpleDateFormat("dd"); SimpleDateFormat currentMonth = new SimpleDateFormat("MM"); SimpleDateFormat currentYear = new SimpleDateFormat("yyyy");
Setting each value is simple, in the original code I set the default year to “0000”. To use change it to the current year I simply change:
private JTextField textFieldYear = new JTextField("0000");
to
private JTextField textFieldYear = new JTextField(currentYear.format(today), 4);
Similarly I change the default values for the combo boxes from:
comboBoxDay.setSelectedIndex(0); comboBoxMonth.setSelectedIndex(0);
to
comboBoxDay.setSelectedIndex(Integer.parseInt(currentDay.format(today))); comboBoxMonth.setSelectedIndex(Integer.parseInt(currentMonth.format(today)));
Now i could stop here but i need to also change the default year in the focusLost method:
public void focusLost(FocusEvent e) { if (textFieldYear.getText().equals("")){ textFieldYear.setText(currentYear.format(today)); } }
At this point my Zeller program is complete.