I have:
class CustomerActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
JComboBox cb = (JComboBox)event.getSource();
.. do something
}
}
Which causes the following compiler warning in jdk7:
JComboBox is a raw type. References to generic type JComboBox should be parameterized
I've tried to parameterize it to such that:
JComboBox<String> cb = (JComboBox<String>)event.getSource();
But this still leaves the following compiler warning:
Type safety: Unchecked cast from Object to JComboBox
Therefore I'm not sure how to eliminate the compiler warnings...
You implement an action listener to define what should be done when an user performs certain operation. An action event occurs, whenever an action is performed by the user. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field.
ActionListener in Java is a class that is responsible for handling all action events such as when the user clicks on a component. Mostly, action listeners are used for JButtons. An ActionListener can be used by the implements keyword to the class definition.
To determine where the user clicked on the screen, Java provides an interface called "ActionListener" through which we determine where the user clicked and generates an event to perform several tasks, like calculation, print a value, print a specific character, etcetera using a button.
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);
}
}
The only way out here is to grab a typed reference to your JComboBox
:
Either like this
JComboBox<String> myStringCb = new JComboBox<String>();
...
myStringCb.addActionListener(new CustomerActionListener(myStringCb);
and with you ActionListener
:
class CustomerActionListener implements ActionListener {
private JComboBox<String> cb;
public CustomerActionListener(JComboBox<String> cb) {
this.cb = cb;
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource()==cb) {
// Here you can do something with the typed cb
}
}
}
Or, another solution is to use an anonymous ActionListener
with a final
reference:
final JComboBox<String> myStringCb = new JComboBox<String>();
myStringCb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// Here you can refer directly to myStringCb
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With