I am using toedter JDateChooser, and I am having problems retrieving the date picked from it.
jDateChooser2.setDateFormatString("dd-MMMM-yy");
jDateChooser2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jDateChooser2MouseClicked(evt);
}
});
private void jDateChooser2MouseClicked(java.awt.event.MouseEvent evt) {
Date dateFromDateChooser = jDateChooser2.getDate();
System.out.println(dateFromDateChooser);
}
How can I retrieve the date? Is there a better way to do it? I think the listener is not being fired or triggered. i tried replacing the listener with:
System.out.println("triggered");
Still there are no output.
Basically, you don't want to listener for MouseEvents, as these could be changing the state of the component in a number of ways, most of which you don't want to know about.
You should be monitoring the date property change event, for example...
JDateChooser dateChooser = new JDateChooser();
dateChooser.addPropertyChangeListener("date", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
Date date = (Date)evt.getNewValue();
System.out.println("Date changed " + date);
}
});
Just beware, this could be triggered in response to calling setDate or by the user selecting a date from the picker, generally, you won't be able to tell
Related
How i would add an action listener to the jcalendar? I want to get the date whenever you click in a day, so i will show the entire date on a jtextfield.
I have tried something like this, but It does nothing when i click a day.
cal = new JCalendar();
cal.setWeekOfYearVisible(false);
cal.getDayChooser().getDayPanel().addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()
+ ": " + (Date) e.getNewValue());
}
});
I tried the code of the answer to this question too, that is similar, but nothing.
Adding actionListener to jCalendar
I think you are adding the property change listener to the wrong bean. I inspected the JCalendar code and the getDayPanel() method returns just a regular JPanel that I don't think knows about the "day" property you are interested in.
/**
* Returns the day panel.
*
* #return the day panel
*/
public JPanel getDayPanel() {
return dayPanel;
}
I think you should add your property change listener to the daychooser itself, which is the class that knows about the "day" property. Also, you might want to register for the "day" property of the day chooser:
cal = new JCalendar();
cal.setWeekOfYearVisible(false);
cal.getDayChooser().addPropertyChangeListener("day", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()
+ ": " + e.getNewValue());
}
});
Still, that will only give you the day that the user picked, not the entire date.
Why does the code enters twice in the change event of the JSpinner?
private javax.swing.JSpinner spinner = new javax.swing.JSpinner()
spinner.setModel(new javax.swing.SpinnerDateModel());
spinner.addChangeListener(new javax.swing.event.ChangeListener() {
#Override
public void stateChanged(javax.swing.event.ChangeEvent evt) {
System.out.println("Just a test");
}
});
The code above shows the message twice when u click only one time.
2 events are generated: one for the value being deselected and another for the new value being selected in the component. As #camickr notes in his comment this behavior occurs in SpinnerDateModel but not in the default SpinnerNumberModel
As a workaround you could use
spinner.addChangeListener(new ChangeListener() {
Object lastValue;
#Override
public void stateChanged(ChangeEvent evt) {
if (lastValue != null && !spinner.getValue().equals(lastValue)) {
// expensive code calls here!
}
lastValue = spinner.getValue();
}
});
This wont prevent the listener being called twice but will prevent any expensive code being invoked unnecessarily
Just bumped into the same problem and found a different workaround, as the one in https://stackoverflow.com/a/19166589/5326620 caused it to miss the event when first editing the date directly on the text field.
In my case, I'm using a SpinnerDateModel for Calendar.DAY_OF_MONTH (same as Calendar.DATE).
If the SpinnerDateModel is initialized with a value precisely at midnight, the event is no longer fired twice.
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
Date value = now.getTime();
JSpinner dateSpn = new JSpinner(new SpinnerDateModel(value, null, null, Calendar.DAY_OF_MONTH));
This is probably because the commitEdit of the JFormattedTextField tests the old and new value by equality, and Date equality is on the millisecond.
How to receive a selected date using method actionPerformed?
Do you know other useful methods?
I cannot find proper method to receive selected date.
_jCalendarButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
Object[] tab = _jCalendarButton.getSelectedObjects();
}
});
The project is documented here: http://www.jbundle.org/jbundle/jcalendarbutton/quickstart.html. You must add a PropertyChangeListener on the button to be notified when the date changes. The ActionListener is only called when you press the button. Given that the project has no published javadoc, look at the source code for more information.
You will need to use a PropertyChangeListener rather than an ActionListener and read the new value from its PropertyChangeEvent
jCalendarButton.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() instanceof Date) {
System.out.println(evt.getNewValue());
}
}
});
If you want to select an date with GUI of Calendar you can use JXDatePicker (Component of SwingX). You will need an SwingX jar file to use it.
Use the following code:
JXDatePicker datePicker = new JXDatePicker(System.currentTimeMillis());
datePicker.addActionListener(new ActionListener(){
label.setText(datePicker.getDate().toString());
});
Using JCalendar, I have entered the following code from this site:
private void jCalendar2PropertyChange(java.beans.PropertyChangeEvent evt)
{
JCalendar jc = new JCalendar();
jc.addPropertyChangeListener("calendar", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
final Calendar c = (Calendar) evt.getNewValue();
System.out.println(c.get(Calendar.DAY_OF_MONTH));
}
})
but I am not getting a result in the output screen? Also the build is taking 23 seconds is this normal?
thank you for your help. Also do I need to create the mouse event?
How would I add an actionListener to the jDayChooser component of an existing jCalendar placed using netbeans?
I would like to only trigger an event only when the day buttons are clicked. as the propertyChange in jCalendar listens to even the jMonthChooser and jYearChooser
P.S. using toedter's jCalendar
Alternatively, you can listen for the specific propertyName, "day".
JDayChooser jdc = new JDayChooser();
jdc.addPropertyChangeListener("day", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()+ ": " + e.getNewValue());
}
});
Addendum: How do I get it to work on a JCalendar?
Similarly, the propertyName, "calendar" represents a Calendar from which you can get() the DAY_OF_MONTH.
JCalendar jc = new JCalendar();
jc.addPropertyChangeListener("calendar", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
final Calendar c = (Calendar) e.getNewValue();
System.out.println(c.get(Calendar.DAY_OF_MONTH));
}
});
In case somebody misses reading the comments. Here's a sample working code.
JCalendar jCalendar = new JCalendar();
jCalendar.getDayChooser().addPropertyChangeListener("day", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
System.out.println(e.getPropertyName()+ ": " + e.getNewValue());
}
});