java-在更改另一个组合框项目选择后如何更改组合框的数据?
作者:互联网
我有一个用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);
}
}}
触发其他事件后如何更改另一个组件?
解决方法:
您可以更改从属组合的模型,如此example所示.
或者,考虑JCalendar
.
标签:jcombobox,event-handling,swing,comboboxmodel,java 来源: https://codeday.me/bug/20191101/1986853.html