'JavaFX TableView how to add items to column?

I've working on a GUI that the user can browse text files from the SYSTEM and then when the user press "Start" button the program reading the text file/s, create lists from its data and supposed to add it to TableView. I'm stuck on inserting the data from the lists to the table. I've created the columns names by file names and added it to table:

tblConfigurationSystemColumns.add("Parameter Name");
tblSystemColumn.stream().map((str) -> str.split("PCM")).forEachOrdered((a) -> {
tblConfigurationSystemColumns.add(a[0].trim());
        });
for (int i = 0; i < tblConfigurationSystemColumns.size(); i++) {
    TableColumn col = new TableColumn(tblConfigurationSystemColumns.get(i));
    tableConfigurationSystem.getColumns().addAll(col);        
}

The column names coming from the list tblConfigurationSystemColumns. This list may be changed from each use of the GUI by number of file you browse from the system. (for now let think that we have 2 strings inside: "column1","column2")

I need to add items to column1 from the list SysParameter , and to column2 from list SysValues.

How can I add values from each list to each column by rows? If you need any more code please tell me (just let you know, the only code that I have it the list creating from the files).

EDIT: enter image description here

This is what I got after the column building. after this I need to get the "Parameter" and the "Value" for each column(as you can see). I've made a list that get the "Parameter" from the text file, and another list that get the "Value" from the text file.

how can I put each list to it's column? This is the code that build this lists:

            boolean inCESystem = false;
        for (final String line : list) {
    if (line.contains("CE-") && !(line.contains("CE-system-equipment-pm") || line.contains("inbound") || line.contains("outbound"))) {
        inCESystem = true;
    }
    else if (line.trim().isEmpty()) {
        inCESystem = false;
    }
    else if (inCESystem) {
        CE_System.add(line);
    }
        }
        boolean inCESystemInbound = false;
        for (final String line : list) {
    if (line.contains("CE-") && (line.contains("inbound")) ) {
        inCESystemInbound = true;
    }
    else if (line.trim().isEmpty()) {
        inCESystemInbound = false;
    }
    else if (inCESystemInbound) {
        CE_System.add("inbound_loadlock - "+line.trim());
    }
        }
        boolean inCESystemOutbound = false;
        for (final String line : list) {
    if (line.contains("CE-") && (line.contains("outbound")) ) {
        inCESystemOutbound = true;
    }
    else if (line.trim().isEmpty()) {
        inCESystemOutbound = false;
    }
    else if (inCESystemOutbound) {
        CE_System.add("outbound_loadlock - "+line.trim());
    }
        }            
        /*
         * Check the CE list to split each object per parameter and value to different lists
         */
        CE_System.stream().map((str) -> str.split(",")).map((a) -> {
            CE_SystemParameter.add(a[0].trim()); //Parameters
            return a;
        }).forEachOrdered((a) -> {
            if(a.length > 1) {
                CE_System_Value.add(a[1].trim()); //Values
            } else {
                CE_System_Value.add(""); //add blank if parameter doesn't have value
            } 
        });

EDIT 2: Text file example

CE-system:
   No features to set for this item...

CE-system-componentmanager:
   Bootstrap Parallelism                             ,Parallel Bootstrapping

CE-system-components:
   No features to set for this item...

CE-system-components-accessmanager:
   Access control enable                             ,disabled
   Access policy prototyping                         ,enabled
   Access user group                                 ,enabled
   Implicit roles access policy                      ,disabled
   World access policy                               ,disabled

CE-system-components-eqlog:
   EquipmentLog Enable                               ,false
  • Line that contains "CE-" its just title to know that is should be in the "Configuration" Tab.
  • each line inside is the "parameter" and the value(after the comma).

EDIT 3: The table should look like this example (This example is from my code in Java SWT) enter image description here

Thank you very much guys.



Solution 1:[1]

The data for a TableView is held in the ObservableList of the items property. A TableView is designed to hold a list of POJOs that contain various properties. Each of the properties will correspond to a TableColumn who obtains the value of these properties using a Callback.

Since you are browsing text files let's say you define a POJO like so:

import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.property.SimpleStringProperty;

public class TextFile {

    private final StringProperty name = new SimpleStringProperty(this, "name");
    public final void setName(String name) { this.name.set(name); }
    public final String getName() { return name.get(); }
    public final StringProperty nameProperty() { return name; }

    private final LongProperty size = new SimpleLongProperty(this, "size");
    public final void setSize(long size) { this.size.set(size); }
    public final long getSize() { return size.get(); }
    public final LongProperty sizeProperty() { return size; }

    public TextFile() {}

    public TextFile(String name, long size) {
        setName(name);
        setSize(size);
    }

}

From this you'll want a TableView of TextFiles that has a TableColumn for name and a TableColumn for size. To tell a TableColumn how to obtain the correct value you set the cellValueFactory with the appropriate Callback. This Callback accepts a TableColumn.CellDataFeatures and returns an ObservableValue. If the ObservableValue changes the TableColumn will update the item of the corresponding TableCell.

ObservableList<TextFile> files = ...;
TableView<TextFile> table = new TableView<>();
table.setItems(files);

TableColumn<TextFile, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(features -> features.getValue().nameProperty());
table.getColumns().add(nameCol);

TableColumn<TextFile, Number> sizeCol = new TableColumn<>("Size");
sizeCol.setCellValueFactory(features -> features.getValue().sizeProperty());
table.getColumns().add(sizeCol);

Note that each TextFile in files is a row in the TableView.

Solution 2:[2]

I guess you are looking for something like that:

TableColumn< YourObject, String> col = new TableColumn<>();
col.setCellValueFactory(new PropertyValueFactory("nameOfThePropertyYouWantToDisplay");
TableColumn< YourObject, String> col2 ....

TableView < YourObject> table = new TableView();
table.setItems(observableListOfYourObject);

Look here for a detailed description: https://docs.oracle.com/javafx/2/ui_controls/table-view.htm

Solution 3:[3]

Choose the item type accordingly. Your description indicates the following properties:

  • The table data is not edited once it's loaded.
  • You cannot hardcode the number of files.

Therefore a suitable choice of data structure would be List<String>. Each list contains one element for every column.

public void initializeTableColumns(TableView<List<String>> table, File file, File... files) {
    List<String> fileItems = readFile(file);
    TableColumn<List<String>, String> column = new TableColumn<>(file.getName());
    column.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().get(0));
    table.getColumns().add(column);

    for (String s : fileItems) {
        List<String> row = new ArrayList<>(files.length + 1);
        row.add(s);
        table.getItems().add(row);
    }

    for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
        File f = files[fileIndex];

        fileItems = readFile(f);
        int itemCount = Math.min(fileItems.size(), table.getItems().size());
        // add items from file
        for (int i = 0; i < itemCount; i++) {
            table.getItems().get(i).add(fileItems.get(i));
        }
        if (itemCount <= table.getItems.size()) {
            // fill items that may be missing
            for (int i = itemCount; i < table.getItems().size(); i++) {
                table.getItems().get(i).add(null);
            }
        } else {
            // add missing rows
            for (int i = table.getItems.size(); i < itemCount; i++) {
                List<String> row = new ArrayList<>(files.length + 1);
                for (int j = 0; j <= fileIndex; j++) {
                    row.add(null);
                }
                row.add(fileItems.get(i));
                table.getItems().add(row);
            }
        }

        final index = fileIndex + 1;
        column = new TableColumn<>(f.getName());
        column.setTableColumn(cd -> new SimpleStringProperty(cd.getValue().get(index)));
        table.getColumns().add(column);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Slaw
Solution 2 fabian
Solution 3 fabian