3

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Vahid Hashemi
  • 5,182
  • 10
  • 58
  • 88

6 Answers6

3

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

Alternatively, consider JCalendar.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I favor composition for this, but it scales poorly, as does the usability of more than two or three combos. – trashgod Dec 30 '11 at 21:13
  • IIUC, a single `JCalendar` component may be more usable than three individual combo boxes; it also has nice property change support. – trashgod Apr 17 '12 at 18:54
1

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.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
0

You can have named comboboxes as fields like

JComboBox monthCobmo;
JComboBox daysCobmo;
JComboBox yearCobmo;

So in itemStateChanged() write following code:

if(comboBox == monthCombo || comboBox== yearCombo)
        {
                daysCombo.setName("Bla-bla-bla");


        }
korifey
  • 3,379
  • 17
  • 17
0

Your comboboxes will need to implement the ItemListener interface and register themselves as listeners to the appropriate component(s).

mre
  • 43,520
  • 33
  • 120
  • 170
0

In the event of selecting a year or a month, you can instantiate a new JComboBox for the months or days (depending on the event) with the appropriate values passed for its constructor, and then update the GUI to reflect the change. Of course, the JComboBoxes must implement ItemListener for detecting the event and updating the appropriate elements in the GUI.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

You can store the combobox in a map. Have a global variable like

Map<String,JComboBox> nameToComboBox = new HashMap<String,JComboBox>();

In your createComboBoxes method, after you create a combox:

nameToComboxBox.put(comboBoxName, nameToComboBox);    

In the listener, you can update the day combobox:

case Helper.COMBOBOX_YEAR:
     year = Integer.parseInt(comboBox.getSelectedItem().toString().trim());
     JComboBox dayComboBox = nameToComboxBox.get("dayComboxBox");
     dayComboBox.setModel(new DefaultComboBoxModel(newDayItems));
     break;
evanwong
  • 5,054
  • 3
  • 31
  • 44