How to solve type mismatch error on event onEditCommit?

后端 未结 2 1329
挽巷
挽巷 2021-01-29 09:27

I have used onEditCommit event in Fxml to retrive data after user edit it.

FXML Code -:



        
相关标签:
2条回答
  • 2021-01-29 09:33

    I made these changes. in EditingCell.java

    public class EditingCell extends TableCell<Record, String> {
    

    in EditController.jva added just TableColumn

    public void columnEdit(TableColumn.CellEditEvent<Record,String> event)
    
    0 讨论(0)
  • 2021-01-29 09:43

    Your event handler type looks fine to me. My guess is the type mismatch is occurring elsewhere.

    Sample

    Here is a sample which demonstrates setting a commit event handler when using FXML. Apologies for the verbosity, that's just how FXML is.

    The edit commit handler in the sample is:

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

    where Singluar is just the name of the record class used in the TableView for the sample.

    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>
    
    0 讨论(0)
提交回复
热议问题