JavaFX-TreeTableView sortpolicProperty()
我的问题是如何使用我自己的JavaFX-TreeTableView sortpolicProperty(),java,javafx,javafx-8,comparator,Java,Javafx,Javafx 8,Comparator,我的问题是如何使用我自己的比较器创建treetableview(JavaFX8) 我发现了一些类似的示例,但它们都是TableView而不是TreeTableView 谢谢 下面是我的示例代码: import java.util.Comparator; import javafx.application.Application; import javafx.application.Platform; import javafx.beans.property.ReadOnlyObjectW
比较器创建treetableview(JavaFX8)
我发现了一些类似的示例,但它们都是TableView
而不是TreeTableView
谢谢
下面是我的示例代码:
import java.util.Comparator;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableViewSampleWithoutEdit extends Application {
private TreeTableView<Person> table = new TreeTableView<Person>();
private ExtraPerson extraPerson = new ExtraPerson("Ninja Village");
TreeItem root = new TreeItem<>("root");
private final ObservableList<TreeItem<Person>> data = FXCollections
.observableArrayList(
new TreeItem<Person>( new Person("Jacob", "Smith", "jacob.smith@example.com")),
new TreeItem<Person>( new Person("Isabella", "Johnson","isabella.johnson@example.com")),
new TreeItem<Person>( new Person("Ethan", "Williams","ethan.williams@example.com")),
new TreeItem<Person>( new Person("Emma", "Jones", "emma.jones@example.com")),
new TreeItem<Person>( new Person("Michael", "Brown", "michael.brown@example.com")),
new TreeItem<Person>( extraPerson));
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
Platform.runLater(() -> {
TreeTableColumn<Person, String> firstNameCol = new TreeTableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(cellData -> {
if (cellData.getValue().getValue()instanceof Person) {
return new ReadOnlyObjectWrapper(cellData.getValue().getValue().getFirstName());
}
return new ReadOnlyObjectWrapper(cellData.getValue().getValue());
});
TreeTableColumn<Person, String> lastNameCol = new TreeTableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(cellData -> {
if (cellData.getValue().getValue()instanceof Person) {
return new ReadOnlyObjectWrapper(cellData.getValue().getValue().getLastName());
}
return new ReadOnlyObjectWrapper(cellData.getValue().getValue());
});
/**
* Adding comparator to extraPerson
*/
table.sortPolicyProperty().set(
new Callback<TreeTableView<Person>, Boolean>() {
@Override
public Boolean call(final TreeTableView<Person> param) {
Comparator<TreeItem<Person>> comparator = new Comparator<TreeItem<Person>>() {
@Override
public int compare(TreeItem<Person> r1, TreeItem<Person> r2) {
if (r1.getValue() == extraPerson) {
return 1;
} else if (r2.getValue() == extraPerson) {
return -1;
} else if (param.getComparator() == null) {
return 0;
} else {System.out.println("c");
return param.getComparator()
.compare(r1, r2);
}
}
};
ObservableList<TreeItem<Person>> tables = FXCollections.observableArrayList();
for (int i = 0; i < table.getExpandedItemCount(); i++) {
tables.add(table.getTreeItem(0));
}
FXCollections.sort(tables,comparator);
if (tables.size()>0) {
table.getRoot().getChildren().setAll(tables);
}
return true;
}
});
root.getChildren().setAll(data);
table.setRoot(root);
table.getColumns().addAll(firstNameCol, lastNameCol);
root.setExpanded(true);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
}
public static class ExtraPerson extends Person {
private final SimpleStringProperty address;
private ExtraPerson(String address) {
super("Itachi", "Uchiha", "leaf@village.ninja");
this.address = new SimpleStringProperty(address);
}
public String getAddress() {
return address.get();
}
public void setAddress(String address) {
this.address.set(address);
}
}
}
import java.util.Comparator;
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.beans.property.ReadOnlyObjectWrapper;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.geometry.Insets;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.TreeItem;
导入javafx.scene.control.TreeTableColumn;
导入javafx.scene.control.TreeTableView;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.Font;
导入javafx.stage.stage;
导入javafx.util.Callback;
公共类TableViewSampleWithoutEdit扩展了应用程序{
private TreeTableView table=new TreeTableView();
私人外人外人=新外人(“忍者村”);
TreeItem root=新的TreeItem(“根”);
私有最终可观察列表数据=FXCollections
.可观察的可追踪名单(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com")),
新人(“伊莎贝拉”、“约翰逊”、“伊莎贝拉”。johnson@example.com")),
新人(“伊桑”、“威廉姆斯”、“伊桑”。williams@example.com")),
新的树(新的人(“爱玛”,“琼斯”,“爱玛”)。jones@example.com")),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com")),
新的TreeItem(外部人员);
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公众假期开始(阶段){
场景=新场景(新组());
stage.setTitle(“表格视图示例”);
舞台布景宽度(450);
舞台设置高度(500);
最终标签=新标签(“地址簿”);
label.setFont(新字体(“Arial”,20));
table.setEditable(true);
Platform.runLater(()->{
TreeTableColumn firstNameCol=新的TreeTableColumn(“名字”);
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(cellData->{
if(cellData.getValue().getValue()Person实例){
返回新的ReadOnlyObjectWrapper(cellData.getValue().getValue().getFirstName());
}
返回新的ReadOnlyObjectWrapper(cellData.getValue().getValue());
});
TreeTableColumn lastNameCol=新的TreeTableColumn(“姓氏”);
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(cellData->{
if(cellData.getValue().getValue()Person实例){
返回新的ReadOnlyObjectWrapper(cellData.getValue().getValue().getLastName());
}
返回新的ReadOnlyObjectWrapper(cellData.getValue().getValue());
});
/**
*向extraPerson添加比较器
*/
table.sortpolicProperty().set(
新回调函数(){
@凌驾
公共布尔调用(最终TreeTableView参数){
比较器比较器=新比较器(){
@凌驾
公共整数比较(树项r1,树项r2){
if(r1.getValue()==外部人员){
返回1;
}else if(r2.getValue()==外部人员){
返回-1;
}else if(param.getComparator()==null){
返回0;
}else{System.out.println(“c”);
返回参数getComparator()
.比较(r1,r2);
}
}
};
ObservableList tables=FXCollections.observableArrayList();
对于(int i=0;i0){
table.getRoot().getChildren().setAll(表);
}
返回true;
}
});
root.getChildren().setAll(数据);
表.setRoot(root);
table.getColumns().addAll(firstNameCol,lastNameCol);
root.setExpanded(true);
最终VBox VBox=新的VBox();
vbox.setspace(5);
设置填充(新的插入(10,0,0,10));
vbox.getChildren().addAll(标签、表格);
((组)scene.getRoot()).getChildren().addAll(vbox);
舞台场景;
stage.show();
}
公共静态类人员{
私有最终SimpleStringProperty名字;
私有最终SimpleStringProperty姓氏;
私人最终SimpleStringProperty电子邮件;
私人(字符串fName、字符串lName、字符串电子邮件){
this.firstName=新的SimpleStringProperty(fName);
this.lastName=新的SimpleStringProperty(lName);
this.email=新的SimpleStringProperty(电子邮件);
}
公共字符串getFirstName(){
返回firstName.get();
}
public void setFirstName(字符串fName){
firstName.set(fName);
}
公共字符串getLastName(){
返回lastName.get();
}
public void setLastName(字符串fName){
lastName.set(fName);
}
公共字符串getEmail(){
返回email.get();
}
public void setEmail(字符串fName){
email.set(fName);
}
}
公共静态类ExtraPerson扩展Person{
私人最终SimpleStringProperty地址;
私有外部人员(字符串地址){
超级(“Itachi”,“Uchiha”leaf@village.ninja");
this.address=新的SimpleStringProperty(地址);
}
P
ObservableList<TreeItem<Person>> tables = FXCollections.observableArrayList();
for (int i = 0; i < table.getExpandedItemCount(); i++) {
tables.add(table.getTreeItem(0));
}
FXCollections.sort(tables,comparator);
if (tables.size()>0) {
table.getRoot().getChildren().setAll(tables);
}
if(table.getRoot() != null)
FXCollections.sort(table.getRoot().getChildren(), comparator);