Java 如何在没有编译器警告的情况下填写jlist

Java 如何在没有编译器警告的情况下填写jlist,java,swing,generics,jlist,Java,Swing,Generics,Jlist,我看了几十篇关于这个话题的不同帖子,仍然没有找到解决办法。我试图修改此代码,使其不再产生编译器警告: 警告:[未选中]未选中调用setListData(Vector)作为原始类型JList的成员 jlistCoilModels.setListData(coilModelList);其中E是一个类型变量: 扩展类JList中声明的对象 我读过的所有帖子都在谈论泛型与原始类型以及类型转换。(我阅读了Oracle文档)我明白了,我只是不知道如何修复它,使它不再产生编译器警告。撇开无聊的细节不谈,下面是

我看了几十篇关于这个话题的不同帖子,仍然没有找到解决办法。我试图修改此代码,使其不再产生编译器警告:

警告:[未选中]未选中调用setListData(Vector)作为原始类型JList的成员 jlistCoilModels.setListData(coilModelList);其中E是一个类型变量: 扩展类JList中声明的对象

我读过的所有帖子都在谈论泛型与原始类型以及类型转换。(我阅读了Oracle文档)我明白了,我只是不知道如何修复它,使它不再产生编译器警告。撇开无聊的细节不谈,下面是我正在使用的代码行

Vector<String> coilModelList = new Vector<>();
while (rset08.next()) {
    coilModelList.add(rset08.getString("CoildModelNumber"));
}
jlistCoilModels.setListData(coilModelList);
Vector coilModelList=new Vector();
while(rset08.next()){
添加(rset08.getString(“CoildModelNumber”);
}
jlistCoilModels.setListData(coilModelList);
感谢您花时间回复

编辑:2015-07-07 好的,希望这会使这条线停止。下面是我在测试应用程序中的全部代码。编译器甚至对NetBeans自动生成的代码段发出警告。我无法更改此代码的自动生成部分。为了避免编译器警告,我仍然没有进一步了解使用JList的正确方法。即使是我在网上找到的教程也会产生相同的结果。我知道这里有一些对我来说非常重要的东西需要学习。我正在认真地寻求了解使用JList的最佳实践

package jlist.example;

import javax.swing.DefaultListModel;

/**
 *
 * @author petehahn
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList(); //this line produces a warning
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(jList1);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(jButton1)))
                .addContainerGap(176, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String[] filler = {"Filler1", "Filler2", "Filler3", "Filler4", "Filler5"};
        DefaultListModel<String> fillerListModel = new DefaultListModel<>();
        for(int i = 0; i < filler.length - 1; i++) {
            fillerListModel.addElement(filler[i]);
        }
        jList1.setModel(fillerListModel);// this line produces a warning

    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JList jList1;// this line produces a warning
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   
}
package jlist.example;
导入javax.swing.DefaultListModel;
/**
*
*@作者彼得·哈恩
*/
公共类NewJFrame扩展了javax.swing.JFrame{
/**
*创建新表单NewJFrame
*/
公共NewJFrame(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
jScrollPane1=newjavax.swing.JScrollPane();
jList1=newjavax.swing.JList();//此行产生一个警告
jButton1=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(jList1);
setText(“jButton1”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58,58,58)
.addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,166,javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(97,97,97)
.addComponent(jButton1)))
.addContainerGap(176,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addContainerGap(12,简称最大值)
.addComponent(jButton1)
.addGap(18,18,18)
.addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED\u SIZE,228,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGap(19,19,19))
);
包装();
}//                         
私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
字符串[]filler={“Filler1”、“Filler2”、“Filler3”、“Filler4”、“Filler5”};
DefaultListModel fillerListModel=新的DefaultListModel();
对于(int i=0;i  jList1 = new javax.swing.JList(); 
  private javax.swing.JList jList1;
  jList1 = new javax.swing.JList<>(); 
  private javax.swing.JList<String> jList1;