有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在JavaFX中用ObservableMap<KeyObject,ObservableList<CustomObject>>填充TableView

我的Java程序生成了大量数据,我用这些数据构造了单独的ResultObject。因为只有特定的ResultsObjects会引起兴趣,所以我填充了一个ObservableHashMap<>;和我的结果。CustomObject由几个int+double和一个JSONObject组成。有了这个CustomObject,我想把类似的ResultObject(它们有一些共同的属性)映射到这个CustomObject

虽然映射和处理这些结果可以按预期的方式工作,但我很难用ObservableHashMap填充TableView<>

My CustomObject(如果两个CustomObject具有相同的属性,只需检查JSONObject):

public CustomObject(Simulation data) {

    this.datapoint1 = data.datapoint1;
    this.datapoint2 = data.datapoint2;

    this.jsonCompareObject = new JSONObject()
            .put("datapoint1", data.datapoint1)
            .put("datapoint1", data.datapoint2);
}

我的ObservableHashMap对象:

private ObservableMap <CustomObject, ObservableList<Simulation>> topResultsList;

public SomeObjectWithObservableMap(){
    this.topResultsList = FXCollections.observableHashMap();
}

通过使用下面的代码,我检查是否有一个已经存在相应数据点的键,然后将其添加到ObservableList(值)中:

private boolean isKeyPresent(CustomObject newCustomObject, Simulation data) {
    for (CustomObject oldCustomObject : this.topResultsList.keySet()) {
        if (oldCustomObject.jsonCompareObject.toString().equals(newCustomObject.jsonCompareObject.toString())) {
            this.topResultsList.get(oldCustomObject).add(data);
            return true;
        }
    }
    return false;
}

我填充了其他一些TableView,如下所示:

{
    tableColumn.setCellValueFactory(new PropertyValueFactory<>("datapoint1"));
    tableColumn.setCellFactory(TextFieldTableCell.<SomeObject, Double>forTableColumn(twoDigits));
    tableView.setItems(someObject.getObservableList());
}

最后,我想要一些TableColumns来显示CustomObject的属性。稍后,我希望在单独的表视图中显示选定CustomObject的单个映射ObservableList

我对Java和JavaFX非常陌生,我希望尽可能专业地描述我的问题。如果我错过了什么,请告诉我


共 (1) 个答案

  1. # 1 楼答案

    用一个Map<String,String>创建TableView的另一种方法是:

    enter image description here

    Main。java

    package tester;
    
    import java.util.HashMap;
    import java.util.Map;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.TableView;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class Tester extends Application
    {
    
        @Override
        public void start(Stage primaryStage)
        {
            MapTable mapTable = new MapTable(getDatabaseRecord());
    
            TableView table = mapTable.getTableView();
    
            HBox root = new HBox();
            root.getChildren().addAll(table);
    
            Scene scene = new Scene(root, 500, 500);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            launch(args);
        }
    
        private Map<String, String> getDatabaseRecord()
        {
            Map<String, String> record1 = new HashMap<>();
    
            record1.put("firstName", "Joe");
            record1.put("lastName", "Blob");
            record1.put("address", "95 circle ave");
            record1.put("city", "Toronto");
            record1.put("postalCode", "L9Y4Z4");
    
            return record1;
        }
    }
    

    映射表。java

    package tester;
    
    import java.util.Map;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.control.SelectionMode;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    
    public class MapTable
    {
    
        private final TableView<Map.Entry<String, String>> table;
        private final ObservableList<Map.Entry<String, String>> data;
    
        public MapTable(Map map)
        {
    
            this.table = new TableView<>();
            this.data = FXCollections.observableArrayList(map.entrySet());
            setUpTable();
        }
    
        private void setUpTable()
        {
            this.table.setEditable(false);
            this.table.setItems(this.data);
            this.table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
            // use fully detailed type for Map.Entry<String, String> 
            TableColumn<Map.Entry<String, String>, String> column1 = new TableColumn<Map.Entry<String, String>, String>();
            column1.setMinWidth(125);
            column1.setSortable(false);
            column1.setCellValueFactory((TableColumn.CellDataFeatures<Map.Entry<String, String>, String> p) -> new SimpleStringProperty(p.getValue().getKey()));
    
            TableColumn<Map.Entry<String, String>, String> column2 = new TableColumn<Map.Entry<String, String>, String>();
            column2.setMinWidth(350);
            column2.setSortable(false);
            column2.setCellValueFactory((TableColumn.CellDataFeatures<Map.Entry<String, String>, String> p) -> new SimpleStringProperty(p.getValue().getValue()));
    
    
            this.table.getColumns().setAll(column1, column2);
    
        }
    
        public TableView getTableView()
        {
            return this.table;
        }
    }