javafx - Select first/last clears anchor information - MultipleSelectionModel -
i have table table.getselectionmodel().setselectionmode(selectionmode.multiple);
. need implement go last/first row of tableview
. used table.scrollto(ind); table.getselectionmodel().clearandselect(ind)
. scroll first row , select it. if try select numerous rows (using mouse right button + shift) doesn't select range of rows between first row , 1 pressed set anchor @ selected row.
what i've observed far:
- from pseudoclass css information. first row selected,focused - exprected.
table.getselectionmodel().selectedindexproperty() table.getselectionmodel().getselectedindices()
shows 0 , [0] respective - expected- row not selected - table no longer focused got grey not blue row. if call
table.requestfocus()
@ time , useplatformutil.runlater()
doesn't change anything. assume has nothing table focus.
any ideas howto (from code) select first row , make such situation after pressing mouse button on other row shift got multiple selection?
sample application. see createselectioncontrol()
-> button("|<")
import javafx.application.application; import javafx.beans.binding.stringbinding; import javafx.beans.value.observablevalue; import javafx.collections.fxcollections; import javafx.collections.listchangelistener; import javafx.collections.observablelist; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.geometry.insets; import javafx.scene.node; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.control.selectionmode; import javafx.scene.control.tablecolumn; import javafx.scene.control.tablecolumn.celleditevent; import javafx.scene.control.tableposition; import javafx.scene.control.tableview; import javafx.scene.control.textfield; import javafx.scene.control.cell.propertyvaluefactory; import javafx.scene.control.cell.textfieldtablecell; import javafx.scene.layout.borderpane; import javafx.scene.layout.gridpane; import javafx.scene.layout.hbox; import javafx.scene.layout.vbox; import javafx.scene.text.font; import javafx.stage.stage; /** * table standard cell editor */ public class selectiontableapp extends application { private static string id_desc_label = "desclabel"; private tableview<person> table; private static final person person_staszek = new person("stanislaw", "czerkawski", "s.czerkawski@wp.pl"); private final observablelist<person> data = fxcollections .observablearraylist( new person("jacob", "smith", "jacob.smith@example.com"), new person("isabella", "johnson", "isabella.johnson@example.com"), new person("ethan", "williams", "ethan.williams@example.com"), new person("emma", "jones", "emma.jones@example.com"), new person("michael", "brown", "michael.brown@example.com"), person_staszek); private tablecolumn firstnamecol; private tablecolumn lastnamecol; private tablecolumn emailcol; public static void main(string[] args) { launch(args); } @override public void start(stage stage) { adddata(); borderpane root = new borderpane(); scene scene = new scene(root); scene.getstylesheets().add("ks/javafx/table/def/1.css"); stage.settitle("table view sample"); stage.setwidth(450); stage.setheight(550); final label label = new label("address book"); label.setfont(new font("arial", 20)); createtable(); configuretable(table); final vbox vbox = new vbox(); vbox.setspacing(5); vbox.setpadding(new insets(10, 0, 0, 10)); vbox.getchildren().addall(createaddpersoncontrol(), createinfocontrol(), createselectioncontrol()); root.settop(label); root.setcenter(table); root.setbottom(vbox); stage.setscene(scene); stage.show(); scene.focusownerproperty().addlistener((obs, o, n) -> { system.out.println("selectiontableapp.start()=" + n); }); } private void adddata() { (int = 0; < 100; i++) { data.add(new person("first-" + i, "name-" + i, "mail-" + i)); } } private void configuretable(tableview<person> table) { table.seteditable(false); table.getselectionmodel().setselectionmode(selectionmode.multiple); } private void createtable() { table = new tableview<person>(); firstnamecol = new tablecolumn("first name"); firstnamecol.setminwidth(100); firstnamecol .setcellvaluefactory(new propertyvaluefactory<person, string>( "firstname")); firstnamecol.setcellfactory(textfieldtablecell.fortablecolumn()); firstnamecol .setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { system.out.println("oneditcommit"); t.gettableview().getitems() .get(t.gettableposition().getrow()) .setfirstname(t.getnewvalue()); } }); lastnamecol = new tablecolumn("last name"); lastnamecol.setminwidth(100); lastnamecol .setcellvaluefactory(new propertyvaluefactory<person, string>( "lastname")); lastnamecol.setcellfactory(textfieldtablecell.fortablecolumn()); lastnamecol .setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { system.out.println("oneditcommit"); t.gettableview().getitems() .get(t.gettableposition().getrow()) .setlastname(t.getnewvalue()); } }); emailcol = new tablecolumn("email"); emailcol.setminwidth(200); emailcol.setcellvaluefactory(new propertyvaluefactory<person, string>( "email")); emailcol.setcellfactory(textfieldtablecell.fortablecolumn()); emailcol.setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { system.out.println("oneditcommit"); t.gettableview().getitems().get(t.gettableposition().getrow()) .setemail(t.getnewvalue()); } }); table.setitems(data); table.getcolumns().addall(firstnamecol, lastnamecol, emailcol); } private node createaddpersoncontrol() { final textfield addfirstname = new textfield(); addfirstname.setprompttext("first name"); addfirstname.setmaxwidth(firstnamecol.getprefwidth()); final textfield addlastname = new textfield(); addlastname.setmaxwidth(lastnamecol.getprefwidth()); addlastname.setprompttext("last name"); final textfield addemail = new textfield(); addemail.setmaxwidth(emailcol.getprefwidth()); addemail.setprompttext("email"); final button addbutton = new button("add"); addbutton.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { data.add(new person(addfirstname.gettext(), addlastname .gettext(), addemail.gettext())); addfirstname.clear(); addlastname.clear(); addemail.clear(); } }); final hbox hb = new hbox(); hb.getchildren().addall(addfirstname, addlastname, addemail, addbutton); hb.setspacing(3); return hb; } private node createselectioncontrol() { button gofist = new button("|<"); gofist.setonaction(e -> { int ind = 0; table.scrollto(ind); table.getselectionmodel().clearandselect(ind); // bounds local = table.getboundsinlocal(); // bounds scenebounds = table.localtoscene(local); // bounds screenbounds = table.localtoscreen(local); // system.out.println("selectiontableapp.createselectioncontrol()=" // + // local); // system.out.println("selectiontableapp.createselectioncontrol()=" // + // scenebounds); // system.out.println("selectiontableapp.createselectioncontrol()=" // + // screenbounds); // // double scenex = scenebounds.getminx() + 10; // double sceney = scenebounds.getminy() + 10; // double screenx = screenbounds.getminx() + 10; // double screeny = screenbounds.getminx() + 10; // // scenex = 21; // sceney = 59; // screenx = 760; // screeny = 287; // // mouseevent me = new mouseevent(mouseevent.mouse_pressed, scenex, // sceney, screenx, screeny, // mousebutton.primary, 1, false, false, false, false, true, false, // false, true, false, false, // null); // mouseevent.fireevent(table, me); // me = new mouseevent(mouseevent.mouse_released, scenex, sceney, // screenx, screeny, // mousebutton.primary, 1, false, false, false, false, true, false, // false, true, false, false, // null); // mouseevent.fireevent(table, me); // me = new mouseevent(mouseevent.mouse_clicked, scenex, sceney, // screenx, screeny, mousebutton.primary, // 1, false, false, false, false, true, false, false, true, false, // false, null); // mouseevent.fireevent(table, me); // event.fireevent(your node, new // mouseevent(mouseevent.mouse_clicked, // 0, 0, 0, 0, mousebutton.primary, 1, true, true, true, true, true, // true, true, true, true, true, null)); // table.fireevent(event); }); button golast = new button(">|"); golast.setonaction(e -> { int ind = table.getitems().size() - 1; table.getselectionmodel().clearandselect(ind); table.scrollto(ind); }); hbox hbox = new hbox(gofist, golast); return hbox; } private node createinfocontrol() { gridpane grid = new gridpane(); grid.sethgap(10); grid.setvgap(5); stringbinding binding = new stringbinding() { { bind(table.editingcellproperty()); } @override protected string computevalue() { tableposition<person, ?> pos = table.geteditingcell(); string ret = (pos != null) ? (pos.getrow() + "." + pos .getcolumn()) : ""; return ret; } }; addlabelandval("editingcell:", binding, grid, 0, 0); addlabelandval("table.focused:", table.focusedproperty().asstring(), grid, 0, 2); addlabelandval("selm.selected:", table.getselectionmodel() .selectedindexproperty().asstring(), grid, 1, 0); addlabelandval("needslayout:", table.needslayoutproperty().asstring(), grid, 1, 2); addlabelandval("selected", table.getselectionmodel() .getselectedindices(), grid, 2, 0); return grid; } private void addlabelandval(string labeltxt, observablevalue<string> val, gridpane grid, int rowind, int colind) { label lab = new label(labeltxt); lab.setid(id_desc_label); label vallab = new label(); vallab.textproperty().bind(val); grid.add(lab, colind, rowind); grid.add(vallab, colind + 1, rowind); } private void addlabelandval(string labeltxt, final observablelist<integer> val, gridpane grid, int rowind, int colind) { label lab = new label(labeltxt); lab.setid(id_desc_label); label vallab = new label(); val.addlistener(new listchangelistener<integer>() { @override public void onchanged( javafx.collections.listchangelistener.change<? extends integer> c) { vallab.settext(val.tostring()); } }); vallab.settext(val.tostring()); grid.add(lab, colind, rowind); grid.add(vallab, colind + 1, rowind); } }
and model:
import javafx.beans.property.simplestringproperty; public class person { private final simplestringproperty firstname; private final simplestringproperty lastname; private final simplestringproperty email; public person(string fname, string lname, string email) { this.firstname = new simplestringproperty(fname); this.lastname = new simplestringproperty(lname); this.email = new simplestringproperty(email); } public string getfirstname() { return firstname.get(); } public void setfirstname(string fname) { firstname.set(fname); } public string getlastname() { return lastname.get(); } public void setlastname(string fname) { lastname.set(fname); } public string getemail() { return email.get(); } public void setemail(string fname) { email.set(fname); } }
Comments
Post a Comment