Added JavaFX demo app for ListView CellFactory (#11133)

* Added JavaFX demo app for ListView CellFactory

* Moved to the common project
This commit is contained in:
Anton Kudruk 2021-08-21 19:09:19 +03:00 committed by GitHub
parent 5dfb39712d
commit cf452d7add
6 changed files with 157 additions and 0 deletions

View File

@ -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<Person> listView;
@Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<Person> 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());
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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<ListView<Person>, ListCell<Person>> {
@Override
public ListCell<Person> call(ListView<Person> param) {
return new ListCell<Person>(){
@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);
}
}
};
}
}

View File

@ -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<ListView<Person>, ListCell<Person>> {
@Override
public ListCell<Person> call(ListView<Person> param) {
return new ListCell<Person>(){
@Override
public void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
if (empty || person == null) {
setText(null);
} else {
setText(person.getFirstName() + " " + person.getLastName());
}
}
};
}
}

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="188.0" prefWidth="457.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.baeldung.listview.ExampleController">
<children>
<ListView id="listView" fx:id="listView" layoutX="14.0" layoutY="25.0" prefHeight="138.0" prefWidth="256.0" />
<Button layoutX="283.0" layoutY="25.0" mnemonicParsing="false" onAction="#defaultButtonClick" prefHeight="25.0" prefWidth="139.0" text="Default" />
<Button layoutX="283.0" layoutY="63.0" mnemonicParsing="false" onAction="#cellFactoryButtonClick" prefHeight="25.0" prefWidth="139.0" text="Cell Factory" />
<Button layoutX="283.0" layoutY="104.0" mnemonicParsing="false" onAction="#checkboxCellFactoryButtonClick" prefHeight="25.0" prefWidth="139.0" text="Checkbox Cell Factory" />
</children>
</AnchorPane>