3

I followed this example mentioned on this link - UITableView - Better Editing through Binding? I changed it a bit accordingly

Model class - 

     public static class TableData {

        private String firstName, lastName;

        private TableData(String first, String last) {
            this.firstName = first;
            this.lastName = last;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

    }

Custom Cell factory -

     public static class TextFieldCellFactory implements Callback<TableColumn<TableData, String>, TableCell<TableData, String>> {

    @Override
    public TableCell<TableData, String> call(TableColumn<TableData, String> param) {
        TextFieldCell textFieldCell = new TextFieldCell();
        return textFieldCell;
    }

    public static class TextFieldCell extends TableCell<TableData, String> {

        private TextField textField;
        private StringProperty boundToCurrently = null;
        private String newval = "";

        public TextFieldCell() {


            textField = new TextField();
            textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent t) {
                    if (t.getCode() == KeyCode.ENTER) {
                        System.out.println("key pressed");
                        commitEdit(textField.getText());

                    } else if (t.getCode() == KeyCode.ESCAPE) {
                        cancelEdit();
                    }
                }
            });

            textField.textProperty().addListener(new ChangeListener<String>() {

                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
             // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

                    // commitEdit(newValue);
                    System.out.println("" + newValue);
                    newval = newValue;
                }

            });

            textField.focusedProperty().addListener(new ChangeListener<Boolean>() {

                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

                        if(!newValue){
                        System.out.println("losing focus" + newval);
                        //commichange();
                        commitEdit(textField.getText());
                        }

                    }
                });



            this.setGraphic(textField);
        }

        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (!empty) {
                // Show the Text Field
                this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                textField.setText(item);



            } else {
                this.setContentDisplay(ContentDisplay.TEXT_ONLY);
            }
        }

    }
}

setting onEditCommit-

c1.setOnEditCommit(
            new EventHandler<TableColumn.CellEditEvent<TableData, String>>() {
                @Override
                public void handle(TableColumn.CellEditEvent<TableData, String> t) {
                    System.out.println("ON edit commit" + t);
                    ((TableData) t.getTableView().getItems().get(
                            t.getTablePosition().getRow())).setFirstName(t.getNewValue());
                }
            }
    );
  1. Problem 1- I want to know what happens when commitEdit() is called. Does it invoke setOnEditCommit ? If it does then why its not invoking setOnEditCoommit
  2. Problem 2 - Why its not going into setOnEditCommit?
  3. Problem 3 I applied startEdit just to check if its enters that field. But that method also is not getting invoked.

Can anyone specify what i am missing here.I don't want a workaround. I need to understand whats the reason behind it P.S I have removed the binding properties as given in the link.

Community
  • 1
  • 1
Gautam
  • 3,252
  • 3
  • 23
  • 32
  • Can you remove all the superfluous code from the question? I assume the style (which should be in an external stylesheet, using CSS pseudoclasses instead of listeners) makes no difference to your question, and the commented out code is just a distraction. It would also be helpful if you could create an [MCVE](http://stackoverflow.com/help/mcve). – James_D Mar 13 '15 at 12:29

1 Answers1

0

Your table never enters an editing state (because you never ask it to). Because the cell never has isEditing() return true, the default commitEdit() method becomes a no-op.

You need the TableView to know that it has to start editing a cell when the text field in that cell receives focus. You can do this by modifying the focus listener on the text field:

                textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                        if (newValue) {
                            getTableView().edit(getIndex(), getTableColumn());
                        } else {
                            commitEdit(textField.getText());
                        }
                    }
                });
James_D
  • 201,275
  • 16
  • 291
  • 322
  • 1
    Thanks a lot james. I get it what i was missing. One more question.though, the custom cell factory implementation in oracle documentation mentioned here http://docs.oracle.com/javafx/2/ui_controls/table-view.htm (Example 12-11) , how they are are invoking table in editing state ? Which piece of code does that ? – Gautam Mar 13 '15 at 14:45
  • 2
    In that example the cell enters an editing state when the user double-clicks in it; that's implemented by the default behavior mechanism that (somehow, somewhere) detects that double click, tell the table view to edit the cell, and that in turn calls the `startEdit(...)` method on the cell. So they override `startEdit()` just to change the appearance of the cell. In your case, you effectively have to modify the behavior, to initiate editing not on a double-click but when the text field gets focus. – James_D Mar 13 '15 at 15:12
  • could you give a example about put the focus in the textfield? – Doberon Jun 22 '16 at 19:38
  • @Doberon What do you mean? – James_D Jun 22 '16 at 19:42
  • @James_D I wanna a way to edit a Textfield value with only one click, could you give some reference or example please? – Doberon Jun 22 '16 at 20:01
  • That's what this answer does. – James_D Jun 22 '16 at 20:01