how to change combobox's data after change another combobox item selection?

13,830

Solution 1

You can change the dependent combos' models, as shown in this example.

Alternatively, consider JCalendar.

Solution 2

You can create your own combo box model for the day combo box. Based on the selected value of the year and month combo boxes, you can put your day combo box model in the correct state to display the appropriate number of days. The easiest way to extend DefaultComboBoxModel.

Edit:
Creating a custom model allows you to more easily change between options. A month can have 28, 29, 30 or 31 days. The custom class can have one method that sets how many days to display and does not require any extra work from the outside. The other option is to create the set of options within your event handler and replace the model every time a change is made. A custom class partitions the code so that the event handler just sets how many days are displayed and does not need to be concerned with how that effects the underlying model of a data.

Share:
13,830
Seyed Vahid Hashemi
Author by

Seyed Vahid Hashemi

A little bit of everything. A husband, programmer, engineer and last but not least a lifelong learner. My Linkedin

Updated on June 05, 2022

Comments

  • Seyed Vahid Hashemi
    Seyed Vahid Hashemi almost 2 years

    I have a MVC application written in java which has a form with three comboboxes in it. year / month / day and I want to change the number of days if the selection of year and month changed. in my viewer I just define the comboboxes

    createComboBoxes( mainContentPage, "combobox name");
    

    in my controller I have :

    public class ComboBoxItemListener implements ItemListener
    {
    
    private int year=0;
    private int month=0;
    private int day=0;
    
    public WeatherController c_wc;
    @Override
    public void itemStateChanged(ItemEvent event)
    {
    
    
        JComboBox comboBox = (JComboBox)event.getSource();
        if (event.getStateChange() == ItemEvent.SELECTED)
        {
                        //this area is my problem
            if(comboBox.getName() == Helper.COMBOBOX_MONTH || comboBox.getName() == Helper.COMBOBOX_YEAR)
            {
                        //definitely this line is not correct
                    c_wc.addDaysToComboBox(comboBox, year, month);
                    comboBox.setEnabled(true);
    
    
            }
            //rest is okay
            switch(comboBox.getName())
            {
                case Helper.COMBOBOX_YEAR:
                    year = Integer.parseInt(comboBox.getSelectedItem().toString().trim());
    
                    break;
    
                case Helper.COMBOBOX_MONTH:
                    KeyValue<String, Integer> selectedItem = (KeyValue<String,Integer>)event.getItem();
    
                    month = Integer.parseInt(selectedItem.getValue().toString());
                    break;
    
                case Helper.COMBOBOX_DAY:
                    day = Integer.parseInt(comboBox.getSelectedItem().toString().trim());
                    break;
    
                case Helper.COMBOBOX_AIRPORT:
                    break;
            }
            System.out.println(year + " " + month + " " + day);
        }
    }}
    

    how can I change another component after firing some other event?