i'm beginner in java and i have an application with UI Swing and sqlite like database so i'want simply to know how to bind data to UI. For 开发者_如何学编程example i want to bind all data in person table to a JTable. thanks in advance (please a detailed example).
My opinion is, you shouldn't be binding sql objects to gui objects, as you just leave connections/statements/resultsets open unnecessarily. Typically you'd create a POJO type object that holds data from the database, and populate a List<SomePojo>
of data that represents what you want to show in the JTable.
Then create your own Model class that derives from
javax.swing.table.AbstractTableModel
and uses this List<SomePojo>
as the data for the model. Thus at a minimum, you'd have to implement:
public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);
that looks at this List<SomePojo>
for the value to return.
Obviously, you'd have some canned mapping of POJO fields to column indexes, and the elements in the List<SomePojo>
maps to the rows.
Of course if you want to ignore this advise and bind sql to gui, then the model data would be the result set, and not the List<SomePojo>
精彩评论