我想建立一个表单,可以将自己的值填入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”中。任何想法是关于我要去哪里的错误?