I wanted to create a flexible table model where it's list variable type may differ upon it's instantiation. I extends AbstractTableModel class, here is the code :
package models;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.table.AbstractTableModel;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
public class GenericTableModel < T > extends AbstractTableModel{
private static final long serialVersionUID = 1L;
private List< T > list = new ArrayList< T >();
private int columns;
GenericTableModel(int columns){
this.columns = columns;
}
public void updateAll(Collection<T> collection){
list.clear();
list.addAll(collection);
fireTableDataChanged();
}
public T getCell(int index){
return list.get(index);
}
public void setData(List list){
this.list = list;
fireTableDataChanged();
}
public void insert(T data){
list.add(data);
fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
}
public void delete(int index){
list.remove(index);
fireTableRowsDeleted(index, index);
}
public void update(int index, T data){
list.set(index, data);
fireTableRowsUpdated(index, index);
}
public T select(int index){
return list.get(index);
}
@Override
public int getRowCount() {
return list.size();
}
@Override
public int getColumnCount() {
return columns;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
String databaseURL = "jdbc:mysql://localhost/perkuliahan";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try{
connection = DriverManager.getConnection(databaseURL, "root", "");
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
T className = null;
resultSet = statement.executeQuery("select * from " + className.getClass().getName());
while(resultSet.getRow()!=rowIndex){
resultSet.next();
}
return resultSet.getObject(columnIndex);
}
catch(SQLException sqlException){
return sqlException.getMessage();
}
finally{
try{
resultSet.close();
statement.close();
connection.close();
}
catch(SQLException sqlException){
return sqlException.getMessage();
}
}
}
@Override
public String getColumnName(int column){
String databaseURL = "jdbc:mysql://localhost/perkuliahan";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try{
connection = DriverManager.getConnection(databaseURL, "root", "");
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
T className = null;
resultSet = statement.executeQuery("select * from " + className.getClass().getName());
ResultSetMetaData resultSetMetaData;
resultSetMetaData = resultSet.getMetaData();
return resultSetMetaData.getColumnName(column);
}
catch(SQLException sqlException){
return sqlException.getMessage();
}
finally{
try{
resultSet.close();
statement.close();
connection.close();
}
catch(SQLException sqlException){
return sqlException.getMessage();
}
}
}
}
But, once I declared it, it shows error message :
package forms;
import models.GenericTableModel;
import pojo.Mahasiswa;
public class MahasiswaForm extends javax.swing.JFrame {
/**
* Creates new form MahasiswaForm
*/
public MahasiswaForm() {
initComponents();
GenericTableModel<Mahasiswa> tableModel = new GenericTableModel<Mahasiswa>();
}
Can anybody tell me what is wrong ? I need this for school assignment, thank you.
Check out Row Table Model. It is an example of a generic TableModel. You will need to extend this class and implement the getValueAt() and setValueAt() methods for your POJO.
You should NOT have database code in the getValueAt()
method. This method is called frequently whenever the table needs to render a cell. Therefore this code should be very efficient. Same comment for the getColumnClass()
method although this will not be an issue if you use the RowTableModel.
For a completely generic solution so that you need do write a custom TableModel you can also look at the Bean Table Model