0

i want fill my JTable with films.

I have class 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()+ '}';
}


}

And from DB i read my film records and create new instances of films and then i give them to 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);
    }

Can anybody help me, how can i fill my JTable with my films to:

|Name|Year|Genre|
=================
|Batman|2010|Action|
...
...

Thanks for any response.

  • 3
    You need to create a TableModel based on your `Vector`. I recommend that you check the [JTable Swing tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) (please click on the link) which will help you get started on the creation of an AbstractTableModel based on this. Come on back with your code attempt including an [sscce](http://sscce.org) if you get stuck. – Hovercraft Full Of Eels May 01 '13 at 21:58
  • 1
    thanks, i'l have a look at it :) – Lukas Hamrla May 01 '13 at 22:02
  • and addeum to @HFOE comment maybe there isn't required to create / use class Film, nor whatever interface, because XxxTableModel is the same as your class Film, for why reason to hold two classes with the same data, – mKorbel May 01 '13 at 22:03
  • 1
    For [example](http://stackoverflow.com/a/16264880/230513). – trashgod May 02 '13 at 00:46
  • You can also look at this [example](http://stackoverflow.com/questions/15731045/jtable-not-updating-with-my-abstracttablemodel/15731761#15731761) for TableModel – Amarnath May 02 '13 at 02:29

1 Answers1

0

I wrote a ListTableModel class some time ago to sove this problem.

Usage:

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());

If you are a Spring user using Hibernate, JPA or IBatis, the table can be configured in a bean defintion file:

<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);

The TablePanel add server side paging and sorting.

Jose Luis Martin
  • 10,459
  • 1
  • 37
  • 38