Last active
March 20, 2020 13:34
-
-
Save PrimosK/c9694aae33b116dfa8bcdf3914f069e3 to your computer and use it in GitHub Desktop.
Running this example with FXControls 8.40.14. compared to FXControls 11.0.1. will produce different result in terms of how many times GridCell#updateItem(...) is being called.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.layout.Priority; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
import org.controlsfx.control.GridCell; | |
import org.controlsfx.control.GridView; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Running this example with FXControls 8.40.14. compared to FXControls 11.0.1. | |
* will produce different result in terms of how many times GridCell#updateItem(...) | |
* is being called (see gridCellUpdateCounter). | |
* | |
* Side note - this example was taken from: | |
* https://github.com/controlsfx/controlsfx/issues/1070 | |
*/ | |
public class GridViewItemUpdateTest extends Application { | |
int gridCellUpdateCounter = 0; | |
public static void main(String[] args) { | |
launch(); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
List<String> itemsFoo = new ArrayList<>(); | |
for (int i = 0; i < 50; i++) { | |
itemsFoo.add("foo"); | |
} | |
List<String> itemsBar = new ArrayList<>(); | |
for (int i = 0; i < 50; i++) { | |
itemsBar.add("bar"); | |
} | |
GridView<String> grid = new GridView<>(); | |
grid.setCellFactory(param -> new SimpleCell()); | |
ObservableList<String> items = FXCollections.observableArrayList(); | |
items.setAll(itemsFoo); | |
grid.setItems(items); | |
Button btnFoo = new Button("foo"); | |
btnFoo.setOnAction(event -> items.setAll(itemsFoo)); | |
Button btnBar = new Button("bar"); | |
btnBar.setOnAction(event -> items.setAll(itemsBar)); | |
VBox root = new VBox(8); | |
root.setPrefSize(400, 355); | |
root.setPadding(new Insets(8)); | |
root.getChildren().addAll(btnFoo, btnBar, grid); | |
VBox.setVgrow(grid, Priority.ALWAYS); | |
Scene scene = new Scene(root); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
private class SimpleCell extends GridCell<String> { | |
@Override | |
protected void updateItem(String item, boolean empty) { | |
super.updateItem(item, empty); | |
System.out.println(++gridCellUpdateCounter); | |
if (empty || item == null) { | |
setText(null); | |
setStyle(""); | |
} else { | |
setText(item); | |
if (item.startsWith("f")) { | |
setStyle("-fx-background-color: red"); | |
} else { | |
setStyle("-fx-background-color: green"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment