how to clear JDateChooser field? [closed]

烂漫一生 提交于 2019-11-27 08:31:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!