3

3つのコンボボックスを含むフォームを持つJavaで記述されたMVCアプリケーションがあります。年/月/日と年と月の選択が変更された場合は日数を変更したい。ビューアでは、コンボボックスを定義するだけです

createComboBoxes( mainContentPage, "combobox name");

私のコントローラーには:

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);
    }
}}

他のイベントを発生させた後、どうすれば別のコンポーネントを変更できますか?

4

6 に答える 6

3

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

Alternatively, consider JCalendar.

于 2011-12-30T20:26:33.887 に答える
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.

于 2011-12-30T20:23:49.627 に答える
0

次のようなフィールドとしてコンボボックスに名前を付けることができます

JComboBox monthCobmo;
JComboBox daysCobmo;
JComboBox yearCobmo;

したがって、itemStateChanged()次のコードを記述します。

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


        }
于 2011-12-30T20:19:33.830 に答える
0

コンボボックスは、ItemListenerインターフェイスを実装し、適切なコンポーネントへのリスナーとして自身を登録する必要があります。

于 2011-12-30T20:19:41.313 に答える
0

年または月を選択する場合、そのコンストラクターに渡された適切な値を使用して、月または日 (イベントに応じて) の新しい JComboBox をインスタンス化し、GUI を更新して変更を反映させることができます。もちろん、JComboBox は、イベントを検出し、GUI の適切な要素を更新するためにItemListenerを実装する必要があります。

于 2011-12-30T20:22:27.723 に答える
0

コンボボックスをマップに格納できます。次のようなグローバル変数を持っています

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

createComboBoxes メソッドで、コンボボックスを作成した後:

nameToComboxBox.put(comboBoxName, nameToComboBox);    

リスナーでは、日のコンボボックスを更新できます。

case Helper.COMBOBOX_YEAR:
     year = Integer.parseInt(comboBox.getSelectedItem().toString().trim());
     JComboBox dayComboBox = nameToComboxBox.get("dayComboxBox");
     dayComboBox.setModel(new DefaultComboBoxModel(newDayItems));
     break;
于 2011-12-30T20:30:08.447 に答える