简体   繁体   English

如何将类型安全的对象强制转换为 JComboBox<String> ?

[英]How to cast type safety a Object to JComboBox<String>?

I have this code:我有这个代码:

@Override
public void itemStateChanged(ItemEvent evt) {
    if (evt.getStateChange() == ItemEvent.SELECTED) {
        Object sourceObject = evt.getSource();

        if (sourceObject instanceof JComboBox<?>) {
            JComboBox<String> jComboBox = (JComboBox<String>) sourceObject;
        }
    }
}

What is the best and correct casting for generics type safety and avoiding suppress the warning?什么是泛型类型安全和避免抑制警告的最佳和正确的转换?

You nailed it.你钉了它。 Runtime instanceof checks don't check generic parameters so the way you have shown it is the standard way that it is done.运行时instanceof检查不检查通用参数,因此您显示它的方式是完成它的标准方式。

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

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