From 3b5728de6f2d8d2852fbb86fa8cac21e6dccc19b Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 4 May 2015 22:11:28 +0200 Subject: Build a file browser looking like Finder --- src/ch/makery/sortfilter/Main.java | 36 +++++++++ src/ch/makery/sortfilter/Person.java | 44 +++++++++++ src/ch/makery/sortfilter/PersonTable.fxml | 25 ++++++ .../makery/sortfilter/PersonTableController.java | 91 ++++++++++++++++++++++ 4 files changed, 196 insertions(+) create mode 100644 src/ch/makery/sortfilter/Main.java create mode 100644 src/ch/makery/sortfilter/Person.java create mode 100644 src/ch/makery/sortfilter/PersonTable.fxml create mode 100644 src/ch/makery/sortfilter/PersonTableController.java (limited to 'src/ch/makery') diff --git a/src/ch/makery/sortfilter/Main.java b/src/ch/makery/sortfilter/Main.java new file mode 100644 index 0000000..6a8f562 --- /dev/null +++ b/src/ch/makery/sortfilter/Main.java @@ -0,0 +1,36 @@ +package ch.makery.sortfilter; + +import java.io.IOException; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; + +/** + * Main class to start the application. + * + * @author Marco Jakob + */ +public class Main extends Application { + + @Override + public void start(Stage primaryStage) { + primaryStage.setTitle("Sorting and Filtering"); + + try { + FXMLLoader loader = new FXMLLoader(Main.class.getResource("PersonTable.fxml")); + AnchorPane page = (AnchorPane) loader.load(); + Scene scene = new Scene(page); + primaryStage.setScene(scene); + primaryStage.show(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + launch(args); + } +} \ No newline at end of file diff --git a/src/ch/makery/sortfilter/Person.java b/src/ch/makery/sortfilter/Person.java new file mode 100644 index 0000000..af6f212 --- /dev/null +++ b/src/ch/makery/sortfilter/Person.java @@ -0,0 +1,44 @@ +package ch.makery.sortfilter; + +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; + +/** + * Simple model class for the person table. + * + * @author Marco Jakob + */ +public class Person { + + private final StringProperty firstName; + private final StringProperty lastName; + + public Person(String firstName, String lastName) { + this.firstName = new SimpleStringProperty(firstName); + this.lastName = new SimpleStringProperty(lastName); + } + + public String getFirstName() { + return firstName.get(); + } + + public void setFirstName(String firstName) { + this.firstName.set(firstName); + } + + public StringProperty firstNameProperty() { + return firstName; + } + + public String getLastName() { + return lastName.get(); + } + + public void setLastName(String lastName) { + this.lastName.set(lastName); + } + + public StringProperty lastNameProperty() { + return lastName; + } +} \ No newline at end of file diff --git a/src/ch/makery/sortfilter/PersonTable.fxml b/src/ch/makery/sortfilter/PersonTable.fxml new file mode 100644 index 0000000..aeb41b0 --- /dev/null +++ b/src/ch/makery/sortfilter/PersonTable.fxml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ch/makery/sortfilter/PersonTableController.java b/src/ch/makery/sortfilter/PersonTableController.java new file mode 100644 index 0000000..98ad591 --- /dev/null +++ b/src/ch/makery/sortfilter/PersonTableController.java @@ -0,0 +1,91 @@ +package ch.makery.sortfilter; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.collections.transformation.FilteredList; +import javafx.collections.transformation.SortedList; +import javafx.fxml.FXML; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableView; +import javafx.scene.control.TextField; + + +/** + * View-Controller for the person table. + * + * @author Marco Jakob + */ +public class PersonTableController { + + @FXML + private TextField filterField; + @FXML + private TableView personTable; + @FXML + private TableColumn firstNameColumn; + @FXML + private TableColumn lastNameColumn; + + private ObservableList masterData = FXCollections.observableArrayList(); + + /** + * Just add some sample data in the constructor. + */ + public PersonTableController() { + masterData.add(new Person("Hans", "Muster")); + masterData.add(new Person("Ruth", "Mueller")); + masterData.add(new Person("Heinz", "Kurz")); + masterData.add(new Person("Cornelia", "Meier")); + masterData.add(new Person("Werner", "Meyer")); + masterData.add(new Person("Lydia", "Kunz")); + masterData.add(new Person("Anna", "Best")); + masterData.add(new Person("Stefan", "Meier")); + masterData.add(new Person("Martin", "Mueller")); + } + + /** + * Initializes the controller class. This method is automatically called + * after the fxml file has been loaded. + * + * Initializes the table columns and sets up sorting and filtering. + */ + @FXML + private void initialize() { + // 0. Initialize the columns. + firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty()); + lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty()); + + // 1. Wrap the ObservableList in a FilteredList (initially display all data). + FilteredList filteredData = new FilteredList<>(masterData, p -> true); + + // 2. Set the filter Predicate whenever the filter changes. + filterField.textProperty().addListener((observable, oldValue, newValue) -> { + filteredData.setPredicate(person -> { + // If filter text is empty, display all persons. + if (newValue == null || newValue.isEmpty()) { + return true; + } + + // Compare first name and last name of every person with filter text. + String lowerCaseFilter = newValue.toLowerCase(); + + if (person.getFirstName().toLowerCase().indexOf(lowerCaseFilter) != -1) { + return true; // Filter matches first name. + } else if (person.getLastName().toLowerCase().indexOf(lowerCaseFilter) != -1) { + return true; // Filter matches last name. + } + return false; // Does not match. + }); + }); + + // 3. Wrap the FilteredList in a SortedList. + SortedList sortedData = new SortedList<>(filteredData); + + // 4. Bind the SortedList comparator to the TableView comparator. + // Otherwise, sorting the TableView would have no effect. + sortedData.comparatorProperty().bind(personTable.comparatorProperty()); + + // 5. Add sorted (and filtered) data to the table. + personTable.setItems(sortedData); + } +} -- cgit v1.2.3-70-g09d2