java - Has anyone figured out how to make a javafx tableview act like a jtable? -


i followed example 13-11 alternative solution of cell editing offical tableview tutorial, want tableview act jtable. means when cell gets focus, ready edited, , using arrow keys or enter key should instantly commit edit , move next cell.

this i've gone far:

first, added

table.getselectionmodel().setcellselectionenabled(true); 

then tried modify class editingcell:

class editingcell extends tablecell<person, string> {      private textfield textfield;      public editingcell() {     }      @override     public void updateselected(boolean selected) {         super.updateselected(selected);         if (selected) {             createtextfield();             settext(null);             setgraphic(textfield);             textfield.requestfocus();             textfield.selectall();         } else {             string value = textfield.gettext();             if (value != null) {                 commitedit(value);             } else {                 commitedit(null);             }         }     }       @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());                 }             }         });         textfield.setonkeypressed(new eventhandler<keyevent>() {             @override             public void handle(keyevent t) {                 if ((t.getcode() == keycode.enter) || (t.getcode() == keycode.up) || (t.getcode() == keycode.down) || (t.getcode() == keycode.left) || (t.getcode() == keycode.right)) {                     t.consume();                     string value = textfield.gettext();                     if (value != null) {                         commitedit(value);                     } else {                         commitedit(null);                     }                 } else if (t.getcode() == keycode.escape) {                     canceledit();                 }             }         });     }      private string getstring() {         return getitem() == null ? "" : getitem().tostring();     } } 

the tableview got mess - have click enter key twice end editing, , won't commit edit instead cancels it.

can point me in right direction?

sadly tutorial missing code fixes introduced later in textfieldtablecell class enter work (rt-34685 - use onaction instead of onkeypressed , rt-28132 - call requestfocus()).

here working example, it's reduced version of textfieldtablecell code uses node.fireevent() move next cell after commit (mimics keypressed event):

class editingcell extends tablecell<person, string> {     private textfield   textfield;      public editingcell()     {     }      @override     public void startedit()     {         if (!iseditable() || !gettableview().iseditable()                 || !gettablecolumn().iseditable())         {             return;         }         super.startedit();          if (isediting())         {             if (textfield == null)             {                 createtextfield();             }             settext(null);             setgraphic(textfield);             textfield.selectall();             // requesting focus key input can go             // textfield (see rt-28132)             textfield.requestfocus();         }     }      @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());                 }             }         });          // use onaction here rather onkeyreleased (with check         // enter), otherwise encounter rt-34685         textfield.setonaction(t -> {             commitedit(textfield.gettext());             t.consume();         });         textfield.setonkeyreleased(t -> {             if (t.getcode() == keycode.escape)             {                 canceledit();                 t.consume();             }         });          textfield.setonkeypressed(new eventhandler<keyevent>()         {             @override             public void handle(keyevent t)             {                 if ((t.getcode() == keycode.up) || (t.getcode() == keycode.down)                         || (t.getcode() == keycode.left)                         || (t.getcode() == keycode.right))                 {                     // commit current text                     commitedit(textfield.gettext());                      // let's move out simulating key press in cell                     keyevent event = new keyevent(t.getsource(), t.gettarget(),                             keyevent.key_pressed, "", "", t.getcode(), false, false,                             false, false);                     editingcell.this.fireevent(event);                 }             }         });     }      private string getstring()     {         return getitem() == null ? "" : getitem().tostring();     } } 

hope helps!


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -