一尘不染

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

java

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

FXML代码-:

<TableColumn editable="true" maxWidth="5000.0" minWidth="10.0" onEditCancel="#editEnd_0" onEditStart="#editStart_0" onEditCommit="#editCommit_0" prefWidth="136.0" text="Description Of Goods" fx:id="value_0" />

实现了onEditCommit事件-:

public void editCommit_0(CellEditEvent<Record,String> 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<Record> data = FXCollections.observableArrayList();
 //public  static  ObservableList<String> data = FXCollections.observableArrayList();



  // Implemented 25 6
     ObservableList<String> data5;
     private ObservableList<ItemGroup>  data2 = FXCollections.observableArrayList();

     ItemService itemService =  itemService=(ItemService)Main.applicationContext.getBean("itemService");


    private ObservableList<ItemGroup> vatData = FXCollections.observableArrayList();


    String[] smooth;
    String[] items;
    static int  sk5;

     @FXML
    private TableView<Record>  tableView ;
     //private TableView<String>  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<Record, String> 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<TableColumn, TableCell> cellFactory =
                new Callback<TableColumn, TableCell>() 
                {

                    @Override
                    public TableCell call(TableColumn p) {
                        return new EditingCell();
                    }
                };


          // searchAction("a");



          col_id.setCellValueFactory(new PropertyValueFactory<Record, String>("id"));
            //col_id.setCellValueFactory(new PropertyValueFactory<String, String>("id"));

          value_0.setCellValueFactory(new PropertyValueFactory<Record, String>("value_0"));
            //value_0.setCellValueFactory(new PropertyValueFactory<String, String>("value_0"));

            //value_0.setCellValueFactory(new PropertyValueFactory<ItemService, String>("itemGroupName"));


            //value_1.setCellValueFactory(new PropertyValueFactory<Record, Integer>("value_1"));
           // value_2.setCellValueFactory(new PropertyValueFactory<Record, String>("value_2"));
            //value_3.setCellValueFactory(new PropertyValueFactory<Record, String>("value_3"));
            //value_4.setCellValueFactory(new PropertyValueFactory<Record, String>("value_4"));


                 System.out.println("\ngettiing Cell value factore-:" +value_0.getCellValueFactory());


        /*
            col_Action.setCellValueFactory(
                new Callback<TableColumn.CellDataFeatures<Record, Boolean>, 
                ObservableValue<Boolean>>() {

            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Record, Boolean> p) {
                return new SimpleBooleanProperty(p.getValue() != null);
            }


        });

        col_Action.setCellFactory(
                new Callback<TableColumn<Record, Boolean>, TableCell<Record, Boolean>>() {

            @Override
            public TableCell<Record, Boolean> call(TableColumn<Record, Boolean> 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<data2.size();i++){

       //  smooth[i] = data2.get(i).getItemGroupName();
            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;


        /*
          data2 = itemService.searchAction(value);

        String quick = data2.get(0).getItemGroupName();
        System.out.println("Checking quick ->" + 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<Record,String> 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(获取设置程序)

  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<XYChart.Data, Number> {


     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<KeyEvent>() {

                @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<XYChart.Data, Number> {

        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<KeyEvent>() {

                @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();
        }
    }

阅读 138

收藏
2020-12-03

共1个答案

一尘不染

您的事件处理程序类型对我来说不错。我的猜测是类型不匹配正在其他地方发生。

样品

这是一个示例,演示了使用FXML时如何设置提交事件处理程序。为冗长而道歉,这就是FXML的方式。

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

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

其中Singluar只是该示例在TableView中使用的记录类的名称。

commit / 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<Singular> table;

    @FXML
    private TableColumn<Singular, String> value;

    @FXML
    void commitValue(TableColumn.CellEditEvent<Singular, String> 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;
        }
    }
}

commit / 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

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<TableView fx:id="table" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="130.0" prefWidth="113.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="commit.CommitController">
  <columns>
    <TableColumn fx:id="value" onEditCommit="#commitValue" prefWidth="85.0" text="C1" />
  </columns>
</TableView>
2020-12-03