📄 jdatechooser.java
字号:
calendar.setTime(date);
}
jcalendar.setCalendar(calendar);
popup.show(calendarButton, x, y);
dateSelected = false;
}
/**
* Listens for a "date" property change or a "day" property change event
* from the JCalendar. Updates the date editor and closes the popup.
*
* @param evt
* the event
*/
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("day")) {
if (popup.isVisible()) {
dateSelected = true;
popup.setVisible(false);
setDate(jcalendar.getCalendar().getTime());
}
} else if (evt.getPropertyName().equals("date")) {
if (evt.getSource() == dateEditor) {
firePropertyChange("date", evt.getOldValue(), evt.getNewValue());
} else {
setDate((Date) evt.getNewValue());
}
}
}
/**
* Updates the UI of itself and the popup.
*/
public void updateUI() {
super.updateUI();
setEnabled(isEnabled());
if (jcalendar != null) {
SwingUtilities.updateComponentTreeUI(popup);
}
}
/**
* Sets the locale.
*
* @param l
* The new locale value
*/
public void setLocale(Locale l) {
super.setLocale(l);
dateEditor.setLocale(l);
jcalendar.setLocale(l);
}
/**
* Gets the date format string.
*
* @return Returns the dateFormatString.
*/
public String getDateFormatString() {
return dateEditor.getDateFormatString();
}
/**
* Sets the date format string. E.g "MMMMM d, yyyy" will result in "July 21,
* 2004" if this is the selected date and locale is English.
*
* @param dfString
* The dateFormatString to set.
*/
public void setDateFormatString(String dfString) {
dateEditor.setDateFormatString(dfString);
invalidate();
}
/**
* Returns the date. If the JDateChooser is started with a null date and no
* date was set by the user, null is returned.
*
* @return the current date
*/
public Date getDate() {
return dateEditor.getDate();
}
/**
* Sets the date. Fires the property change "date" if date != null.
*
* @param date
* the new date.
*/
public void setDate(Date date) {
dateEditor.setDate(date);
if (getParent() != null) {
getParent().invalidate();
}
}
/**
* Returns the calendar. If the JDateChooser is started with a null date (or
* null calendar) and no date was set by the user, null is returned.
*
* @return the current calendar
*/
public Calendar getCalendar() {
Date date = getDate();
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
/**
* Sets the calendar. Value null will set the null date on the date editor.
*
* @param calendar
* the calendar.
*/
public void setCalendar(Calendar calendar) {
if (calendar == null) {
dateEditor.setDate(null);
} else {
dateEditor.setDate(calendar.getTime());
}
}
/**
* Enable or disable the JDateChooser.
*
* @param enabled
* the new enabled value
*/
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if (dateEditor != null) {
dateEditor.setEnabled(enabled);
calendarButton.setEnabled(enabled);
}
}
/**
* Returns true, if enabled.
*
* @return true, if enabled.
*/
public boolean isEnabled() {
return super.isEnabled();
}
/**
* Sets the icon of the buuton.
*
* @param icon
* The new icon
*/
public void setIcon(ImageIcon icon) {
calendarButton.setIcon(icon);
}
/**
* Sets the font of all subcomponents.
*
* @param font
* the new font
*/
public void setFont(Font font) {
if (isInitialized) {
dateEditor.getUiComponent().setFont(font);
jcalendar.setFont(font);
}
super.setFont(font);
}
/**
* Returns the JCalendar component. THis is usefull if you want to set some
* properties.
*
* @return the JCalendar
*/
public JCalendar getJCalendar() {
return jcalendar;
}
/**
* Returns the calendar button.
*
* @return the calendar button
*/
public JButton getCalendarButton() {
return calendarButton;
}
/**
* Returns the date editor.
*
* @return the date editor
*/
public IDateEditor getDateEditor() {
return dateEditor;
}
/**
* Sets a valid date range for selectable dates. If max is before min, the
* default range with no limitation is set.
*
* @param min
* the minimum selectable date or null (then the minimum date is
* set to 01\01\0001)
* @param max
* the maximum selectable date or null (then the maximum date is
* set to 01\01\9999)
*/
public void setSelectableDateRange(Date min, Date max) {
jcalendar.setSelectableDateRange(min, max);
dateEditor.setSelectableDateRange(jcalendar.getMinSelectableDate(),
jcalendar.getMaxSelectableDate());
}
public void setMaxSelectableDate(Date max) {
jcalendar.setMaxSelectableDate(max);
dateEditor.setMaxSelectableDate(max);
}
public void setMinSelectableDate(Date min) {
jcalendar.setMinSelectableDate(min);
dateEditor.setMinSelectableDate(min);
}
/**
* Gets the maximum selectable date.
*
* @return the maximum selectable date
*/
public Date getMaxSelectableDate() {
return jcalendar.getMaxSelectableDate();
}
/**
* Gets the minimum selectable date.
*
* @return the minimum selectable date
*/
public Date getMinSelectableDate() {
return jcalendar.getMinSelectableDate();
}
/**
* Should only be invoked if the JDateChooser is not used anymore. Due to popup
* handling it had to register a change listener to the default menu
* selection manager which will be unregistered here. Use this method to
* cleanup possible memory leaks.
*/
public void cleanup() {
MenuSelectionManager.defaultManager().removeChangeListener(changeListener);
changeListener = null;
}
/**
* Creates a JFrame with a JDateChooser inside and can be used for testing.
*
* @param s
* The command line arguments
*/
public static void main(String[] s) {
JFrame frame = new JFrame("JDateChooser");
JDateChooser dateChooser = new JDateChooser();
// JDateChooser dateChooser = new JDateChooser(null, new Date(), null,
// null);
// dateChooser.setLocale(new Locale("de"));
// dateChooser.setDateFormatString("dd. MMMM yyyy");
// dateChooser.setPreferredSize(new Dimension(130, 20));
// dateChooser.setFont(new Font("Verdana", Font.PLAIN, 10));
// dateChooser.setDateFormatString("yyyy-MM-dd HH:mm");
// URL iconURL = dateChooser.getClass().getResource(
// "/com/toedter/calendar/images/JMonthChooserColor32.gif");
// ImageIcon icon = new ImageIcon(iconURL);
// dateChooser.setIcon(icon);
frame.getContentPane().add(dateChooser);
frame.pack();
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -