问题
I am using a JDateChooser as a component in a JForm. I have setup a JButton, which resets all of the fields on this form to blank. I do not have issues with the actionListener, or the method for clearing, but instead am unsure how to clear the JDateChooser component.
More specifically I am wondering how to clear the Date from the JTextField component of the date chooser, such that it is blank.
Furthermore, I would like to set this field to the current date after it is cleared.
回答1:
If you want to clear the JDateChooser field:
JDateChooser dateChooser = new JDateChooser();
dateChooser.setCalendar(null);
This code will set the field to null
.
To set the date to today you can use for example:
dateChooser.setDate(new Date())
回答2:
If dateChooser is your instanceof JDateChooser:
dateChooser.setDate(new Date());
Edit: I'm not sure what you meen by "set todays date". I assumed you meant set the date in the JDateChooser to todays date (that's what my code does). If it is not what you want, please clarify your question.
回答3:
((JTextField)dateChooser.getDateEditor().getUiComponent()).setText("");
dateChooser.setDate(new Date());
来源:https://stackoverflow.com/questions/11719917/how-to-clear-jdatechooser-field