Merge branch 'eugenp:master' into master

This commit is contained in:
JoannaaKL 2021-08-21 18:18:44 +02:00 committed by GitHub
commit 9935885d3b
10 changed files with 220 additions and 1 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>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-surefire-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven-surefire-plugin</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>maven-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,14 @@
package com.baeldung.runasingletest;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class TheFirstUnitTest {
private static final Logger logger = LoggerFactory.getLogger(TheFirstUnitTest.class);
@Test
void whenTestCase_thenPass() {
logger.info("Running a dummyTest");
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.runasingletest;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class TheSecondUnitTest {
private static final Logger logger = LoggerFactory.getLogger(TheSecondUnitTest.class);
@Test
void whenTestCase1_thenPrintTest1_1() {
logger.info("Running When Case1: test1_1");
}
@Test
void whenTestCase1_thenPrintTest1_2() {
logger.info("Running When Case1: test1_2");
}
@Test
void whenTestCase1_thenPrintTest1_3() {
logger.info("Running When Case1: test1_3");
}
@Test
void whenTestCase2_thenPrintTest2_1() {
logger.info("Running When Case2: test2_1");
}
}

View File

@ -35,6 +35,7 @@
<module>maven-builder-plugin</module>
<module>host-maven-repo-example</module>
<module>plugin-management</module>
<module>maven-surefire-plugin</module>
</modules>
</project>
</project>