如何解决事件onEditCommit上的类型不匹配错误?

我在Fxml中使用了onEditCommit事件来在用户编辑后检索数据。

FXML代码 – :

 

实现onEditCommit事件 – :

 public void editCommit_0(CellEditEvent event) { System.out.println("Value edited: On edit new value is "+event.getNewValue()); } 

错误 – :

 Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1763) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.event.Event.fireEvent(Event.java:204) at javafx.scene.control.TableCell.commitEdit(TableCell.java:335) at javafx.scene.control.cell.CellUtils$4.handle(CellUtils.java:252) at javafx.scene.control.cell.CellUtils$4.handle(CellUtils.java:243) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.event.Event.fireEvent(Event.java:204) at javafx.scene.Scene$KeyHandler.process(Scene.java:3949) at javafx.scene.Scene$KeyHandler.access$2100(Scene.java:3896) at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2036) at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2493) at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:170) at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:123) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:197) at com.sun.glass.ui.View.handleKeyEvent(View.java:517) at com.sun.glass.ui.View.notifyKey(View.java:927) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39) at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112) at java.lang.Thread.run(Thread.java:745) 

完整代码 – :

 Controller.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.techi.calco.transaction.controller; import com.techi.calco.Main; import com.techi.calco.model.ItemGroup; import com.techi.calco.util.EditingCell; import com.techi.calco.model.Record; import com.techi.calco.service.ItemService; import com.techi.calco.util.ButtonCell; import java.net.URL; import java.util.Random; import java.util.ResourceBundle; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TreeTableColumn.CellEditEvent; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.control.cell.TextFieldTableCell; import javafx.util.Callback; import np.com.ngopal.control.AutoFillTextBox; /** * FXML Controller class * * @author Vipin Bhatt */ public class SaleInvoiceController implements Initializable { public static ObservableList data = FXCollections.observableArrayList(); //public static ObservableList data = FXCollections.observableArrayList(); // Implemented 25 6 ObservableList data5; private ObservableList data2 = FXCollections.observableArrayList(); ItemService itemService = itemService=(ItemService)Main.applicationContext.getBean("itemService"); private ObservableList vatData = FXCollections.observableArrayList(); String[] smooth; String[] items; static int sk5; @FXML private TableView tableView ; //private TableView tableView ; @FXML private AutoFillTextBox autoFill; @FXML private Button addNew,btnNew; static Random random = new Random(); @FXML //private TableColumn col_id,value_0,value_1,value_2,value_3,value_4 ,col_Action,sale_invoiceNo; private TableColumn col_id,value_0,value_1,value_2,value_3,value_4 ,col_Action,sale_invoiceNo; //index for delete Item private IntegerProperty index = new SimpleIntegerProperty(); /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { // TODO System.out.println("Get text saleinvoice--->"+btnNew.getText()); tableView.setEditable(true); index.set(-1); Callback cellFactory = new Callback() { @Override public TableCell call(TableColumn p) { return new EditingCell(); } }; // searchAction("a"); col_id.setCellValueFactory(new PropertyValueFactory("id")); //col_id.setCellValueFactory(new PropertyValueFactory("id")); value_0.setCellValueFactory(new PropertyValueFactory("value_0")); //value_0.setCellValueFactory(new PropertyValueFactory("value_0")); //value_0.setCellValueFactory(new PropertyValueFactory("itemGroupName")); //value_1.setCellValueFactory(new PropertyValueFactory("value_1")); // value_2.setCellValueFactory(new PropertyValueFactory("value_2")); //value_3.setCellValueFactory(new PropertyValueFactory("value_3")); //value_4.setCellValueFactory(new PropertyValueFactory("value_4")); System.out.println("\ngettiing Cell value factore-:" +value_0.getCellValueFactory()); /* col_Action.setCellValueFactory( new Callback<TableColumn.CellDataFeatures, ObservableValue>() { @Override public ObservableValue call(TableColumn.CellDataFeatures p) { return new SimpleBooleanProperty(p.getValue() != null); } }); col_Action.setCellFactory( new Callback<TableColumn, TableCell>() { @Override public TableCell call(TableColumn p) { return new ButtonCell(); } }); */ //value_0.setCellFactory(cellFactory); //value_1.setCellFactory(cellFactory); //value_2.setCellFactory(cellFactory); //value_3.setCellFactory(cellFactory); value_0.setCellFactory(TextFieldTableCell.forTableColumn()) ; //value_1.setCellFactory(TextFieldTableCell.forTableColumn()) ; // value_2.setCellFactory(TextFieldTableCell.forTableColumn()) ; //value_3.setCellFactory(TextFieldTableCell.forTableColumn()) ; tableView.setItems(data); searchAction("a"); } //Implemented 25 6 public String searchAction(String value) { System.out.println("Inside searchAction() SaleInvoiceController inside transaction.controller->"+itemService); //itemService = new ItemService(); data2=itemService.searchAction(value); String quick = data2.get(0).getItemGroupName(); System.out.println("Checking quick ->"+quick); String[] smooth = new String[data2.size()]; for (int i=0;i" + quick); smooth = new String[data.size()]; for (int i = 0; i < data.size(); i++) { smooth[i] = data2.get(i).getItemGroupName()+"-"+data2.get(i).getId(); } for (String str : smooth) { System.out.println("-------" + str); } data5 = FXCollections.observableArrayList(smooth); autoFill.setData(data5); return value; */ } public void editStart_0() { String a = tableView.getSelectionModel().getSelectedItem().getValue_0(); System.out.println("Getting Sales column data before Update -"+a); } public void editEnd_0() { String a = tableView.getSelectionModel().getSelectedItem().getValue_0(); System.out.println("Getting Sales column data after Update -"+a); } public void editCommit_0(CellEditEvent event) { // System.out.println("Value edited: On edit new value is "+event.getNewValue()); } public void newRecord(ActionEvent event) { System.out.println("Hello Add NEw Record"); //generate new Record with random number int newId = data.size()+1; /* Record newRec = new Record( newId, random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100)); data.add(newRec); */ Record newRec = new Record( newId,"ram1","ram2","ram3","ram4","ram5"); data.add(newRec); System.out.println("LIST SIZE---->"+SaleInvoiceController.data.size()); } public void addNew(ActionEvent event) { System.out.println("addNew"); } } 

Record.java(getter setter)

  private int id; private String value_0; private String value_1; private String value_2; private String value_3; private String value_4; public Record(int i,String a, String b, String c, String d, String e) { this.id = i; this.value_0 = a; this.value_1 = b; this.value_2=c; this.value_3=d; this.value_4 = e; } /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @return the value_0 */ public String getValue_0() { return value_0; } /** * @param value_0 the value_0 to set */ public void setValue_0(String value_0) { this.value_0 = value_0; } /** * @return the value_1 */ public String getValue_1() { return value_1; } /** * @param value_1 the value_1 to set */ public void setValue_1(String value_1) { this.value_1 = value_1; } /** * @return the value_2 */ public String getValue_2() { return value_2; } /** * @param value_2 the value_2 to set */ public void setValue_2(String value_2) { this.value_2 = value_2; } /** * @return the value_3 */ public String getValue_3() { return value_3; } /** * @param value_3 the value_3 to set */ public void setValue_3(String value_3) { this.value_3 = value_3; } /** * @return the value_4 */ public String getValue_4() { return value_4; } /** * @param value_4 the value_4 to set */ public void setValue_4(String value_4) { this.value_4 = value_4; } } 

请帮我解决这个错误,我应该在onEditCommit事件实现中放置什么类型。 我已打印柱单元值工厂

 value_0.getCellValueFactory() 

得到了

 javafx.scene.control.cell.PropertyValueFactory@5941ae6a 

EditingCell.java – :

 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.techi.calco.util; import javafx.event.EventHandler; import javafx.scene.chart.XYChart; import javafx.scene.control.ContentDisplay; import javafx.scene.control.TableCell; import javafx.scene.control.TextField; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; /** * * @author Vipin Bhatt */ /* public class EditingCell extends TableCell { private TextField textField; public EditingCell() {} @Override public void startEdit() { System.out.println("StarEdit Called when"); super.startEdit(); System.out.println("Editing cell"); if (textField == null) { createTextField(); } setGraphic(textField); setContentDisplay(ContentDisplay.GRAPHIC_ONLY); textField.selectAll(); } @Override public void cancelEdit() { super.cancelEdit(); setText(String.valueOf(getItem())); setContentDisplay(ContentDisplay.TEXT_ONLY); } @Override public void updateItem(Number item, boolean empty) { super.updateItem(item, empty); if (empty) { setText(null); setGraphic(null); } else { if (isEditing()) { if (textField != null) { textField.setText(getString()); } setGraphic(textField); setContentDisplay(ContentDisplay.GRAPHIC_ONLY); } else { setText(getString()); setContentDisplay(ContentDisplay.TEXT_ONLY); } } } private void createTextField() { textField = new TextField(getString()); textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()*2); textField.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent t) { if (t.getCode() == KeyCode.ENTER) { commitEdit(Integer.parseInt(textField.getText())); } else if (t.getCode() == KeyCode.ESCAPE) { cancelEdit(); } } }); } private String getString() { return getItem() == null ? "" : getItem().toString(); } } */ public class EditingCell extends TableCell { private TextField textField; public EditingCell() {} @Override public void startEdit() { super.startEdit(); if (textField == null) { createTextField(); } setGraphic(textField); setContentDisplay(ContentDisplay.GRAPHIC_ONLY); textField.selectAll(); } @Override public void cancelEdit() { super.cancelEdit(); setText(String.valueOf(getItem())); setContentDisplay(ContentDisplay.TEXT_ONLY); } @Override public void updateItem(Number item, boolean empty) { super.updateItem(item, empty); if (empty) { setText(null); setGraphic(null); } else { if (isEditing()) { if (textField != null) { textField.setText(getString()); } setGraphic(textField); setContentDisplay(ContentDisplay.GRAPHIC_ONLY); } else { setText(getString()); setContentDisplay(ContentDisplay.TEXT_ONLY); } } } private void createTextField() { textField = new TextField(getString()); textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()*2); textField.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent t) { if (t.getCode() == KeyCode.ENTER) { commitEdit(Integer.parseInt(textField.getText())); } else if (t.getCode() == KeyCode.ESCAPE) { cancelEdit(); } } }); } private String getString() { return getItem() == null ? "" : getItem().toString(); } } 

您的事件处理程序类型对我来说很好。 我的猜测是其他地方出现的类型不匹配。

样品

下面的示例演示了在使用FXML时设置提交事件处理程序。 对于冗长的道歉,这就是FXML的方式。

示例中的编辑提交处理程序是:

 @FXML void commitValue(TableColumn.CellEditEvent event) { System.out.println("Commit: " + event.getNewValue()); } 

其中Singluar只是TableView中用于样本的记录类的名称。

提交/ CommitController.java

 import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.fxml.FXML; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.control.cell.TextFieldTableCell; public class CommitController { @FXML private TableView table; @FXML private TableColumn value; @FXML void commitValue(TableColumn.CellEditEvent event) { System.out.println("Commit: " + event.getNewValue()); } public void initialize() { value.setCellFactory(TextFieldTableCell.forTableColumn()); value.setCellValueFactory(new PropertyValueFactory<>("value")); value.setEditable(true); table.getItems().setAll( new Singular("enie"), new Singular("meenie"), new Singular("minie"), new Singular("moe"), new Singular("just commit!") ); table.setEditable(true); } public static class Singular { private StringProperty value = new SimpleStringProperty(); public Singular(String value) { this.value.setValue(value); } public StringProperty valueProperty() { return value; } } } 

提交/ CommitmentApp.java

 import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; import java.io.IOException; public class CommitmentApp extends Application { @Override public void start(Stage stage) throws IOException { stage.setScene(new Scene(createContent())); stage.show(); } private Parent createContent() throws IOException { FXMLLoader loader = new FXMLLoader(); return loader.load( getClass().getResourceAsStream("commit.fxml") ); } public static void main(String[] args) { launch(args); } } 

commit.fxml

        

我做了这些改变。 在EditingCell.java中

 public class EditingCell extends TableCell { 

在EditController.jva中只添加了TableColumn

 public void columnEdit(TableColumn.CellEditEvent event)