且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据添加到列的简单方法

更新时间:2023-11-28 21:37:40

我强烈建议您重新组织数据,以便拥有一个包含两个属性的类,其中一个是您的元素第一个列表,另一个是第二个列表中的相应元素。然后你可以列出该类的对象列表,并且你可以减少到通常的情况。

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.

例如。假设您有

// your first list:
List<Integer> intList = ... ;
// your second list:
List<String> stringList = ... ;

然后定义

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 ;
    }
}

现在你可以做到

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)));
}

并按常规方式定义列。

如果由于某种原因确实无法进行数据转换,那么可以将表的类型视为整数,每行的值是两个列表的索引。使用值 0,1,... 填充表,最大为列表大小。这看起来像:

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);
    }
}