Advertisement
Guest User

JavaFX ListView will slow ListCell instantiation.

a guest
Jul 31st, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import com.sun.javafx.runtime.VersionInfo;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.Label;
  5. import javafx.scene.control.ListCell;
  6. import javafx.scene.control.ListView;
  7. import javafx.stage.Stage;
  8. import javafx.util.Callback;
  9.  
  10. public class SlowInitCell extends Application {
  11.     @Override
  12.     public void start(Stage stage) throws Exception {
  13.         ListView<String> listView = new ListView<>();
  14.  
  15.         listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
  16.             @Override
  17.             public ListCell<String> call(ListView<String> stringListView) {
  18.                 return new CustomListCell();
  19.             }
  20.         });
  21.  
  22.         for(int i=0;i<10000;i++) {
  23.             listView.getItems().add("List item " + i);
  24.         }
  25.  
  26.         stage.setTitle("List View Scrolling");
  27.         stage.setScene(new Scene(listView));
  28.         stage.show();
  29.     }
  30.  
  31.     public static void main(String[] args) {
  32.         System.out.println(VersionInfo.getRuntimeVersion());
  33.         launch(args);
  34.     }
  35.  
  36.     private class CustomListCell extends ListCell<String> {
  37.         private final Label label = new Label();
  38.  
  39.         private CustomListCell() {
  40.             try {
  41.                 Thread.sleep(50);
  42.             }
  43.             catch (Exception e) {
  44.                 e.printStackTrace();
  45.             }
  46.         }
  47.  
  48.         @Override
  49.         protected void updateItem(String s, boolean empty) {
  50.             super.updateItem(s, empty);
  51.  
  52.             if(!empty) {
  53.                 label.setText(s);
  54.                 setGraphic(label);
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement