JavaFX TableView自訂義排序-最後一列資料不納入排序判斷
package funcion;
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;
/**200921-GDK-tableview自訂義排序範例-數字格式化排序*/
public class TableViewLastColumNotSort extends Application {
private TableView<Person> table = new TableView<Person>();
private ExtraPerson extraPerson = new ExtraPerson(“Ninja Village”);
private final ObservableList<Person> data = FXCollections
.observableArrayList(
new Person(“Jacob”, “1,111”, “jacob.smith@example.com”),
new Person(“Isabella”, “1,123”,
“isabella.johnson@example.com”),
new Person(“Ethan”, “111”,
“ethan.williams@example.com”),
new Person(“Emma”, “222,222”, “emma.jones@example.com”),
new Person(“Michael”, “312”, “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<Person, String>(
“firstName”));
TableColumn moneyCol = new TableColumn(“Money”);
moneyCol.setMinWidth(100);
moneyCol
.setCellValueFactory(new PropertyValueFactory<Person, String>(
“Money”));
TableColumn emailCol = new TableColumn(“Email”);
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>(
“email”));
/**
* Adding comparator to extraPerson
* 覆寫sort判斷
*/
table.sortPolicyProperty().set(
new Callback<TableView<Person>, Boolean>() {
/*自動生成*/
@Override
public Boolean call(TableView<Person> param) {
Comparator<Person> comparator = new Comparator<Person>() {
@Override
public int compare(Person r1, Person r2) {
if (r1 == table.getItems().get(table.getItems().size()-1)) {
/* r1是最後一筆時正常派排序 */
return 1;
} else if (r2 == table.getItems().get(table.getItems().size()-1)) {
/* r2是最後一筆時不排序 */
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, moneyCol, emailCol);
// table.getColumns().hashCode();
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 money;
private final SimpleStringProperty email;
private Person(String fName, String money, String email) {
this.firstName = new SimpleStringProperty(fName);
this.money = new SimpleStringProperty(money);
this.email = new SimpleStringProperty(email);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getMoney() {
return money.get();
}
public void setMoney(String Money) {
money.set(Money);
}
public String getEmail() {
return email.get();
}
public void setEmail(String Email) {
email.set(Email);
}
}
public static class ExtraPerson extends Person {
private final SimpleStringProperty address;
private ExtraPerson(String address) {
super(“Itachi”, “333”, “leaf@village.ninja”);
this.address = new SimpleStringProperty(address);
}
public String getAddress() {
return address.get();
}
public void setAddress(String address) {
this.address.set(address);
}
}
}