如何使用另一个组合框swing来控制组合框 [英] how to control a combo box by using another combo box swing

查看:111
本文介绍了如何使用另一个组合框swing来控制组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个组合框,第一个项目是(女性和男性)。我想当用户在第一个组合框中选择女性时,女性连衣裙列表将出现在第二个组合框中,当男性被选中时男性连衣裙列表将出现在第二个。可以使用JCombo框执行此功能吗?如果是的话我该怎么办呢?请给我举个例子。
任何帮助将不胜感激。

I have two combo box the items first one is (women and men).I want when user select women in first combo box the list of women's dress will appear in second combo box and when men is selected the list of men's dress will appear in second one.Can do this functionality by using JCombo box? if yes how can I do that give me example please. any help will be appreciated.

推荐答案

查看如何使用如何使用组合框如何使用使用列表 totorials。根据第一个组合框中的选择 - 重建,过滤或替换第二个组合框的模型。您可以使用/扩展 DefaultComboBoxModel - <使用的默认模型code>的JComboBox 。例如,请考虑以下代码段:

Check out how to work with models in How to Use Combo Boxes and How to Use Lists totorials. According to a selection in the first combo box - rebuild, filter or perhaps replace the model of the second combo box. You can use/extend DefaultComboBoxModel - a default model used by a JComboBox. For example consider this snippet:

final JComboBox genderComboBox = null;
final JComboBox itemComboBox = null;

final DefaultComboBoxModel hisModel = new DefaultComboBoxModel(new String[]{"a", "b", "c"});
final DefaultComboBoxModel herModel = new DefaultComboBoxModel(new String[]{"x", "y", "z"});

genderComboBox.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        if ("Men".equals(genderComboBox.getSelectedItem())){
            itemComboBox.setModel(hisModel);    
        } else {
            itemComboBox.setModel(herModel);    
        }
    }
});

或者,在第一个组合中选择后,您可以手动重建第二个项目中的项目,即:使用 JComboBox 方法 removeAllItems() addItem()

Alternatively, upon selection in the first combo you can rebuild the items in the second one manually, ie: using JComboBox methods removeAllItems() and addItem().

这篇关于如何使用另一个组合框swing来控制组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆