I have two date picker components and a JComboBox component with their associated Action Listeners. My problem is this: When I select a date from any of the date pickers, not their associated events are fired but also the event for JComboBox is fired. To add to the surprise, the event for JComboBox is fired first!
On the other hand, when I click on the JComboBox component and select a value, only the JComboBox event is fired and date picker events are not fired, which is just fine. But what is the reason of the situation described in the above paragraph?

To clarify it further, here’s the sample output from console. First I click on the first date picker and select a date:
index: -1 null
Combo Box is involved. Surprised?
This is only fired when datePicker1 is involved.
(Yes, I’m surprised!) Then I click on the second date picker and select a date:
index: -1 null
Combo Box is involved. Surprised?
This is only fired when datePicker2 is involved.
(Yes, I’m surprised again!) And finally I click on the combo box and select an item:
index: 1 Last 6 months
Combo Box is involved. Surprised?
No, I’m not surprised by looking at the output above.
Any ideas about why this strange situation happens?
The complete source code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import net.sourceforge.jdatepicker.JDateComponentFactory;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilCalendarModel;
import org.jdesktop.swingx.JXDatePicker;
public class DatePickerDemo {
private static boolean isDateRangeConsistent(UtilCalendarModel m1, UtilCalendarModel m2) {
return m1.getValue().compareTo(m2.getValue()) <= 0 ? true : false;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("DatePickerDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label1 = new JLabel();
label1.setText("Choose Range (Start - End): ");
String[] fixedRanges = {"Last 3 months", "Last 6 months", "Last 12 months"};
final JComboBox<String> fixedRangesComboBox = new JComboBox<String>(fixedRanges);
fixedRangesComboBox.setSelectedIndex(-1);
final JDatePickerImpl datePicker1 = (JDatePickerImpl) JDateComponentFactory.createJDatePicker();
datePicker1.getModel().setSelected(true);
final JDatePickerImpl datePicker2 = (JDatePickerImpl) JDateComponentFactory.createJDatePicker();
datePicker2.getModel().setSelected(true);
datePicker1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Start date cannot be after end date!
if ( ! isDateRangeConsistent((UtilCalendarModel) datePicker1.getModel(),
(UtilCalendarModel) datePicker2.getModel())) {
((UtilCalendarModel) datePicker1.getModel())
.setValue(((UtilCalendarModel) datePicker2.getModel()).getValue());
}
//if the date range is changed by date picker, the fixed combo box becomes irrelevant
fixedRangesComboBox.setSelectedIndex(-1);
int rangeDaysStart = datePicker1.getModel().getDay();
int rangeMonthsStart = 1 + datePicker1.getModel().getMonth();
int rangeYearsStart = datePicker1.getModel().getYear();
label1.setText("Choose Range (Start - End): " + rangeDaysStart
+ "/" + rangeMonthsStart + "/" + rangeYearsStart);
System.out.println("This is only fired when datePicker1 is involved.");
}
});
datePicker2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// End date cannot be before start date!
if ( ! isDateRangeConsistent((UtilCalendarModel) datePicker1.getModel(),
(UtilCalendarModel) datePicker2.getModel())) {
((UtilCalendarModel) datePicker2.getModel())
.setValue(((UtilCalendarModel) datePicker1.getModel()).getValue());
}
//if the date range is changed by date picker, the fixed combo box becomes irrelevant
fixedRangesComboBox.setSelectedIndex(-1);
int rangeDaysStart = datePicker2.getModel().getDay();
int rangeMonthsStart = 1 + datePicker2.getModel().getMonth();
int rangeYearsStart = datePicker2.getModel().getYear();
label1.setText("Choose Range (Start - End): " + rangeDaysStart
+ "/" + rangeMonthsStart + "/" + rangeYearsStart);
System.out.println("This is only fired when datePicker2 is involved.");
}
});
fixedRangesComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("index: " + fixedRangesComboBox.getSelectedIndex() + " "
+ fixedRangesComboBox.getSelectedItem());
System.out.println("Combo Box is involved. Surprised?");
}
});
frame.getContentPane().add(label1, BorderLayout.NORTH);
frame.getContentPane().add(datePicker1, BorderLayout.CENTER);
frame.getContentPane().add(datePicker2, BorderLayout.EAST);
frame.getContentPane().add(fixedRangesComboBox, BorderLayout.PAGE_END);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
You should remove
fixedRangesComboBox.setSelectedIndex(-1);from the twoActionListeneranonymous classes 🙂