用类的实例填充java JTable
我想用胶卷填满我的桌子
我有课Film
public class Film extends MainComponent{
String title;
String year;
String genre;
//director
//actor
public Film(String title, String year, String genre, int id, int stars) {
super(id, stars);
this.title = title;
this.year = year;
this.genre = genre;
}
public String getTitle() {
return title;
}
public String getYear() {
return year;
}
public String getGenre() {
return genre;
}
@Override
public String toString() {
return "Film{" + "title=" + title + ", year=" + year + ", genre=" + genre +",id="+getId()+", stars="+getStars()+ '}';
}
}
我从DB中读取我的电影记录,创建新的电影实例,然后把它们交给vector
public ResultSet select(String table, String where) {
try {
Statement sta = con.createStatement();
System.out.println( "SELECT * FROM " + table + " WHERE " + where );
return sta.executeQuery( "SELECT * FROM " + table + " WHERE " + where );
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return null;
}
ResultSet rs;
Vector<Film> films = new Vector();
rs = db.select("films");
try{
while(rs.next()){
films.add(new Film(rs.getString("name"), rs.getString("year"), "Action", rs.getInt("id"), 5));
}
}catch (SQLException exc){
System.out.println("Error: " + exc);
}
有人能帮我吗?我如何用我的电影填充我的JTable以:
|Name|Year|Genre|
=================
|Batman|2010|Action|
...
...
谢谢你的回复
# 1 楼答案
不久前我写了一个ListTableModel类来解决这个问题
用法:
如果您是使用
Hibernate
、JPA
或IBatis
的Spring用户,则可以在bean定义文件中配置该表:{}添加服务器端分页和排序