如何在 JavaFX 中用 ObservableMap<KeyObject, ObservableList<CustomObject>> 填充 TableView
How to fill TableView with ObservableMap<KeyObject, ObservableList<CustomObject>> in JavaFX
我的 Java 程序产生大量数据,我用这些数据构建了单独的 ResultObject。因为只有某些 ResultsObjects 会感兴趣,所以我用我的结果填充了一个 ObservableHashMap<>。 CustomObject 由一些 int + double 和一个 JSONObject 组成。有了这个 CustomObject,我想将类似的 ResultObjects(具有某些共同的属性)映射到那个 CustomObject。
虽然映射和处理这些结果按预期工作,但我几乎无法用 ObservableHashMap<> 填充 TableView。
我的 CustomObject(JSONObject 只是为了检查,如果两个 CustomObjects 具有相同的属性):
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 的各个映射 ObservableLists 显示在单独的 TableView 中。
我是 Java 和 JavaFX 的新手,我希望尽可能专业地描述我的问题。如果我遗漏了什么,请告诉我。
这应该适用于 List<Map<String,String>>
Main.java
package tester;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
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)
{
TableView<LineItem> table = new TableView<>();
List<Map<String, String>> databaseObject = getDatabaseObject();
for (String key : databaseObject.get(0).keySet())
{
TableColumn<LineItem, String> col = new TableColumn<>(key);
col.setCellValueFactory((TableColumn.CellDataFeatures<LineItem, String> cellData) -> cellData.getValue().fieldProperty(key));
table.getColumns().add(col);
}
List<LineItem> data = new ArrayList<>();
LineItem sequence1 = new LineItem(databaseObject.get(0));
LineItem sequence2 = new LineItem(databaseObject.get(1));
data.add(sequence1);
data.add(sequence2);
table.setItems(FXCollections.observableArrayList(data));
table.setPrefWidth(500);
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 List<Map<String, String>> getDatabaseObject()
{
List<Map<String, String>> values = new ArrayList<>();
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");
values.add(record1);
Map<String, String> record2 = new HashMap<>();
record2.put("firstName", "Sarah");
record2.put("lastName", "Blob");
record2.put("address", "92 main st");
record2.put("city", "London");
record2.put("postalCode", "T9Y4K4");
values.add(record2);
return values;
}
}
LineItem.java
package tester;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class LineItem
{
private final Map<String, StringProperty> fields;
public LineItem(Map<String, String> record)
{
this.fields = new HashMap<>();
for (String key : record.keySet())
{
this.fields.put(key, new SimpleStringProperty());
}
for (Entry<String, String> entry : ((Set<Entry<String, String>>) record.entrySet()))
{
this.fields.get(entry.getKey()).set(entry.getValue());
}
}
public final StringProperty fieldProperty(String key)
{
return this.fields.get(key);
}
}
用 Map<String,String>
创建 TableView
的另一种方法是:
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;
}
}
我的 Java 程序产生大量数据,我用这些数据构建了单独的 ResultObject。因为只有某些 ResultsObjects 会感兴趣,所以我用我的结果填充了一个 ObservableHashMap<>。 CustomObject 由一些 int + double 和一个 JSONObject 组成。有了这个 CustomObject,我想将类似的 ResultObjects(具有某些共同的属性)映射到那个 CustomObject。
虽然映射和处理这些结果按预期工作,但我几乎无法用 ObservableHashMap<> 填充 TableView。
我的 CustomObject(JSONObject 只是为了检查,如果两个 CustomObjects 具有相同的属性):
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 的各个映射 ObservableLists 显示在单独的 TableView 中。
我是 Java 和 JavaFX 的新手,我希望尽可能专业地描述我的问题。如果我遗漏了什么,请告诉我。
这应该适用于 List<Map<String,String>>
Main.java
package tester;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
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)
{
TableView<LineItem> table = new TableView<>();
List<Map<String, String>> databaseObject = getDatabaseObject();
for (String key : databaseObject.get(0).keySet())
{
TableColumn<LineItem, String> col = new TableColumn<>(key);
col.setCellValueFactory((TableColumn.CellDataFeatures<LineItem, String> cellData) -> cellData.getValue().fieldProperty(key));
table.getColumns().add(col);
}
List<LineItem> data = new ArrayList<>();
LineItem sequence1 = new LineItem(databaseObject.get(0));
LineItem sequence2 = new LineItem(databaseObject.get(1));
data.add(sequence1);
data.add(sequence2);
table.setItems(FXCollections.observableArrayList(data));
table.setPrefWidth(500);
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 List<Map<String, String>> getDatabaseObject()
{
List<Map<String, String>> values = new ArrayList<>();
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");
values.add(record1);
Map<String, String> record2 = new HashMap<>();
record2.put("firstName", "Sarah");
record2.put("lastName", "Blob");
record2.put("address", "92 main st");
record2.put("city", "London");
record2.put("postalCode", "T9Y4K4");
values.add(record2);
return values;
}
}
LineItem.java
package tester;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class LineItem
{
private final Map<String, StringProperty> fields;
public LineItem(Map<String, String> record)
{
this.fields = new HashMap<>();
for (String key : record.keySet())
{
this.fields.put(key, new SimpleStringProperty());
}
for (Entry<String, String> entry : ((Set<Entry<String, String>>) record.entrySet()))
{
this.fields.get(entry.getKey()).set(entry.getValue());
}
}
public final StringProperty fieldProperty(String key)
{
return this.fields.get(key);
}
}
用 Map<String,String>
创建 TableView
的另一种方法是:
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;
}
}