对象< String>的通配符;在Java6中 [英] WildCard for Object<String> in Java6

查看:87
本文介绍了对象< String>的通配符;在Java6中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请如何更正投射并删除警告

Please how to correct casting and remove warning

[unchecked] unchecked cast
required:   T
found:      java.lang.Object

来自SSCCE

import java.awt.*;
import javax.swing.*;

public class JComboBoxWithWildCard {

    private JDialog dlg = new JDialog();
    private final Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");

    public JComboBoxWithWildCard() {
        JComboBox comboWithCustomRenderer = new FlexiComboBox<String>(
                "1 one", "2 two", "3 three", "4 four", "5 five", "6 six",
                "7 seven", "8 eight", "9 nine", "10 ten", "11 eleven") {

            private static final long serialVersionUID = 1L;

            @Override
            public String getCaption(String item) {
                return item;
            }

            @Override
            public Icon getItemIcon(String item) {
                return errorIcon;
            }
        };
        comboWithCustomRenderer.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXX");
        comboWithCustomRenderer.setMaximumRowCount(6);
        dlg.add(comboWithCustomRenderer);
        dlg.pack();
        dlg.setLocationRelativeTo(null);
        dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dlg.setVisible(true);
    }

    public static void main(String[] a_args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JComboBoxWithWildCard pd = new JComboBoxWithWildCard();
            }
        });
    }
}

abstract class FlexiComboBox<T> extends JComboBox {

    private static final long serialVersionUID = 1L;

    public FlexiComboBox(T... items) {
        super(items);
    }

    @Override
    public void updateUI() {
        setRenderer(new DefaultListCellRenderer() {

            private static final long serialVersionUID = 1L;

            @Override
            public Component getListCellRendererComponent(JList list, Object value,
                    int index, boolean isSelected, boolean cellHasFocus) {
                Component result = super.getListCellRendererComponent(list,
                        getCaption((T) value), index, isSelected, cellHasFocus);
                Color color = getItemColor((T) value);
                if (color != null) {
                    result.setBackground(color);
                }
                if (result instanceof JLabel) {
                    ((JLabel) result).setIcon(getItemIcon((T) value));
                }
                return result;
            }
        });
        super.updateUI();
    }

    public abstract String getCaption(T item);

    public Color getItemColor(T item) {
        return null;
    }

    public Icon getItemIcon(T item) {
        return null;
    }
}

警告来自代码行

1.(代码行70.th)

1.(codeline 70.th)

Component result = super.getListCellRendererComponent(list,
       getCaption((T) value), index, isSelected, cellHasFocus);

2.(代码行71.th)

2.(codeline 71.th)

Color color = getItemColor((T) value);

3.(代码行76.th)

3.(codeline 76.th)

((JLabel) result).setIcon(getItemIcon((T) value));

推荐答案

出现警告是因为编译器无法确保不会抛出ClassCastException.在这种情况下,以下内容将导致异常.

The warning is there because the compiler can not ensure that a ClassCastException won't be thrown. In this case, the following would cause an exception.

FlexiComboBox<Integer> box = new FlexiComboBox<Integer>();
box.addItem("BAD ITEM");
//then add the combobox to a panel and display it.

虽然您不太可能编写出与您编写的类的意图明显相反的代码,但编译器警告您,该代码可能被滥用.

While it is not likely that you will write code that blatantly goes against the intent of the class you wrote, the compiler is warning you that this code could be misused.

隐藏错误的方法是将@SuppressWarnings("unchecked")添加到getListCellRendererComponent.这告诉编译器您知道存在风险,但是您不希望将它们报告为警告.

The way you can hide the errors is to add @SuppressWarnings("unchecked") to getListCellRendererComponent. This tells the compiler that you know there are risks, but you don't want them to be reported as a warning.

这篇关于对象&lt; String&gt;的通配符;在Java6中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆