Skip to content

Commit cf452d7

Browse files
authored
Added JavaFX demo app for ListView CellFactory (#11133)
* Added JavaFX demo app for ListView CellFactory * Moved to the common project
1 parent 5dfb397 commit cf452d7

6 files changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.listview;
2+
3+
import com.baeldung.listview.cellfactory.CheckboxCellFactory;
4+
import com.baeldung.listview.cellfactory.PersonCellFactory;
5+
import javafx.collections.FXCollections;
6+
import javafx.collections.ObservableList;
7+
import javafx.fxml.FXML;
8+
import javafx.fxml.Initializable;
9+
import javafx.scene.control.ListView;
10+
11+
import java.net.URL;
12+
import java.util.ResourceBundle;
13+
14+
public class ExampleController implements Initializable {
15+
@FXML
16+
private ListView<Person> listView;
17+
18+
@Override
19+
public void initialize(URL location, ResourceBundle resources) {
20+
ObservableList<Person> wordsList = FXCollections.observableArrayList();
21+
wordsList.add(new Person("Isaac", "Newton"));
22+
wordsList.add(new Person("Albert", "Einstein"));
23+
wordsList.add(new Person("Ludwig", "Boltzmann"));
24+
listView.setItems(wordsList);
25+
}
26+
27+
public void defaultButtonClick() {
28+
listView.setCellFactory(null);
29+
}
30+
31+
public void cellFactoryButtonClick() {
32+
listView.setCellFactory(new PersonCellFactory());
33+
}
34+
35+
public void checkboxCellFactoryButtonClick() {
36+
listView.setCellFactory(new CheckboxCellFactory());
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.javafx.listview;
2+
3+
import javafx.application.Application;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Parent;
6+
import javafx.scene.Scene;
7+
import javafx.stage.Stage;
8+
9+
import java.net.URL;
10+
11+
public class Main extends Application {
12+
13+
public static void main(String args[]) {
14+
launch(args);
15+
}
16+
17+
@Override
18+
public void start(Stage primaryStage) throws Exception {
19+
FXMLLoader loader = new FXMLLoader();
20+
URL xmlUrl = getClass().getResource("/example.fxml");
21+
loader.setLocation(xmlUrl);
22+
Parent root = loader.load();
23+
24+
primaryStage.setTitle("List View Demo");
25+
primaryStage.setScene(new Scene(root));
26+
primaryStage.show();
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.listview;
2+
3+
public class Person {
4+
5+
private final String firstName;
6+
private final String lastName;
7+
8+
public Person(String firstName, String lastName) {
9+
this.firstName = firstName;
10+
this.lastName = lastName;
11+
}
12+
13+
public String getFirstName() {
14+
return firstName;
15+
}
16+
17+
public String getLastName() {
18+
return lastName;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return firstName + " " + lastName;
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.listview.cellfactory;
2+
3+
import com.baeldung.listview.Person;
4+
import javafx.scene.control.CheckBox;
5+
import javafx.scene.control.ListCell;
6+
import javafx.scene.control.ListView;
7+
import javafx.util.Callback;
8+
9+
public class CheckboxCellFactory implements Callback<ListView<Person>, ListCell<Person>> {
10+
@Override
11+
public ListCell<Person> call(ListView<Person> param) {
12+
return new ListCell<Person>(){
13+
@Override
14+
public void updateItem(Person person, boolean empty) {
15+
super.updateItem(person, empty);
16+
if (empty) {
17+
setText(null);
18+
setGraphic(null);
19+
} else if (person != null) {
20+
setText(null);
21+
setGraphic(new CheckBox(person.getFirstName() + " " + person.getLastName()));
22+
} else {
23+
setText("null");
24+
setGraphic(null);
25+
}
26+
}
27+
};
28+
}
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.listview.cellfactory;
2+
3+
import com.baeldung.listview.Person;
4+
import javafx.scene.control.ListCell;
5+
import javafx.scene.control.ListView;
6+
import javafx.util.Callback;
7+
8+
public class PersonCellFactory implements Callback<ListView<Person>, ListCell<Person>> {
9+
@Override
10+
public ListCell<Person> call(ListView<Person> param) {
11+
return new ListCell<Person>(){
12+
@Override
13+
public void updateItem(Person person, boolean empty) {
14+
super.updateItem(person, empty);
15+
if (empty || person == null) {
16+
setText(null);
17+
} else {
18+
setText(person.getFirstName() + " " + person.getLastName());
19+
}
20+
}
21+
};
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.Button?>
4+
<?import javafx.scene.control.ListView?>
5+
<?import javafx.scene.layout.AnchorPane?>
6+
7+
<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">
8+
<children>
9+
<ListView id="listView" fx:id="listView" layoutX="14.0" layoutY="25.0" prefHeight="138.0" prefWidth="256.0" />
10+
<Button layoutX="283.0" layoutY="25.0" mnemonicParsing="false" onAction="#defaultButtonClick" prefHeight="25.0" prefWidth="139.0" text="Default" />
11+
<Button layoutX="283.0" layoutY="63.0" mnemonicParsing="false" onAction="#cellFactoryButtonClick" prefHeight="25.0" prefWidth="139.0" text="Cell Factory" />
12+
<Button layoutX="283.0" layoutY="104.0" mnemonicParsing="false" onAction="#checkboxCellFactoryButtonClick" prefHeight="25.0" prefWidth="139.0" text="Checkbox Cell Factory" />
13+
</children>
14+
</AnchorPane>

0 commit comments

Comments
 (0)