0

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 からフィルム レコードを読み取り、フィルムの新しいインスタンスを作成してから、それらをベクターに渡します。

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|
...
...

返信ありがとうございます。

4

1 に答える 1

0

この問題を解決するために、少し前に ListTableModel クラスを作成しました。

使用法:

ListTableModel ltm = new ListTableModel();
ltm.setModelClass(Film.class);
ltm.setColumnNames(new String[] {"title", "year", "genre"});
ltm.setDisplayNames(new String[] {"Title", "Year", "Genre"});
ltm.setList(getFilmList());

JTable table = new JTable(ltm, ltm.getTableColumnModel());

HibernateJPAまたはを使用している Spring ユーザーの場合IBatis、Bean 定義ファイルでテーブルを構成できます。

<jdal:service entity="Film" />

<swing:defaults />

<swing:table entity="Film">
    <swing:columns>
        <swing:column name="title" displayName="title" />
        <swing:column name="year"  displayName="Year"  /> 
        <swing:column name="gemre" displayName="Gemre" />  
    </swing:columns>
</swing:table>

TablePanel table = applicationContext.getBean("filmTable", TablePanel.class);

サーバー側のTablePanelページングとソートを追加します。

于 2013-05-02T12:39:56.400 に答える