java - Detect doubleclick on cell of TableView JavaFX -
i trying detect doubleclick on random cell of tableview. detection of doubleclick not problem rather cell has been doubleclicked.
table.addeventfilter(mouseevent.mouse_clicked, new eventhandler<mouseevent>() { @override public void handle(mouseevent event) { if (event.getclickcount() > 1) { system.out.println("double clicked!"); tablecell c = (tablecell) event.getsource(); system.out.println("cell text: " + c.gettext()); } } });
this how i'm building table:
private void buildtable() throws exception { /*some initialisations etc*/ for(int i=0; i<result.getmetadata().getcolumncount();i++) { final int j = i; tablecolumn col = new tablecolumn(result.getmetadata().getcolumnname(i+1)); col.setcellvaluefactory(new callback<celldatafeatures<observablelist,string>,observablevalue<string>>(){ public observablevalue<string> call(celldatafeatures<observablelist, string> param) { return new simplestringproperty(param.getvalue().get(j).tostring()); } }); table.getcolumns().addall(col); } while(result.next()){ observablelist<string> row = fxcollections.observablearraylist(); for(int = 1; i<=result.getmetadata().getcolumncount();i++){ row.add(result.getstring(i)); } data.add(row); } table.setitems(data); }catch(exception e){ e.printstacktrace(); } }
the real problem here can't typecast tablecell. can me out? grateful.
instead of registering handler table, need register handler table cells themselves. this, use cell factory on appropriate tablecolumn
(s).
as example, add following code standard table example (listing 13.6).
firstnamecol.setcellfactory(new callback<tablecolumn<person, string>, tablecell<person, string>>() { @override public tablecell<person, string> call(tablecolumn<person, string> col) { final tablecell<person, string> cell = new tablecell<person, string>() { @override public void updateitem(string firstname, boolean empty) { super.updateitem(firstname, empty); if (empty) { settext(null); } else { settext(firstname); } } }; cell.addeventhandler(mouseevent.mouse_clicked, new eventhandler<mouseevent>() { @override public void handle(mouseevent event) { if (event.getclickcount() > 1) { system.out.println("double click on "+cell.getitem()); } } }); return cell ; } });
Comments
Post a Comment