带有地图的JavaFX TableView对象 [英] JavaFX TableView Objects with Maps
问题描述
所以我在JavaFx TableView上做了一些挖掘,我找到了一些很好的解决方案来处理简单的情况。
So I have done some digging around on the JavaFx TableView and I have found some nice solutions to simple situations.
此文章提供了如何使用Person对象创建表的一个很好的解释,并且还展示了如何使用Map创建表。
This article provides a nice explanation of how to create a Table with a Person object and also shows how to create a Table using a Map.
这是我的问题,假设我有一个Object Person,其中包含一些简单的成员数据以及一个分配给Grades的地图。
Here is my problem, let's say I have an Object Person that contains some simple member data and also a Map of Assignments to Grades.
示例:
public class Person {
String firstName;
String lastName;
String age;
Map<Assignment, Grade> map;
}
我想显示如下表格
firstName | lastName | age | Assignment1 | Assignment 2 | Assignment 3 | ...
如果我像这样定义一个表:
If I define a Table like so:
private TableView<Student> table;
很容易定义简单的列,如:
It is easy to define simple columns like:
private TableColumn<Student, String> firstNameColumn =
firstNameColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
但我该如何为地图中的每个键定义列?
But how should I go about defining columns for each of the keys in the map?
这是可能的,还是我应该创建另一个处理地图的表?
Is this even possible or should I just create another table that handles the map?
推荐答案
您不必使用默认的PropertyValueFactory,您可以编写自己的回调。
You don't have to use the default PropertyValueFactory, you can write your own callback.
import java.util.HashMap;
import java.util.LinkedHashMap;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class AssTable extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<Student> students = FXCollections.observableArrayList(
new Student("jack"),new Student("john"),new Student("jill"),new Student("jane"));
TableView<Student> studentTable = new TableView(students);
TableColumn<Student, String> firstNameColumn = new TableColumn("name");
firstNameColumn.setCellValueFactory(new PropertyValueFactory("firstName"));
studentTable.getColumns().add(firstNameColumn);
int maxAss = 0;
for (Student student : students)
maxAss = Math.max(maxAss, student.map.size());
Callback<TableColumn.CellDataFeatures<Student, String>, ObservableValue<String>> callBack =
new Callback<TableColumn.CellDataFeatures<Student, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Student, String> param) {
return param.getValue().map.containsKey(
"ass"+Integer.toString((int)param.getTableColumn().getUserData()))
? new SimpleStringProperty(String.format("%.1f",100d*param.getValue().map.get(
"ass"+Integer.toString((int)param.getTableColumn().getUserData()))))
:new SimpleStringProperty("");
}
};
ObservableList<TableColumn<Student, String>> assCols = FXCollections.observableArrayList();
for (int i = 1; i<=maxAss; i++){
TableColumn<Student, String> tmpCol = new TableColumn("ass"+Integer.toString(i));
tmpCol.setUserData(i);
tmpCol.setCellValueFactory(callBack);
assCols.add(tmpCol);
}
studentTable.getColumns().addAll(assCols);
VBox root = new VBox(studentTable);
Scene scene = new Scene(root, 500, 250);
primaryStage.setTitle("Table with map");
primaryStage.setScene(scene);
primaryStage.show();
}
public class Student {
private final StringProperty firstName = new SimpleStringProperty();
public StringProperty firstNameProperty(){return firstName;}
public final HashMap<String, Double> map;
public Student(String fn) {
firstName.set(fn);
map = new LinkedHashMap<>();
for (int i = 1; i <= 10; i++) {
double grade = Math.random();
if (grade > .5) {
map.put("ass" + Integer.toString(i), grade);
}
}
}
}
}
你可以看到它根据有多少分配添加列。此随机样本中也没有人做过ass4。使用此代码和示例,您无法添加#8之类的分配而不添加新列,或者它不会显示。
You can see it adds columns depending on how many assignments there are. Also nobody has done ass4 in this random sample. With this code and example you can't add an assignment like #8 without also adding a new column, or it won't show up.
这篇关于带有地图的JavaFX TableView对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!