0

I have never used tableview before and I am quite new to with Java and JavaFX. I tried to mirror an example but since the data is coming from a db client created in house I couldn't copy it exactly. Anyway, my data is going into the table but its entering as what looks like a csv data and each column is not getting put into its respective column. Here is a screenshot to clarify my question:

enter image description here

Getting columns here

   for (Column col : drs.getColumns()){
        TableColumn tblCol = new TableColumn(col.getName());
        tblCol.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
            public ObservableValue<String> call(CellDataFeatures<ObservableList, String> p) {
                return new SimpleStringProperty(p.getValue().toString());
            }
        });
         table.getColumns().addAll(tblCol);
    }

Getting the rows here:

while (drs.hasNextRow()) {
    ObservableList<String> row = FXCollections.observableArrayList();
    for (int i = 0; i < drs.getColumns().length; i++) {
        row.add(drs.getNextRow().getItem(i) + "");
    }
    data.add(row);
}

And here is where I fail at sending the data to the table.

table.setItems(data);
System.out.println("dis is data: " +data);

the data prints like so:

dis is data: [[data, data, data....], [data, data, data....], [data, data, data....]]

It takes each [row] and puts it into each column for each record. I am assuming I need the data to look like:

[[data], [data], [data]....[data]], [[data], [data], [data]....[data]], [[data], [data], [data]....[data]]
    } catch(Exception e){
        e.printStackTrace();
        System.out.println("everything is broken");
    }
}

So, I am lost. I don't know what I am doing and I'd appreciate any help that you can offer me.

1 Answer 1

1

In TableView all the data in a row is associated with a item. The TableColumn.cellValueFactory is used to select the "part" of a item that should be shown in the column. Therefore you should use it to select the value:

TableView<ObservableList<String>> table = ...
int index = 0;
for (Column col : drs.getColumns()) {
    final int columnIndex = index++;
    TableColumn<ObservableList<String>, String> tblCol = new TableColumn(col.getName());
    tblCol.setCellValueFactory(new Callback<CellDataFeatures<ObservableList<String>, String>, ObservableValue<String>>(){
        public ObservableValue<String> call(CellDataFeatures<ObservableList, String> p) {
            return Bindings.stringValueAt(p.getValue(), columnIndex);
        }
    });
    table.getColumns().add(tblCol);
}

Here the Bindings.stringValueAt is used to select the element form the ObservableList.

Also you need to use one row per row:

while (drs.hasNextRow()) {
    ObservableList<String> row = FXCollections.observableArrayList();
    Row sourceRow = drs.getNextRow();
    for (int i = 0; i < drs.getColumns().length; i++) {
        row.add(Objects.toString(sourceRow.getItem(i)));
    }
    data.add(row);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! I got it working however now when I enter another query it appends the new query to the right of the old one in the table.
@anshanno: I don't know what your input data structure is, but when in doubt you can clear the items and columns of the TableView before setting new data...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.