Java Swing - How to handle generics in ActionListener
I apreciate this approach. It avoids any Typecasts and is easy to read.
I improved my answer, now It doesn't give you Compiler Warnings. The Type of JComboBox is now set to String. To get the selected Item, you have to go through the ComboBoxModel.
class CustomerActionListener implements ActionListener
{
private JComboBox<String> comboBox;
public CustomerActionListener(JComboBox<String> comboBox){
this.comboBox = comboBox;
}
@Override
public void actionPerformed(ActionEvent event)
{
// Just use the comboBox
ComboBoxModel<String> model = comboBox.getModel();
int index = comboBox.getSelectedIndex();
String choosen = model.getElementAt(index);
System.out.println("Hey you choose "+choosen);
}
}