Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing - How to handle generics in ActionListener

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...

like image 824
Stephane Grenier Avatar asked Oct 05 '12 00:10

Stephane Grenier


People also ask

What is ActionListener in Java Swing?

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.

What is the purpose of ActionListener interface in swing?

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.

Why do we implement ActionListener in Java?

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.


2 Answers

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);
  }
}
like image 53
daniel Avatar answered Sep 29 '22 10:09

daniel


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
    }

});
like image 41
Guillaume Polet Avatar answered Sep 29 '22 11:09

Guillaume Polet