java - ListView in javafx, adds multiple cells -


debugging code below, shows updateitem() method called multiple times, unable figure out why being called multiple times.

i wanted add tooltip listview.

// add tooltip, not working fully. lstcomments.setcellfactory(new callback<listview<string>, listcell<string>>() {     @override     public listcell<string> call(listview<string> p) {         final tooltip tt = new tooltip();         final listcell<string> cell = new listcell<string>() {             string message = ca.getmessage();              @override             public void updateitem(string s, boolean empty) {                 super.updateitem(s, empty);                 tt.settext(message);                 settooltip(tt);             }         };          cell.settext(ca.getmessage());         return cell;     } }); 

recommendation

i find usability of tooltips on listview cells horrible, because tooltips intercept standard mouse events used select rows, scroll list, etc. not recommend placing tooltips on listview cells.

why multiple cells created , updateitem called multiple times

it expected listview have multiple cells , updateitem() potentially called multiple times each cell.

a cell created every row in listview displayed on scene, if cells empty. couple more cells offscreen created efficient scroll handling. each time underlying data listview set or modified, or list scrolled, updateitem() invoked on relevant cells update cell's contents. in case of scrolling large list, updateitem() invoked many, many times each cell.

sample code setting tooltip on listview cells

the code below based on oracle javafx tutorial listview sample, customizes create tooltips cells when hover on them.

mport javafx.application.application; import javafx.collections.*; import javafx.scene.scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.scene.paint.color; import javafx.scene.shape.rectangle; import javafx.scene.text.font; import javafx.stage.stage; import javafx.util.callback;  public class listviewsample extends application {      listview<string> list = new listview<string>();     observablelist<string> data = fxcollections.observablearraylist(             "chocolate", "salmon", "gold", "coral", "darkorchid",             "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",             "blueviolet", "brown");     final label label = new label();      @override     public void start(stage stage) {         vbox box = new vbox();         scene scene = new scene(box, 200, 200);         stage.setscene(scene);         stage.settitle("listviewsample");         box.getchildren().addall(list, label);         vbox.setvgrow(list, priority.always);          label.setlayoutx(10);         label.setlayouty(115);         label.setfont(font.font("verdana", 20));          list.setitems(data);          list.setcellfactory(new callback<listview<string>, listcell<string>>() {             @override public listcell<string> call(listview<string> list) {                 return new colorrectcell();             }         });          list.getselectionmodel().selecteditemproperty().addlistener(                 (ov, old_val, new_val) -> {                         label.settext(new_val);                         label.settextfill(color.web(new_val));                 });         stage.show();     }      static class colorrectcell extends listcell<string> {         final rectangle swatch = new rectangle(30, 30);         final tooltip tip = new tooltip();          public colorrectcell() {             tip.setgraphic(swatch);         }          @override         public void updateitem(string color, boolean empty) {             super.updateitem(color, empty);              if (color != null) {                 swatch.setfill(color.valueof(color.touppercase()));                 settext(color);                 settooltip(tip);             } else {                 settext("");                 settooltip(null);             }         }     }      public static void main(string[] args) {         launch(args);     } } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -