Easy way to add data to columns

I want to insert two separate datasets into columns in a JavaFX TableView. Basically, I have 2 LinkedLists with rows, and I want to put one list in one column and the second a second.

What is the easiest way to do this? Or is this another JavaFX element?

Only the solutions that I have found so far were based on collections that were inserted into strings, which I do not want to do.

+5
source share
2 answers

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 this class and come down to the usual case.

eg. 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 cannot do this data conversion for any reason, then you can consider the table type as an integer, with the value for each row being an index in two lists. Fill the table with the values 0, 1,... to the size of the lists. It looks like this:

 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); } } 
+9
source

I found a JavaFX tutorial that seems to have solutions for collecting collections and populating columns based on them, and also automatically updates the table as collections change using an ObservableList.

http://code.makery.ch/library/javafx-8-tutorial/part2/

Their code has a list of objects with two attributes, you would like to change their code to draw from your two separate lists instead of accessing the two variables of their objects for individual columns.

0
source

All Articles