Howto select and edit TabelCell in TableView with mouseclick on a MenuItem of ContextMenu - JavaFX -
is there possiblity select & edit tablecell
within tableview
after 1 has mouse clicked or key pressed menuitem
of contextmenu
? default textfieldtablecell
implementation supports pressing enter key while tablerow
has focus or directly clicking respective cell select & edit content.
if understand question correctly, yes can this. here's example lets edit cells in second column using context menu:
import javafx.application.application; import javafx.beans.property.simplestringproperty; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.event.eventhandler; import javafx.scene.scene; import javafx.scene.control.contextmenu; import javafx.scene.control.menuitem; import javafx.scene.control.tablecolumn; import javafx.scene.control.tablecolumn.celleditevent; import javafx.scene.control.tableview; import javafx.scene.control.cell.textfieldtablecell; import javafx.stage.stage; public class editabletablemcve extends application { @override public void start(stage stage) { int numofcols = 10; observablelist<observablelist<string>> tabledata = fxcollections.observablearraylist(); // generate dummy data. (int = 0; < 100; i++) { observablelist<string> row = fxcollections.observablearraylist(); (int j = 0; j < numofcols; j++) row.add("row" + + "col" + j); tabledata.add(row); } tableview<observablelist<string>> table = new tableview<observablelist<string>>(); table.seteditable(true); // add columns table. (int = 0; < numofcols; i++) { // make cells in column on index 1 editable. if (i == 1) { table.getcolumns().add(addeditablecolumn(i, "column " + i)); } else { table.getcolumns().add(addcolumn(i, "column " + i)); } } table.getitems().addall(tabledata); menuitem edititem = new menuitem("edit"); edititem.setonaction(e -> { // need index of selected row , tablecolumn // on column index want edit. int selectedrowindex = table.getselectionmodel().getselectedindex(); table.edit(selectedrowindex, table.getcolumns().get(1)); }); contextmenu menu = new contextmenu(edititem); table.setcontextmenu(menu); scene scene = new scene(table); stage.setscene(scene); stage.show(); } /** * returns simple column. */ private tablecolumn<observablelist<string>, string> addcolumn(int index, string name) { tablecolumn<observablelist<string>, string> col = new tablecolumn<observablelist<string>, string>(name); col.setcellvaluefactory(e -> new simplestringproperty(e.getvalue().get(index))); return col; } /** * returns editable column. */ private tablecolumn<observablelist<string>, string> addeditablecolumn(int index, string name) { tablecolumn<observablelist<string>, string> col = new tablecolumn<observablelist<string>, string>(name); col.setcellvaluefactory(e -> new simplestringproperty(e.getvalue().get(index))); col.setcellfactory(textfieldtablecell.fortablecolumn()); col.setoneditcommit(new eventhandler<celleditevent<observablelist<string>, string>>() { @override public void handle(celleditevent<observablelist<string>, string> t) { ((observablelist<string>) t.gettableview().getitems().get(t.gettableposition().getrow())).set(index, t.getnewvalue()); } }); return col; } public static void main(string[] args) { launch(); } }
Comments
Post a Comment