java自定义排序策略_JavaFX TableView排序策略

本文介绍了如何在 JavaFX 的 TableView 中实现自定义排序策略,确保特定行始终位于底部。通过设置 Comparator 和 sortPolicyProperty,可以实现当没有列被排序时保持原有顺序,或者根据列的排序规则进行相应排序,同时将特定对象(如总览行)置于列表底部。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于你的情况,你可以尝试这样的事情:

table.sortPolicyProperty().set(t -> {

Comparator comparator = (r1, r2)

-> r1 == rowTotal ? 1 //rowTotal at the bottom

: r2 == rowTotal ? -1 //rowTotal at the bottom

: t.getComparator() == null ? 0 //no column sorted: don't change order

: t.getComparator().compare(r1, r2); //columns are sorted: sort accordingly

FXCollections.sort(table.getItems(), comparator);

return true;

});

如果你不明白这里发生了什么,快照 without lambda expression :

table.sortPolicyProperty().set( new Callback, Boolean>() {

@Override

public Boolean call(TableView param) {

Comparator comparator = new Comparator() {

@Override

public int compare(SalesInvoiceNetSale r1, SalesInvoiceNetSale r2) {

if (r1 == rowTotal) {

return 1;

} else if (r2 == rowTotal) {

return -1;

} else if (param.getComparator() == null) {

return 0;

} else {

return param.getComparator().compare(r1, r2);

}

}

};

FXCollections.sort(table.getItems(), comparator);

return true;

}

});

Working Example

如果您仍有疑问,请找一个工作示例,其方案与您的类似,我创建了一个类 ExtraPerson ,它扩展了 Person 并将 ExtraPerson 的新对象作为页脚

import java.util.Comparator;

import javafx.application.Application;

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.TableColumn;

import javafx.scene.control.TableView;

import javafx.scene.control.cell.PropertyValueFactory;

import javafx.scene.layout.VBox;

import javafx.scene.text.Font;

import javafx.stage.Stage;

import javafx.util.Callback;

public class TableViewSampleWithoutEdit extends Application {

private TableView table = new TableView();

private ExtraPerson extraPerson = new ExtraPerson("Ninja Village");

private final ObservableList data = FXCollections

.observableArrayList(

new Person("Jacob", "Smith", "jacob.smith@example.com"),

new Person("Isabella", "Johnson",

"isabella.johnson@example.com"),

new Person("Ethan", "Williams",

"ethan.williams@example.com"),

new Person("Emma", "Jones", "emma.jones@example.com"),

new Person("Michael", "Brown", "michael.brown@example.com"),

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

TableColumn firstNameCol = new TableColumn("First Name");

firstNameCol.setMinWidth(100);

firstNameCol

.setCellValueFactory(new PropertyValueFactory(

"firstName"));

TableColumn lastNameCol = new TableColumn("Last Name");

lastNameCol.setMinWidth(100);

lastNameCol

.setCellValueFactory(new PropertyValueFactory(

"lastName"));

TableColumn emailCol = new TableColumn("Email");

emailCol.setMinWidth(200);

emailCol.setCellValueFactory(new PropertyValueFactory(

"email"));

/**

* Adding comparator to extraPerson

*/

table.sortPolicyProperty().set(

new Callback, Boolean>() {

@Override

public Boolean call(TableView param) {

Comparator comparator = new Comparator() {

@Override

public int compare(Person r1, Person r2) {

if (r1 == extraPerson) {

return 1;

} else if (r2 == extraPerson) {

return -1;

} else if (param.getComparator() == null) {

return 0;

} else {

return param.getComparator()

.compare(r1, r2);

}

}

};

FXCollections.sort(table.getItems(), comparator);

return true;

}

});

table.setItems(data);

table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

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

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值