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 TableViewCostumerSort extends Application {
private TableView table = new TableView();
private ExtraPerson extraPerson = new ExtraPerson(“Ninja Village”);
private final ObservableList 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(
“firstName”));
TableColumn moneyCol = new TableColumn(“Money”);
moneyCol.setMinWidth(100);
moneyCol
.setCellValueFactory(new PropertyValueFactory(
“Money”));
TableColumn emailCol = new TableColumn(“Email”);
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(new PropertyValueFactory(
“email”));
/**
* Adding comparator to extraPerson
* 覆寫sort判斷
*/
table.sortPolicyProperty().set(
new Callback<TableView, Boolean>() {
/*自動生成*/
@Override
public Boolean call(TableView param) {
Comparator comparator = new Comparator() {
@Override
public int compare(Person r1, Person r2) {
/*資料形式轉換*/
double r1_d = 0.0;
double r2_d = 0.0;
if (param.getComparator() != null) {
r1_d = Double.parseDouble(r1.getMoney().replaceAll(“,”, “”).trim());
r2_d = Double.parseDouble(r2.getMoney().replaceAll(“,”, “”).trim());
}
/*資料空值try-catch*/
String col_s = “”;
String _sort_s = “”;
try {
col_s = param.getSortOrder().get(0).getText();
_sort_s = param.getSortOrder().get(0).getSortType().toString();
} catch (java.lang.IndexOutOfBoundsException e) {
System.out.println(e);
}
/*設定選取Money欄位時動作*/
if (col_s.equals(“Money”) && !col_s.equals(“”)) {
if (_sort_s.equals(“ASCENDING”)) {
if (r1_d > r2_d) {
/* 0&-1是不排序1是排序 */
return 1;
} else if (r1_d < r2_d) {
/* 0&-1是不排序1是排序 */
return -1;
} else if (param.getComparator() == null) {
/* 0&-1是不排序1是排序 */
return 0;
} else {
/*預設判斷*/
return param.getComparator().compare(r1, r2);
}
} else if (_sort_s.equals("DESCENDING")) {
if (r1_d r2_d) {
/* 0&-1是不排序1是排序 */
return -1;
} else if (param.getComparator() == null) {
/* 0&-1是不排序1是排序 */
return 0;
} else {
/*預設判斷*/
return param.getComparator().compare(r1, r2);
}
} else {
/*預設判斷*/
return param.getComparator().compare(r1, r2);
}
} else {
try {
/*預設判斷*/
return param.getComparator().compare(r1, r2);
} catch (java.lang.NullPointerException e) {
/*排序*/
System.out.println(e);
return 0;
}
}
}
};
/*預設複寫狀態成立*/
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);
}
}
}