简体   繁体   English

Java Swing - 如何在 ActionListener 中处理泛型

[英]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:这会导致 jdk7 中出现以下编译器警告:

JComboBox is a raw type. JComboBox 是原始类型。 References to generic type JComboBox should be parameterized对泛型类型 JComboBox 的引用应该被参数化

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类型安全:从 Object 到 JComboBox 的未经检查的强制转换

Therefore I'm not sure how to eliminate the compiler warnings...因此我不确定如何消除编译器警告......

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. JComboBox 的类型现在设置为字符串。 To get the selected Item, you have to go through the ComboBoxModel.要获得选定的 Item,您必须通过 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 :唯一的出路是获取对JComboBox的类型化引用:

Either like this要么像这样

JComboBox<String> myStringCb = new JComboBox<String>();
...
myStringCb.addActionListener(new CustomerActionListener(myStringCb);

and with you ActionListener :和你一起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引用的匿名ActionListener

final JComboBox<String> myStringCb = new JComboBox<String>();
myStringCb.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e) {
        // Here you can refer directly to myStringCb
    }

});

Try to check this:尝试检查这个:

A very useful link form StackOverflow 一个非常有用的链接表单 StackOverflow

In few words, Java compiler don't know what object is the one you're trying to cast, so it don't like to do the cast without a word, it must advise you that you maybe are making a mistake (but you DO know what kind of Class is the Object you are casting, so don't mind about it) Add a @SuppressWarning("unchecked")简而言之,Java 编译器不知道您要转换的对象是什么对象,因此它不喜欢一言不发地进行转换,它必须提醒您您可能犯了错误(但是您知道你正在投射的对象是什么类型的类,所以不要介意)添加一个@SuppressWarning("unchecked")

Yet another solution另一个解决方案

You can also use the Java Wildcard pattern:您还可以使用Java 通配符模式:

class CustomerActionListener implements ActionListener
{
  @Override
  public void actionPerformed(ActionEvent event)
  {
    JComboBox<?> cb = (JComboBox<?>)event.getSource();
    .. do something
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM