我定义了一个自定义ListModel
作为保存一些数据的其他类的内部类。填充ListModel
,我查询一个数据库文件并选择其中的数据,如下面的代码所示。一切都在 JList
中完美呈现。使用
这个ListModel
.
我不明白的是“使用未经检查或不安全的操作”。当我使用 -Xlint 编译时,我收到警告:
“缺少泛型类的类型参数DefaultListModel<E>
”
我的问题是 <E>
是什么?在下面我的代码的第一行:
public class SalListModel extends DefaultListModel<E=?>{
String defaultSearch="*";
String salName=defaultSearch;
//inner class constructor
public SalListModel(){
try{
//connection creates the database if it does not exist
Class.forName("org.sqlite.JDBC");
Connection c=
DriverManager.getConnection("jdbc:sqlite:"+dbfile);
Statement st=c.createStatement();
String sql;
sql="select "+salName+" from "+tablename+
" ORDER BY "+name[0]+" ASC;"; //alphabetical order
ResultSet rs=st.executeQuery(sql);
while (rs.next()){
addElement(rs.getString(name[0])+" "+rs.getString(cnp[0]));
}
rs.close();
st.close();
c.close();
}
catch (ClassNotFoundException cnfe){
System.out.println("SQL SalListModel class not found: "+
cnfe.getMessage());
}
catch (SQLException sqle){
System.out.println("SalListModel SQL exception:"+
sqle.getMessage());
}
}
}
最佳答案
为什么?您没有向模型添加任何新功能。将数据加载到模型中不应被视为新功能。只需在类中创建一个方法即可将数据加载到模型中。那么你的代码将类似于:I have defined a custom ListModel
ListModel<String> model = new DefaultListModel<String>();
addDataToModel( model );
My questions is what is
<E>
in the first line of my code below:
您正在添加String
数据到模型,所以你应该使用 DefaultListModel<String>
关于java - 列表模型中元素的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584275/