我有两个组合框,第一个项目是(女性和男性).我想当用户在第一个组合框中选择女性时,女性服装列表将出现在第二个组合框中,当男性被选中时,男士服装列表将出现在第二个.可以使用JCombo框执行此功能吗?如果是的话我该怎么办呢?请给我举个例子.任何帮助将不胜感激.
ten*_*ica 12
在如何使用组合框和如何使用列表 totorials中查看如何使用模型.根据第一个组合框中的选择 - 重建,过滤或替换第二个组合框的模型.您可以使用/ extend DefaultComboBoxModel - a使用的默认模型JComboBox.例如,请考虑以下代码段:
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);
}
}
});
Run Code Online (Sandbox Code Playgroud)
或者,在第一个组合中选择后,您可以手动重建第二个组合中的项目,即:使用JComboBox方法removeAllItems()和addItem().
| 归档时间: |
|
| 查看次数: |
14710 次 |
| 最近记录: |