import org.tbee.javafx.scene.layout.MigPane;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
/**
* Simple test app for the ListView setGraphics bug in MigPane
*/
public class ListViewTestApp extends Application {
/**
* @param args a
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
ListViewTester listViewTester = new ListViewTester();
Button btn = new Button("Add to list");
btn.setOnAction(e -> listViewTester.stringsData.add("New " + listViewTester.stringsData.size()));
MigPane pane = new MigPane();
pane.add(new Label("Error in MigPane 5.1 on Java9"), "wrap");
pane.add(new Label("Reproduce for example by adding a few items to the list view and then quickly select different items in the list"), "wrap");
pane.add(btn, "wrap");
pane.add(listViewTester, "growx, pushx");
primaryStage.setTitle("ListView test");
primaryStage.setScene(new Scene(pane));
// solution: avoid dimension calculation, with either bind
// bindDimensions(listViewTester, primaryStage.getScene());
// or absolute fix
// listViewTester.setMinHeight(500);
// listViewTester.setMaxHeight(500);
// listViewTester.setPrefHeight(500);
// listViewTester.setMinWidth(500);
// listViewTester.setMaxWidth(500);
// listViewTester.setPrefWidth(500);
primaryStage.show();
}
private static void bindDimensions(MigPane migPane, Scene scene) {
migPane.minWidthProperty().bind(scene.widthProperty());
migPane.maxWidthProperty().bind(scene.widthProperty());
migPane.prefWidthProperty().bind(scene.widthProperty());
migPane.minHeightProperty().bind(scene.heightProperty());
migPane.maxHeightProperty().bind(scene.heightProperty());
migPane.prefHeightProperty().bind(scene.heightProperty());
}
private static class ListViewTester extends MigPane {
ListView<String> stringsView = new ListView<>();
ObservableList<String> stringsData = FXCollections.observableArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
ListViewTester() {
stringsView.setItems(stringsData);
stringsView.setCellFactory(list -> new TestCell());
add(stringsView, "growx, pushx");
}
}
private static class TestCell extends ListCell<String> {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
// setText(item);
setGraphic(new Label("Label: " + item));
}
}
}
}