Search in sources :

Example 41 with KeyEvent

use of javafx.scene.input.KeyEvent in project trex-stateless-gui by cisco-system-traffic-generator.

the class TextFieldTableViewCell method call.

@Override
public TableCell call(TableColumn param) {
    return new TableCell<S, T>() {

        private TextField textField;

        {
            textField = new TextField();
            textField.setPrefSize(158, 22);
            textField.setOnKeyPressed((KeyEvent event) -> {
                if (event.getCode().equals(KeyCode.ENTER)) {
                    commitEdit((T) textField.getText());
                }
            });
            textField.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
                if (!newValue) {
                    commitEdit((T) textField.getText());
                }
            });
            textField.textProperty().bindBidirectional(textProperty());
        }

        @Override
        public void updateItem(final T item, final boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                setGraphic(textField);
                setText((String) getItem());
            }
        }

        @Override
        public void commitEdit(T item) {
            if (!isEditing() && !item.equals(getItem())) {
                TableView<S> table = getTableView();
                if (table != null) {
                    TableColumn<S, T> column = getTableColumn();
                    CellEditEvent<S, T> event = new CellEditEvent<>(table, new TablePosition<S, T>(table, getIndex(), column), TableColumn.editCommitEvent(), item);
                    Event.fireEvent(column, event);
                }
            }
            super.commitEdit(item);
        }
    };
}
Also used : KeyEvent(javafx.scene.input.KeyEvent) TableCell(javafx.scene.control.TableCell) ObservableValue(javafx.beans.value.ObservableValue) TextField(javafx.scene.control.TextField) CellEditEvent(javafx.scene.control.TableColumn.CellEditEvent)

Aggregations

KeyEvent (javafx.scene.input.KeyEvent)41 KeyCode (javafx.scene.input.KeyCode)10 ObservableValue (javafx.beans.value.ObservableValue)9 Scene (javafx.scene.Scene)8 Test (org.junit.Test)8 TextField (javafx.scene.control.TextField)7 Stage (javafx.stage.Stage)7 Application (javafx.application.Application)6 ChangeListener (javafx.beans.value.ChangeListener)6 EventHandler (javafx.event.EventHandler)6 IOException (java.io.IOException)5 Insets (javafx.geometry.Insets)5 KeyCombination (javafx.scene.input.KeyCombination)5 MouseEvent (javafx.scene.input.MouseEvent)5 Pane (javafx.scene.layout.Pane)5 List (java.util.List)4 Event (javafx.event.Event)4 FXML (javafx.fxml.FXML)4 Label (javafx.scene.control.Label)4 Tooltip (javafx.scene.control.Tooltip)4