我想建立一個表單,可以將自己的值填入JTextField或依賴預設的選項,這取決於從JComboBox中的選擇。JTextField更新基於JComboBox選擇
這是JComboBox中
String[] areas = new String [] {"Own Specifications", "SurveySample", "UK", "London", "Surrey"};
@SuppressWarnings({ "unchecked", "rawtypes" })
final JComboBox<String> selectedArea = new JComboBox(areas);
//selectedArea = new JComboBox<String>();
selectedArea.setModel(new DefaultComboBoxModel<String>(areas));
selectedArea.setBounds(282, 52, 164, 27);
contentPane.add(selectedArea);
這是JTextField的
tenurePrivateRenters = new JTextField();
tenurePrivateRenters.setHorizontalAlignment(SwingConstants.CENTER);
tenurePrivateRenters.setText("Private Renters");
tenurePrivateRenters.setBounds(58, 213, 134, 28);
contentPane.add(tenurePrivateRenters);
根據用戶的選擇的JComboBox,在一個JTextField,該值應該改變,例如如果調查中的樣本選擇應JTextField的偶然其值設置爲10
我嘗試以下兩個選項:
selectedArea.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
Object selectedValue = selectedArea.getSelectedItem();
if(selectedValue.equals("Own Specifications")){
tenurePrivateRenters.setText("10");
System.out.println("Good choice!");
}
}
});
和
selectedArea.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e){
@SuppressWarnings("unchecked")
JComboBox<String> selectedArea = (JComboBox<String>) e.getSource();
String selectedItem = (String) selectedArea.getSelectedItem();
if(selectedItem.equals("Own Specifications")){
tenurePrivateRenters.setText("10");
System.out.println("Good choice!");
}
}
}
);
}
但對於這兩個選項沒有任何反應和JTextField的值保留在「Private Renters」中。任何想法是關於我要去哪裏的錯誤?