如何在编辑后获取TableView列数据?

时间:2014-07-01 09:55:55

标签: java javafx-8

我在编辑后尝试获取数据,但一次又一次地获取以前的值。我在onEditStart上使用了一个onEditCancel属性,比如 - :

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

在控制器文件中实现oneditStart_0和onEditEnd_0以及editCommit_0。

public void editStart_0()
    {

        int  a = tableView.getSelectionModel().getSelectedItem().getValue_0();
       System.out.println("Getting Sales column data on Update -"+a);
       String b = value_0.getText();
       System.out.println("\"Getting Sales column data on Update -"+b);

    }

  public void editCommit_0(CellEditEvent<String,String> event) 
    {

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




    public void editEnd_0()
    {

        int  a = tableView.getSelectionModel().getSelectedItem().getValue_0();
       System.out.println("Getting Sales column data after Update -"+a);
    }

编辑前 enter image description here 编辑后 enter image description here 输出 - :

Getting Sales column data on Update -29

"Getting Sales column data on Update -Description Of Goods

Getting Sales column data after Update -29

但是当我在更新后进入它抱怨 - :

错误 - :

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)

请建议我如何从单元格中检索已编辑的值。

Complete Code -:

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 ;


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

        //sale_invoiceNo.setEditable(false);
        //  col_id.setEditable(false);

           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"));

            value_0.setCellValueFactory(new PropertyValueFactory<Record, 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"));




        /*
            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<String,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);
                    */

            String a,b,c,d,e;
            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;
    }


}

1 个答案:

答案 0 :(得分:1)

您需要实现onEditCommit,方法与实现其他方法的方式相同。 onEditCancel定义(顾名思义)用户取消编辑时调用的方法(通常按Escape)。

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

public void editCommit_0(CellEditEvent<S,T> event) {
    System.out.println("Value edited: new value is "+event.getNewValue());
}

您可以分别用ST的相应类型替换TableViewTableColumn