通用 Table 型号

Generic Table Model

我想创建一个灵活的 table 模型,它的列表变量类型在实例化时可能会有所不同。我扩展了 AbstractTableModel class,这里是代码:

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

}

但是,一旦我声明它,它就会显示错误消息:

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

谁能告诉我哪里出了问题?学校作业需要这个,谢谢。

查看 Row Table Model。它是通用 TableModel 的示例。您将需要扩展此 class 并为您的 POJO 实现 getValueAt() 和 setValueAt() 方法。

您不应在 getValueAt() 方法中包含数据库代码。每当 table 需要呈现单元格时,都会频繁调用此方法。因此这段代码应该非常高效。 getColumnClass() 方法的注释相同,但如果您使用 RowTableModel,这将不是问题。

对于完全通用的解决方案,您需要编写自定义 TableModel,您还可以查看 Bean Table Model