From cf452d7add33fba456c75f5d496a1364690518c1 Mon Sep 17 00:00:00 2001 From: Anton Kudruk Date: Sat, 21 Aug 2021 19:09:19 +0300 Subject: [PATCH] Added JavaFX demo app for ListView CellFactory (#11133) * Added JavaFX demo app for ListView CellFactory * Moved to the common project --- .../baeldung/listview/ExampleController.java | 38 +++++++++++++++++++ .../main/java/com/baeldung/listview/Main.java | 28 ++++++++++++++ .../java/com/baeldung/listview/Person.java | 25 ++++++++++++ .../cellfactory/CheckboxCellFactory.java | 29 ++++++++++++++ .../cellfactory/PersonCellFactory.java | 23 +++++++++++ javafx/src/main/resources/example.fxml | 14 +++++++ 6 files changed, 157 insertions(+) create mode 100644 javafx/src/main/java/com/baeldung/listview/ExampleController.java create mode 100644 javafx/src/main/java/com/baeldung/listview/Main.java create mode 100644 javafx/src/main/java/com/baeldung/listview/Person.java create mode 100644 javafx/src/main/java/com/baeldung/listview/cellfactory/CheckboxCellFactory.java create mode 100644 javafx/src/main/java/com/baeldung/listview/cellfactory/PersonCellFactory.java create mode 100644 javafx/src/main/resources/example.fxml diff --git a/javafx/src/main/java/com/baeldung/listview/ExampleController.java b/javafx/src/main/java/com/baeldung/listview/ExampleController.java new file mode 100644 index 0000000000..c02fa68d2e --- /dev/null +++ b/javafx/src/main/java/com/baeldung/listview/ExampleController.java @@ -0,0 +1,38 @@ +package com.baeldung.listview; + +import com.baeldung.listview.cellfactory.CheckboxCellFactory; +import com.baeldung.listview.cellfactory.PersonCellFactory; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.ListView; + +import java.net.URL; +import java.util.ResourceBundle; + +public class ExampleController implements Initializable { + @FXML + private ListView listView; + + @Override + public void initialize(URL location, ResourceBundle resources) { + ObservableList wordsList = FXCollections.observableArrayList(); + wordsList.add(new Person("Isaac", "Newton")); + wordsList.add(new Person("Albert", "Einstein")); + wordsList.add(new Person("Ludwig", "Boltzmann")); + listView.setItems(wordsList); + } + + public void defaultButtonClick() { + listView.setCellFactory(null); + } + + public void cellFactoryButtonClick() { + listView.setCellFactory(new PersonCellFactory()); + } + + public void checkboxCellFactoryButtonClick() { + listView.setCellFactory(new CheckboxCellFactory()); + } +} diff --git a/javafx/src/main/java/com/baeldung/listview/Main.java b/javafx/src/main/java/com/baeldung/listview/Main.java new file mode 100644 index 0000000000..a067971758 --- /dev/null +++ b/javafx/src/main/java/com/baeldung/listview/Main.java @@ -0,0 +1,28 @@ +package com.baeldung.javafx.listview; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +import java.net.URL; + +public class Main extends Application { + + public static void main(String args[]) { + launch(args); + } + + @Override + public void start(Stage primaryStage) throws Exception { + FXMLLoader loader = new FXMLLoader(); + URL xmlUrl = getClass().getResource("/example.fxml"); + loader.setLocation(xmlUrl); + Parent root = loader.load(); + + primaryStage.setTitle("List View Demo"); + primaryStage.setScene(new Scene(root)); + primaryStage.show(); + } +} diff --git a/javafx/src/main/java/com/baeldung/listview/Person.java b/javafx/src/main/java/com/baeldung/listview/Person.java new file mode 100644 index 0000000000..cdc0ab2dc8 --- /dev/null +++ b/javafx/src/main/java/com/baeldung/listview/Person.java @@ -0,0 +1,25 @@ +package com.baeldung.listview; + +public class Person { + + private final String firstName; + private final String lastName; + + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + @Override + public String toString() { + return firstName + " " + lastName; + } +} diff --git a/javafx/src/main/java/com/baeldung/listview/cellfactory/CheckboxCellFactory.java b/javafx/src/main/java/com/baeldung/listview/cellfactory/CheckboxCellFactory.java new file mode 100644 index 0000000000..522afcb76e --- /dev/null +++ b/javafx/src/main/java/com/baeldung/listview/cellfactory/CheckboxCellFactory.java @@ -0,0 +1,29 @@ +package com.baeldung.listview.cellfactory; + +import com.baeldung.listview.Person; +import javafx.scene.control.CheckBox; +import javafx.scene.control.ListCell; +import javafx.scene.control.ListView; +import javafx.util.Callback; + +public class CheckboxCellFactory implements Callback, ListCell> { + @Override + public ListCell call(ListView param) { + return new ListCell(){ + @Override + public void updateItem(Person person, boolean empty) { + super.updateItem(person, empty); + if (empty) { + setText(null); + setGraphic(null); + } else if (person != null) { + setText(null); + setGraphic(new CheckBox(person.getFirstName() + " " + person.getLastName())); + } else { + setText("null"); + setGraphic(null); + } + } + }; + } +} diff --git a/javafx/src/main/java/com/baeldung/listview/cellfactory/PersonCellFactory.java b/javafx/src/main/java/com/baeldung/listview/cellfactory/PersonCellFactory.java new file mode 100644 index 0000000000..57866b9ead --- /dev/null +++ b/javafx/src/main/java/com/baeldung/listview/cellfactory/PersonCellFactory.java @@ -0,0 +1,23 @@ +package com.baeldung.listview.cellfactory; + +import com.baeldung.listview.Person; +import javafx.scene.control.ListCell; +import javafx.scene.control.ListView; +import javafx.util.Callback; + +public class PersonCellFactory implements Callback, ListCell> { + @Override + public ListCell call(ListView param) { + return new ListCell(){ + @Override + public void updateItem(Person person, boolean empty) { + super.updateItem(person, empty); + if (empty || person == null) { + setText(null); + } else { + setText(person.getFirstName() + " " + person.getLastName()); + } + } + }; + } +} diff --git a/javafx/src/main/resources/example.fxml b/javafx/src/main/resources/example.fxml new file mode 100644 index 0000000000..c68df076d0 --- /dev/null +++ b/javafx/src/main/resources/example.fxml @@ -0,0 +1,14 @@ + + + + + + + + + +