我正在将 JDateChooser 用于我正在开发的 java swing 项目,在此,日期可以通过两种方式设置:由最终用户或以编程方式设置。
所以我在相应的类中定义了一个 propertychangelistener(变量 trig 被初始化为零并保持跟踪属性更改被监听的次数)。
public class WriteEntry{
private int trig=0;
private Date currentDate = new Date();
public JDateChooser dateChooser = new JDateChooser();
public CustomDate selectedDate = DateConverter.convertDate(currentDate);
private static String filename = StorageSpace.currentpath+CurrentUser.getInstance().getUserName()+"\\"+
Integer.toString(selectedDate.getYear())+"\\"
+Integer.toString(selectedDate.getMonth())+"\\"+Integer.toString(selectedDate.getDay())+".txt";
private JLabel dayinfo = new JLabel("");
private JTextArea contentfield = new JTextArea("");
private PropertyChangeListener lis = new PropertyChangeListener(){
@Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println("triggered "+trig++);
if(dateBoundary()) {
selectedDate = DateConverter.convertDate(dateChooser);
filename = StorageSpace.currentpath+CurrentUser.getInstance().getUserName()+"\\"+
Integer.toString(selectedDate.getYear())+"\\"
+Integer.toString(selectedDate.getMonth())+"\\"+Integer.toString(selectedDate.getDay())+".txt";
}
else {
updateDateChooser(selectedDate);
}
if(isAlreadyWritten())
{
try {
updateEditFields(selectedDate, "content");
} catch (IOException e1) {
e1.printStackTrace();
}
}
else
{
contentfield.setText("Start writing here");
dayinfo.setText("You are making entry for: "+ new SimpleDateFormat("dd/MM/yyyy").format(dateChooser.getDate()));
}
}
};
相关分类