java - JavaFX Error when trying to make Tableview Clickable -
i trying make tableview clickable, return text in cell clicked. receiving 2 errors when trying compile in netbeans. of code taken "example 12-11: alternative solution of cell editing" official tableview tutorial & from stackoverflow.com answer. here errors:
- type argument mouseevent not within bounds of type-variable t cell.addeventfilter(mouseevent.mouse_clicked, new eventhandler() { t type-variable: t extends event declared in interface eventhandler
- method addeventfilter in class node cannot applied given types; cell.addeventfilter(mouseevent.mouse_clicked, new eventhandler() { required: eventtype,eventhandler found: int,> reason: no instance(s) of type variable(s) t exist argument type int conforms formal parameter type eventtype t type-variable: t extends event declared in method addeventfilter(eventtype,eventhandler)
import java.awt.event.mouseevent; import javafx.application.application; import javafx.beans.property.simplestringproperty; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.geometry.insets; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.control.tablecell; import javafx.scene.control.tablecolumn; import javafx.scene.control.tablecolumn.celleditevent; import javafx.scene.control.tableview; import javafx.scene.control.textfield; import javafx.scene.control.cell.propertyvaluefactory; import javafx.scene.layout.hbox; import javafx.scene.layout.vbox; import javafx.scene.text.font; import javafx.stage.stage; import javafx.util.callback; public class tableviewsample extends application { private tableview<person> table = new tableview<>(); 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")); final hbox hb = new hbox(); public static void main(string[] args) { launch(args); } @override public void start(stage stage) { scene scene = new scene(new group()); stage.settitle("table view sample"); stage.setwidth(450); stage.setheight(550); final label label = new label("address book"); label.setfont(new font("arial", 20)); table.seteditable(false); callback<tablecolumn, tablecell> cellfactory = new callback<tablecolumn, tablecell>() { public tablecell call(tablecolumn p) { tablecell cell = new tablecell<person, string>() { @override public void updateitem(string item, boolean empty) { super.updateitem(item, empty); settext(empty ? null : getstring()); setgraphic(null); } private string getstring() { return getitem() == null ? "" : getitem().tostring(); } }; cell.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()); } } }); return cell; } }; tablecolumn firstnamecol = new tablecolumn("first name"); firstnamecol.setminwidth(100); firstnamecol.setcellvaluefactory( new propertyvaluefactory<person, string>("firstname")); firstnamecol.setcellfactory(cellfactory); firstnamecol.setoneditcommit( new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems().get( t.gettableposition().getrow()) ).setfirstname(t.getnewvalue()); } } ); tablecolumn lastnamecol = new tablecolumn("last name"); lastnamecol.setminwidth(100); lastnamecol.setcellvaluefactory( new propertyvaluefactory<person, string>("lastname")); lastnamecol.setcellfactory(cellfactory); lastnamecol.setoneditcommit( new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems().get( t.gettableposition().getrow()) ).setlastname(t.getnewvalue()); } } ); tablecolumn emailcol = new tablecolumn("email"); emailcol.setminwidth(200); emailcol.setcellvaluefactory( new propertyvaluefactory<person, string>("email")); emailcol.setcellfactory(cellfactory); emailcol.setoneditcommit( new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems().get( t.gettableposition().getrow()) ).setemail(t.getnewvalue()); } } ); table.setitems(data); table.getcolumns().addall(firstnamecol, lastnamecol, emailcol); 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(); } }); hb.getchildren().addall(addfirstname, addlastname, addemail, addbutton); hb.setspacing(3); final vbox vbox = new vbox(); vbox.setspacing(5); vbox.setpadding(new insets(10, 0, 0, 10)); vbox.getchildren().addall(label, table, hb); ((group) scene.getroot()).getchildren().addall(vbox); stage.setscene(scene); stage.show(); } public static class person { private final simplestringproperty firstname; private final simplestringproperty lastname; private final simplestringproperty email; private 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); } } class editingcell extends tablecell<person, string> { private textfield textfield; public editingcell() { } @override public void startedit() { if (!isempty()) { super.startedit(); createtextfield(); settext(null); setgraphic(textfield); textfield.selectall(); } } @override public void canceledit() { super.canceledit(); settext((string) getitem()); setgraphic(null); } @override public void updateitem(string item, boolean empty) { super.updateitem(item, empty); if (empty) { settext(null); setgraphic(null); } else { if (isediting()) { if (textfield != null) { textfield.settext(getstring()); } settext(null); setgraphic(textfield); } else { settext(getstring()); setgraphic(null); } } } private void createtextfield() { textfield = new textfield(getstring()); textfield.setminwidth(this.getwidth() - this.getgraphictextgap()* 2); textfield.focusedproperty().addlistener(new changelistener<boolean>(){ @override public void changed(observablevalue<? extends boolean> arg0, boolean arg1, boolean arg2) { if (!arg2) { commitedit(textfield.gettext()); } } }); } private string getstring() { return getitem() == null ? "" : getitem(); } } }
you have wrong import mouseevent
. need javafx.scene.input.mouseevent
, not awt version.
Comments
Post a Comment