Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

java - How to fill TableView with ObservableMap<KeyObject, ObservableList<CustomObject>> in JavaFX

My Java programm produces a lot of data, with wich I construct individual ResultObject. Because only certain ResultsObjects will be of interest, I populate an ObservableHashMap<> with my results. The CustomObject consists of a handful ints + doubles and one JSONObject. With this CustomObject I want to map similar ResultObjects(which have certain properties in common) to that one CustomObject.

While mapping and handling those results works as intended, I am pretty much helpless to populate a TableView with that ObservableHashMap<>.

My CustomObject (JSONObject simply to check, if two CustomObjects have the same properties):

public CustomObject(Simulation data) {

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

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

My ObservableHashMap Object:

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

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

By using the following code, I check if there is a key with the corresponding datapoints already present to then add to its ObservableList (Value):

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

I populate some other TableView like this:

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

In the end I want some TableColumns to show properties from the CustomObject. Later on, I would want the individual mapped ObservableLists of a selected CustomObject to be shown in a seperate TableView.

I am pretty new to Java and JavaFX and I hope to have described my problem as professional as possible. Please let me know, if I missed anything.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The other way to create a TableView with one Map<String,String> is:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...