com.toedter.calendar

Class JDateChooser

Implemented Interfaces:
ActionListener, PropertyChangeListener

public class JDateChooser
extends JPanel
implements ActionListener, PropertyChangeListener

A date chooser containig a date editor and a button, that makes a JCalendar visible for choosing a date. If no date editor is specified, a JTextFieldDateEditor is used as default.
Version:
$LastChangedRevision: 101 $, $LastChangedDate: 2006-06-04 14:42:29 +0200 (So, 04 Jun 2006) $
Author:
Kai Toedter

Field Summary

protected JButton
calendarButton
protected IDateEditor
dateEditor
protected boolean
dateSelected
protected boolean
isInitialized
protected JCalendar
jcalendar
protected Date
lastSelectedDate
protected JPopupMenu
popup

Constructor Summary

JDateChooser()
Creates a new JDateChooser.
JDateChooser(Date date)
Creates a new JDateChooser.
JDateChooser(Date date, String dateFormatString)
Creates a new JDateChooser.
JDateChooser(Date date, String dateFormatString, IDateEditor dateEditor)
Creates a new JDateChooser.
JDateChooser(String datePattern, String maskPattern, char placeholder)
Creates a new JDateChooser.
JDateChooser(IDateEditor dateEditor)
Creates a new JDateChooser with given IDateEditor.
JDateChooser(JCalendar jcal, Date date, String dateFormatString, IDateEditor dateEditor)
Creates a new JDateChooser.

Method Summary

void
actionPerformed(ActionEvent e)
Called when the jalendar button was pressed.
void
cleanup()
Should only be invoked if the JDateChooser is not used anymore.
Calendar
getCalendar()
Returns the calendar.
JButton
getCalendarButton()
Returns the calendar button.
Date
getDate()
Returns the date.
IDateEditor
getDateEditor()
Returns the date editor.
String
getDateFormatString()
Gets the date format string.
JCalendar
getJCalendar()
Returns the JCalendar component.
Date
getMaxSelectableDate()
Gets the maximum selectable date.
Date
getMinSelectableDate()
Gets the minimum selectable date.
boolean
isEnabled()
Returns true, if enabled.
static void
main(String[] s)
Creates a JFrame with a JDateChooser inside and can be used for testing.
void
propertyChange(PropertyChangeEvent evt)
Listens for a "date" property change or a "day" property change event from the JCalendar.
void
setCalendar(Calendar calendar)
Sets the calendar.
void
setDate(Date date)
Sets the date.
void
setDateFormatString(String dfString)
Sets the date format string.
void
setEnabled(boolean enabled)
Enable or disable the JDateChooser.
void
setFont(Font font)
Sets the font of all subcomponents.
void
setIcon(ImageIcon icon)
Sets the icon of the buuton.
void
setLocale(Locale l)
Sets the locale.
void
setMaxSelectableDate(Date max)
void
setMinSelectableDate(Date min)
void
setSelectableDateRange(Date min, Date max)
Sets a valid date range for selectable dates.
void
updateUI()
Updates the UI of itself and the popup.

Field Details

calendarButton

protected JButton calendarButton

dateEditor

protected IDateEditor dateEditor

dateSelected

protected boolean dateSelected

isInitialized

protected boolean isInitialized

jcalendar

protected JCalendar jcalendar

lastSelectedDate

protected Date lastSelectedDate

popup

protected JPopupMenu popup

Constructor Details

JDateChooser

public JDateChooser()
Creates a new JDateChooser. By default, no date is set and the textfield is empty.

JDateChooser

public JDateChooser(Date date)
Creates a new JDateChooser.
Parameters:
date - the date or null

JDateChooser

public JDateChooser(Date date,
                    String dateFormatString)
Creates a new JDateChooser.
Parameters:
date - the date or null
dateFormatString - the date format string or null (then MEDIUM SimpleDateFormat format is used)

JDateChooser

public JDateChooser(Date date,
                    String dateFormatString,
                    IDateEditor dateEditor)
Creates a new JDateChooser.
Parameters:
date - the date or null
dateFormatString - the date format string or null (then MEDIUM SimpleDateFormat format is used)
dateEditor - the dateEditor to be used used to display the date. if null, a JTextFieldDateEditor is used.

JDateChooser

public JDateChooser(String datePattern,
                    String maskPattern,
                    char placeholder)
Creates a new JDateChooser. If the JDateChooser is created with this constructor, the mask will be always visible in the date editor. Please note that the date pattern and the mask will not be changed if the locale of the JDateChooser is changed.
Parameters:
datePattern - the date pattern, e.g. "MM/dd/yy"
maskPattern - the mask pattern, e.g. "##/##/##"
placeholder - the placeholer charachter, e.g. '_'

JDateChooser

public JDateChooser(IDateEditor dateEditor)
Creates a new JDateChooser with given IDateEditor.
Parameters:
dateEditor - the dateEditor to be used used to display the date. if null, a JTextFieldDateEditor is used.

JDateChooser

public JDateChooser(JCalendar jcal,
                    Date date,
                    String dateFormatString,
                    IDateEditor dateEditor)
Creates a new JDateChooser.
Parameters:
jcal - the JCalendar to be used
date - the date or null
dateFormatString - the date format string or null (then MEDIUM Date format is used)
dateEditor - the dateEditor to be used used to display the date. if null, a JTextFieldDateEditor is used.

Method Details

actionPerformed

public void actionPerformed(ActionEvent e)
Called when the jalendar button was pressed.
Parameters:
e - the action event

cleanup

public void cleanup()
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.

getCalendar

public Calendar getCalendar()
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.
Returns:
the current calendar

getCalendarButton

public JButton getCalendarButton()
Returns the calendar button.
Returns:
the calendar button

getDate

public Date getDate()
Returns the date. If the JDateChooser is started with a null date and no date was set by the user, null is returned.
Returns:
the current date

getDateEditor

public IDateEditor getDateEditor()
Returns the date editor.
Returns:
the date editor

getDateFormatString

public String getDateFormatString()
Gets the date format string.
Returns:
Returns the dateFormatString.

getJCalendar

public JCalendar getJCalendar()
Returns the JCalendar component. THis is usefull if you want to set some properties.
Returns:
the JCalendar

getMaxSelectableDate

public Date getMaxSelectableDate()
Gets the maximum selectable date.
Returns:
the maximum selectable date

getMinSelectableDate

public Date getMinSelectableDate()
Gets the minimum selectable date.
Returns:
the minimum selectable date

isEnabled

public boolean isEnabled()
Returns true, if enabled.
Returns:
true, if enabled.

main

public static void main(String[] s)
Creates a JFrame with a JDateChooser inside and can be used for testing.
Parameters:
s - The command line arguments

propertyChange

public void propertyChange(PropertyChangeEvent evt)
Listens for a "date" property change or a "day" property change event from the JCalendar. Updates the date editor and closes the popup.
Parameters:
evt - the event

setCalendar

public void setCalendar(Calendar calendar)
Sets the calendar. Value null will set the null date on the date editor.
Parameters:
calendar - the calendar.

setDate

public void setDate(Date date)
Sets the date. Fires the property change "date" if date != null.
Parameters:
date - the new date.

setDateFormatString

public void setDateFormatString(String dfString)
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.
Parameters:
dfString - The dateFormatString to set.

setEnabled

public void setEnabled(boolean enabled)
Enable or disable the JDateChooser.
Parameters:
enabled - the new enabled value

setFont

public void setFont(Font font)
Sets the font of all subcomponents.
Parameters:
font - the new font

setIcon

public void setIcon(ImageIcon icon)
Sets the icon of the buuton.
Parameters:
icon - The new icon

setLocale

public void setLocale(Locale l)
Sets the locale.
Parameters:
l - The new locale value

setMaxSelectableDate

public void setMaxSelectableDate(Date max)

setMinSelectableDate

public void setMinSelectableDate(Date min)

setSelectableDateRange

public void setSelectableDateRange(Date min,
                                   Date max)
Sets a valid date range for selectable dates. If max is before min, the default range with no limitation is set.
Parameters:
min - the minimum selectable date or null (then the minimum date is set to 01\01\0001)
max - the maximum selectable date or null (then the maximum date is set to 01\01\9999)

updateUI

public void updateUI()
Updates the UI of itself and the popup.

Copyright B) 1999 - 2006 Kai Toedter.