Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7041809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:05:30+00:00 2026-05-28T02:05:30+00:00

I have two date picker components and a JComboBox component with their associated Action

  • 0

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?

JComboBox event fired even if only date pickers are clicked

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();
            }
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:05:30+00:00Added an answer on May 28, 2026 at 2:05 am

    You should remove fixedRangesComboBox.setSelectedIndex(-1); from the two ActionListener anonymous classes 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two input dates taking from Date Picker control. I have selected start
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.
I have two Ajax date time picker in my aspx picker.I want to have
So i have this picker: http://www.filamentgroup.com/lab/date_range_picker_using_jquery_ui_16_and_jquery_ui_css_framework/ Now, how can I prepend the date/daterange you
I have two inline datepickers on my page to select a date range: As
I've got two date pickers in one form. They have different id's so this
I been struggeling with this problem for two days now and have still not
I'm using the jQuery datepicker where I have two textboxes (start date / end
I have two date entities, start_date and end_date and now I want to find
I have two Zend Date objects to work with: $now = Zend_Date::now(); $sessionStart =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.