0

我正在使用 2 个 JComboBoxes

String arr1[] = {"text1", "text2", "text3"};
String arr2[] = {"text1", "text2", "text3"};

JComboBox box1 = new JComboBox(arr1);
JComboBox box2 = new JComboBox(arr2);

我正在寻找类似的条件

if(text1 in box1 is selected)
only text2 and text3 is selectable/enabled in box2
4

3 回答 3

1

要从JComboBox中获取所选值,请使用getSelectedItem

String value = (String)box.getSelectedItem();

现在您可以检查是否value等于text1,如果是,您可以使用removeItem从另一个JComboBox中删除项目。

于 2013-04-22T12:00:21.433 回答
0

您可以添加一个动作监听器box1并控制显示/启用的内容box2

box1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
             // code to manipulate values in box2 depending upon value in `box1Value`
            String box1Value = txtFilename.getSelectedItem().toString();
            if(box1Value.equalsIgnoreCase("text1")){
                String arr2[] = { "text2", "text3"};
                new JComboBox(arr2);
            } else {
                // ..
            }
        }
    });
于 2013-04-22T12:03:27.420 回答
0

如果你想Disable在你的项目中JComboBox不删除它,就像这样:

在此处输入图像描述

所以你可以试试这个例子

于 2013-04-22T12:17:36.363 回答