JavaFX: Create a dynamic tableview with generic types

后端 未结 1 1929
太阳男子
太阳男子 2021-01-16 11:58

So, first of all, my code is based on the accepted answer in this thread.

I\'m trying to create a tableview from a database, and this tableview should be populated d

相关标签:
1条回答
  • 2021-01-16 12:48

    This should work if you use ResultSet.getObject(index) to populate the table. The JDBC driver will return an appropriate object type, according to standard mappings defined in the JDBC specification (e.g. an int column will map to a java.lang.Integer, etc).

    I don't particularly like the code you linked: it produces a lot of warnings about type safety, to which you should pay attention. I would use something like this (caveat: not tested):

    Data wrapper class:

    public class DataResult {
    
        private final List<String> columnNames ;
        private final List<List<Object>> data ;
    
        public DataResult(List<String> columnNames, List<List<Object>> data) {
            this.columnNames = columnNames ;
            this.data = data ;
        }
    
        public int getNumColumns() {
            return columnNames.size();
        }
    
        public String getColumnName(int index) {
            return columnNames.get(index);
        }
    
        public int getNumRows() {
            return data.size();
        }
    
        public Object getData(int column, int row) {
            return data.get(row).get(column);
        }
    
        public List<List<Object>> getData() {
            return data ;
        }
    }
    

    Database accessor class:

    public class DAO {
    
        private Connection conn ;
    
    
        public DAO() {
            // initialize connection...
        }
    
        public DataResult getAllData() throws SQLException {
    
            List<List<Object>> data = new ArrayList<>();
            List<String> columnNames = new ArrayList<>();
    
            try (
                    Statement stmt = conn.createStatement();
                    ResultSet rs = stmt.executeQuery("select * from some_table")) {
    
    
                int columnCount = rs.getMetaData().getColumnCount();
    
                for (int i = 1 ; i <= columnCount ; i++) {
                    columnNames.add(rs.getMetaData().getColumnName(i));
                }
    
                while (rs.next()) {
                    List<Object> row = new ArrayList<>();
                    for (int i = 1 ; i <= columnCount ; i++) {
                        row.add(rs.getObject(i));
                    }
                    data.add(row);
                }
            }
    
            return new DataResult(columnNames, data);
        }
    }
    

    GUI code:

    TableView<List<Object>> table = new TableView<>();
    DAO dao = new DAO();
    DataResult data = dao.getAllData();
    
    for (int i = 0 ; i < data.getNumColumns() ; i++) {
        TableColumn<List<Object>, Object> column = new TableColumn<>(data.getColumnName(i));
        int columnIndex = i ;
        column.setCellValueFactory(cellData -> 
            new SimpleObjectProperty<>(cellData.getValue().get(columnIndex)));
        table.getColumns().add(column);
    }
    
    table.getItems().setAll(data.getData());
    

    Using this version, the data provided to the table contains objects of the appropriate type for the columns - not necessarily strings. So, for example, if a database column is defined with (SQL) type int, then the table view column will contain java.lang.Integer instances, and the sorting will be according to the implementation of Integer.compareTo(...) (i.e. in proper numeric order).

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