【发布时间】:2015-09-24 08:15:03
【问题描述】:
我已经查看了数十篇关于此主题的不同帖子,但仍未找到解决方案。我正在尝试修改此代码,使其不再产生编译器警告:
警告:[未选中] 作为原始类型 JList 的成员对 setListData(Vector) 的未选中调用 jlistCoilModels.setListData(coilModelList);其中 E 是一个类型变量: E 扩展类 JList 中声明的 Object
我读过的所有帖子都在谈论泛型与原始以及类型转换。 (我阅读了 Oracle 文档)我明白了,我只是不知道如何解决这个问题,以便它不再产生编译器警告。省略无聊的细节,这是我正在使用的代码行。
Vector<String> coilModelList = new Vector<>();
while (rset08.next()) {
coilModelList.add(rset08.getString("CoildModelNumber"));
}
jlistCoilModels.setListData(coilModelList);
感谢您抽出宝贵时间回复。
编辑:2015-07-07 好的,希望这将使该线程脱离 HOLD。这是我在测试应用程序中的全部代码。编译器甚至会针对 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
}
【问题讨论】:
-
显示
jlistCoilModels的声明,我打赌这只是JList而不是JList<String>。 -
您遗漏了太多代码,我们需要查看更多。哪一行具体导致了警告?
-
使用
ListModel恕我直言也更容易 -
如果你看一下方法声明;
setListData(Vector<? extends E> listData),您会看到它期待从E扩展的对象的Vector。E由JList类本身定义,JList<E>,当你声明你的变量时,你也需要定义约束。也许你也应该看看Generics Trail -
JList 对象 (jlistCoilModels) 是通过将其拖放到 GUI 上来创建的。它的代码是自动生成的。那么我是否需要检查生成的代码,并确保它使用“JList
”声明它?