JavaFX TableColumn:有没有办法生成列标题事件?

时间:2015-07-31 16:32:55

标签: javafx tablecolumn

我通过在整个表上设置比较器来解决我所看到的基本问题,但我最初尝试做的是找到一种方法来“点击”标题以生成排序事件。

我仍然想知道如何做到这一点,因为我目前还不知道一种方法来处理列的排序方法,只有表本身。

1 个答案:

答案 0 :(得分:2)

TableView上调用getSortOrder():返回表示行排序顺序的TableColumn列表:

  

空的sortOrder列表表示没有对其应用排序   TableView中。如果sortOrder列表中有一个TableColumn,则   TableView将使用sortType和comparator属性进行排序   这个TableColumn(假设TableColumn.sortable为true)。如果   sortOrder列表包含多个TableColumn实例,然后是   TableView首先根据第一个属性进行排序   TableColumn的。如果两个元素被认为是相等的,那么第二个元素   列表中的TableColumn用于确定排序。这重复   直到考虑所有TableColumn比较器的结果,如果   必要的。

然后根据需要添加,删除,设置,清除等列表。

SSCCE:

import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableViewProgrammaticSort extends Application {

    @Override
    public void start(Stage primaryStage) {

        TableView<Person> table = new TableView<>();

        TableColumn<Person, String> firstNameCol = column("First Name", Person::firstNameProperty);
        TableColumn<Person, String> lastNameCol = column("Last Name", Person::lastNameProperty);
        TableColumn<Person, String> emailCol = column("Email", Person::emailProperty);

        table.getColumns().add(firstNameCol);
        table.getColumns().add(lastNameCol);
        table.getColumns().add(emailCol);

        table.getItems().addAll(
            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")
        );

        ComboBox<TableColumn<Person, ?>> sortCombo = new ComboBox<>();
        sortCombo.getItems().add(firstNameCol);
        sortCombo.getItems().add(lastNameCol);
        sortCombo.getItems().add(emailCol);

        sortCombo.setCellFactory(lv -> new ColumnListCell());
        sortCombo.valueProperty().addListener((obs, oldColumn, newColumn) -> {
            table.getSortOrder().clear();
            if (newColumn != null) {
                table.getSortOrder().add(newColumn);
            }
        });
        sortCombo.setButtonCell(new ColumnListCell());

        BorderPane root = new BorderPane(table, sortCombo, null, null, null);
        BorderPane.setMargin(table, new Insets(10));
        BorderPane.setMargin(sortCombo, new Insets(10));
        BorderPane.setAlignment(sortCombo, Pos.CENTER);

        Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private static class ColumnListCell extends ListCell<TableColumn<Person, ?>> {
        @Override
        public void updateItem(TableColumn<Person, ?> column, boolean empty) {
            super.updateItem(column, empty);
            if (empty) {
                setText(null);
            } else {
                setText(column.getText());
            }
        }
    }

    private static <S,T> TableColumn<S,T> column(String title, Function<S, ObservableValue<T>> property) {
        TableColumn<S,T> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        return col ;
    }

    public static class Person {
        private final StringProperty firstName = new SimpleStringProperty(this, "firstName");
        private final StringProperty lastName = new SimpleStringProperty(this, "lastName");
        private final StringProperty email = new SimpleStringProperty(this, "email");

        public Person(String firstName, String lastName, String email) {
            this.firstName.set(firstName);
            this.lastName.set(lastName);
            this.email.set(email);
        }

        public final StringProperty firstNameProperty() {
            return this.firstName;
        }

        public final java.lang.String getFirstName() {
            return this.firstNameProperty().get();
        }

        public final void setFirstName(final java.lang.String firstName) {
            this.firstNameProperty().set(firstName);
        }

        public final StringProperty lastNameProperty() {
            return this.lastName;
        }

        public final java.lang.String getLastName() {
            return this.lastNameProperty().get();
        }

        public final void setLastName(final java.lang.String lastName) {
            this.lastNameProperty().set(lastName);
        }

        public final StringProperty emailProperty() {
            return this.email;
        }

        public final java.lang.String getEmail() {
            return this.emailProperty().get();
        }

        public final void setEmail(final java.lang.String email) {
            this.emailProperty().set(email);
        }

    }

    public static void main(String[] args) {
        launch(args);
    }
}