当表从结果集填充时,更改列的jtable类型

时间:2014-03-04 18:51:42

标签: java swing jtable swingworker tablemodel

我有工作人员从结果集中填写jtable,如下所示;

public class WorkerFillTable extends SwingWorker<DefaultTableModel, DefaultTableModel> {
        private DefaultTableModel modeltable;
        public WorkerFillTable(DefaultTableModel modeltable) {
         this.modeltable = modeltable;
        }
       @Override
     protected DefaultTableModel doInBackground() throws Exception {
            ResultSet rs;
            Statement stmt;
            String query = "select Name,ID,Status,IsActive from current_conf\n" +
            "order by Name,ID";

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://192.168.130.100;" + "databaseName=DBST;" + "user=" + "user1" + ";" + "password=" + "userpass1" + ";"; 
            Connection con = DriverManager.getConnection(connectionUrl);
            stmt = con.createStatement();
            rs = stmt.executeQuery(query);
            ResultSetMetaData rsmd = rs.getMetaData();

            Vector<String> columnNames = new Vector<String>();
            int columnCount = rsmd.getColumnCount();

            for (int column = 0; column < columnCount; column++) {
                columnNames.addElement(rsmd.getColumnLabel(column + 1));
            } 

            Vector<Vector<Object>> data = new Vector<Vector<Object>>();

           while (rs.next()) {
                Vector<Object> vector = new Vector<Object>();
                for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
                }
                data.add(vector);
           }

            rs.close();
            stmt.close();
            modeltable = new DefaultTableModel(data, columnNames);
            return modeltable;
        }


      @Override
      protected void done() {
        try {
            TableModel modeltable = get();
            sorter = new TableRowSorter(modeltable);
            cTable.setRowSorter(sorter);
            cTable.setModel(modeltable);
        } catch (InterruptedException | ExecutionException ex) {
        }
      }

    }

但所有列都是String。 DB的“IsActive”列只有“1”或“0”Jtable将此列显示为“true”或“false”我需要在jtable中编辑最后一列typr到boelan。我该怎么办?

2 个答案:

答案 0 :(得分:4)

扩展AbstractTableModel

JDBCAdapter说明了关系数据库和Java数据类型之间的典型映射。可以看到heresamples/demo/jfc/TableExample中可以找到getColumnClass()中的完整示例。概括地说,

另请考虑在publish()/process()中更精细的{{1}} {/ 1}。

答案 1 :(得分:0)

我建议你使用AbstractTableModel的子类并覆盖getColumnClass方法来为JTable设置正确的数据类型。

public class CustomTableModel extends AbstractTableModel
{
    @Override
    public int getRowCount() 
    {
    }

    @Override
    public int getColumnCount() 
    {
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) 
    {
    }

    @Override
    public String getColumnName(int index)
    {
    }

    @Override
    public Class<String> getColumnClass(int i)
    {
        //return the Specific class for each column based on index i
    }

}