Change Column Type of jtable when table populate from resultset

前端 未结 2 1941

I have worker for fill the jtable from resultset like below;

public class WorkerFillTable extends SwingWorker {
          


        
相关标签:
2条回答
  • 2020-12-04 03:54

    I would recommend you to use a subclass of AbstractTableModel and override the getColumnClass method to set the correct data type for the 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
        }
    
    }
    
    0 讨论(0)
  • 2020-12-04 03:56

    JDBCAdapter, which extends AbstractTableModel, illustrates a typical mapping between relational database and Java data types. It may be seen here, and a complete example may be found in samples/demo/jfc/TableExample, found among the Java SE Development Kit 8u25 Demos and Samples Downloads. In outline,

    • Override getColumnClass().

    • Get the column's data type from ResultSetMetaData.

    • Use switch(type) to return the correct type-token.

    Also consider a SwingWorker<Row, Row> for finer granularity in publish()/process().

    0 讨论(0)
提交回复
热议问题