I want to insert two separate datasets to columns in JavaFX TableView. Basically I have 2 LinkedLists with Strings and I want to put one list in one column and another to second one.
What is the easiest way to do this? Or is another JavaFX element better for this?
Only solutions I found so far were based on collections that were inserted into rows, which is not what I want to do.
I would strongly recommend reorganizing your data, so that you have a class with two properties, one of which is an element from your first list, and the other of which is the corresponding element from your second list. Then you can make a list of objects of that class, and you are reduced to the usual case.
E.g. suppose you have
// your first list:
List<Integer> intList = ... ;
// your second list:
List<String> stringList = ... ;
Then define
public class MyDataType {
private final int intValue ;
private final String stringValue ;
public MyDataType(int intValue, String stringValue) {
this.intValue = intValue ;
this.stringValue = stringValue ;
}
public int getIntValue() {
return intValue ;
}
public String getStringValue() {
return stringValue ;
}
}
Now you can do
TableView<MyDataType> table = new TableView<>();
for (int i = 0; i < intList.size() && i < stringList.size(); i++) {
table.getItems().add(new MyDataType(intList.get(i), stringList.get(i)));
}
and define the columns in the usual way.
If you really can't make that data transformation for some reason, then you can regard the type of the table as an integer, with the value for each row being the index into the two lists. Populate the table with the values 0, 1,...
up to the size of the lists. This looks like:
import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TableViewByColumns extends Application {
@Override
public void start(Stage primaryStage) {
List<Integer> intValues = Arrays.asList(1, 2, 3, 4, 5);
List<String> stringValues = Arrays.asList("One", "Two", "Three", "Four", "Five");
TableView<Integer> table = new TableView<>();
for (int i = 0; i < intValues.size() && i < stringValues.size(); i++) {
table.getItems().add(i);
}
TableColumn<Integer, Number> intColumn = new TableColumn<>("Value");
intColumn.setCellValueFactory(cellData -> {
Integer rowIndex = cellData.getValue();
return new ReadOnlyIntegerWrapper(intValues.get(rowIndex));
});
TableColumn<Integer, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(cellData -> {
Integer rowIndex = cellData.getValue();
return new ReadOnlyStringWrapper(stringValues.get(rowIndex));
});
table.getColumns().add(intColumn);
table.getColumns().add(nameColumn);
primaryStage.setScene(new Scene(new BorderPane(table), 600, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With