更改另一个组合框项目选择后如何更改组合框的数据?
我有一个用 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);
}
}}
:如何在触发其他事件后更改另一个组件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以更改依赖组合的模型,如本示例所示。
或者,考虑
JCalendar
。You can change the dependent combos' models, as shown in this example.
Alternatively, consider
JCalendar
.您可以为当天的组合框创建自己的组合框模型。根据年和月组合框的选定值,您可以将日期组合框模型置于正确的状态以显示适当的天数。扩展
DefaultComboBoxModel
的最简单方法。编辑:
创建自定义模型使您可以更轻松地在选项之间进行更改。一个月可以有 28 天、29 天、30 天或 31 天。自定义类可以有一种方法来设置显示多少天,并且不需要外部进行任何额外的工作。另一个选项是在事件处理程序中创建选项集,并在每次进行更改时替换模型。自定义类对代码进行分区,以便事件处理程序仅设置显示的天数,而无需关心这如何影响数据的基础模型。
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.
您可以将组合框命名为字段,例如
在
itemStateChanged()
中编写以下代码:You can have named comboboxes as fields like
So in
itemStateChanged()
write following code:您的组合框需要实现
ItemListener
接口并将自己注册为适当组件的侦听器。Your comboboxes will need to implement the
ItemListener
interface and register themselves as listeners to the appropriate component(s).如果选择一年或一个月,您可以使用为其构造函数传递的适当值来实例化几个月或几天(取决于事件)的新 JComboBox,然后更新 GUI 以反映更改。当然,JComboBoxes必须实现 ItemListener 用于检测事件并更新 GUI 中的相应元素。
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.
您可以将组合框存储在地图中。
有一个全局变量,如
在 createComboBoxes 方法中,创建组合框后:
在侦听器中,您可以更新日期组合框:
You can store the combobox in a map.
Have a global variable like
In your createComboBoxes method, after you create a combox:
In the listener, you can update the day combobox: