2013-04-22 59 views
0

我使用2个JComboBoxes条件的JComboBox

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 

回答

1

要获得从的JComboBox选择的值使用getSelectedItem

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

现在你可以检查是否value等于text1,如果是这样,则可以使用removeItem从其他JComboBox中删除项目。

+1

+1谢谢你的回答 – vsr 2013-04-22 12:50:57

0

您可以添加动作监听box1,如果你想Disable项目在JComboBox没有删除它,就像控制显示什么/在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 { 
       // .. 
      } 
     } 
    }); 
+1

+1谢谢你的回答 – vsr 2013-04-22 12:56:07

0

启用:

enter image description here

所以你可以试试this example

+2

+1谢谢你的回答 – vsr 2013-04-22 12:55:11