Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
867 views
in Technique[技术] by (71.8m points)

java - JavaFX: Create a dynamic tableview with generic types

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 dynamically based on what data the user asks for. While that's working so far, my problem is that all values gathered from the database are interpreted as strings in java, but I need their actual values.

The reason for this is that the built-in sorting mechanism of the javafx tableview sorts all numbers as strings because their type is converted to string when populating the table.

Example: Values 921, 200, 110, 1, 2011, 1299 would be sorted as (using integer datatype) 1, 110, 200, 921, 1299, 2011 while it would be sorted as 1, 110, 1299, 200, 2011, 921 using string datatype.

I'm quite new to generics and java in general, and hope someone here might be able to help me out.

Basically: whenever I read a value from the database, I need to insert the value in the tableview with its actual datatype, rather than it being converted to string.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...