Java Code Examples for javafx.scene.control.TextField#setOnKeyPressed()

The following examples show how to use javafx.scene.control.TextField#setOnKeyPressed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessAccountantController.java    From Hostel-Management-System with MIT License 6 votes vote down vote up
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();
            }
        }
    });
}
 
Example 2
Source File: EditingStrCell.java    From CPUSim with GNU General Public License v3.0 6 votes vote down vote up
/**
 * creates a text field with listeners so that that edits will be committed 
 * at the proper time
 */
protected void createTextField() {
    textField = new TextField(getString());
    textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
    textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> arg0,
                            Boolean arg1, Boolean arg2) {
            if (!arg2) {
                commitEdit(textField.getText());
            }
        }
    });
    textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent t) {
            if (t.getCode() == KeyCode.ENTER) {
                commitEdit(textField.getText());
            } else if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        }
    });
}
 
Example 3
Source File: EditingStrListCell.java    From CPUSim with GNU General Public License v3.0 6 votes vote down vote up
/**
 * creates a text field with listeners so that that edits will be committed 
 * at the proper time
 */
protected void createTextField() {
    textField = new TextField(getString());
    textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
    textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> arg0,
                            Boolean arg1, Boolean arg2) {
            if (!arg2) {
                commitEdit(textField.getText());
            }
        }
    });
    textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent t) {
            if (t.getCode() == KeyCode.ENTER) {
                commitEdit(textField.getText());
            } else if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        }
    });
}
 
Example 4
Source File: EditingStrStyleCell.java    From CPUSim with GNU General Public License v3.0 6 votes vote down vote up
/**
 * creates a text field with listeners so that that edits will be committed 
 * at the proper time
 */
protected void createTextField() {
    textField = new TextField(getString());
    textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
    textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> arg0,
                            Boolean arg1, Boolean arg2) {
            if (!arg2) {
                commitEdit(textField.getText());
            }
        }
    });
    textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent t) {
            if (t.getCode() == KeyCode.ENTER) {
                commitEdit(textField.getText());
            } else if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        }
    });
}
 
Example 5
Source File: DataSourceTreeItemInit.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
/**
 * 给 DataSourceBorderPane 设置键盘监听,用于搜索 table
 */
public void addListenOnDataSourceBorderPane(TreeView<DataItem> treeViewDataSource) {
    TextField tableFindTextField = mainController.getTableFindTextField();
    tableFindTextField.textProperty().addListener((observable, oldValue, newValue) -> {
        if (oldValue != null && newValue != null) {
            this.filterTables(newValue, treeViewDataSource.getRoot(), oldValue.length() > newValue.length());
        }
    });

    tableFindTextField.setOnKeyPressed(tableTextFieldListener::escListener);

    treeViewDataSource.setOnKeyPressed(tableTextFieldListener::ctrlFListener);
}
 
Example 6
Source File: TextAreaReadline.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public TextAreaReadline(TextField text, TextArea output, final String message) {
    this.area = text;
    this.output = output;
    readline = new Readline();
    inputJoin.send(Channel.EMPTY, null);
    text.setOnKeyPressed(this);
    if (message != null) {
        append(message, promptStyle);
    }
}
 
Example 7
Source File: RenamingTextField.java    From Recaf with MIT License 5 votes vote down vote up
private RenamingTextField(GuiController controller, String initialText, Consumer<RenamingTextField> renameAction) {
	this.controller = controller;
	setHideOnEscape(true);
	setAutoHide(true);
	setOnShown(e -> {
		// Center on main window
		Stage main = controller.windows().getMainWindow().getStage();
		int x = (int) (main.getX() + Math.round((main.getWidth() / 2) - (getWidth() / 2)));
		int y = (int) (main.getY() + Math.round((main.getHeight() / 2) - (getHeight() / 2)));
		setX(x);
		setY(y);
	});
	text = new TextField(initialText);
	text.getStyleClass().add("remap-field");
	text.setPrefWidth(400);
	// Close on hitting escape/close-window bind
	text.setOnKeyPressed(e -> {
		if (controller.config().keys().closeWindow.match(e) || e.getCode() == KeyCode.ESCAPE)
			hide();
	});
	// Set on-enter action
	text.setOnAction(e -> renameAction.accept(this));
	// Setup & show
	getScene().setRoot(text);
	Platform.runLater(() -> {
		text.requestFocus();
		text.selectAll();
	});
}
 
Example 8
Source File: EditAxis.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
private final TextField getBoundField(final Axis axis, final boolean isLowerBound) {
    final TextField textField = new TextField();

    // ValidationSupport has a slow memory leak
    // final ValidationSupport support = new ValidationSupport();
    // final Validator<String> validator = (final Control control, final
    // String value) -> {
    // boolean condition = value == null ? true :
    // !value.matches(NUMBER_REGEX);
    //
    // // additional check in case of logarithmic axis
    // if (!condition && axis.isLogAxis() && Double.parseDouble(value)
    // <= 0) {
    // condition = true;
    // }
    // // change text colour depending on validity as a number
    // textField.setStyle(condition ? "-fx-text-inner-color: red;" :
    // "-fx-text-inner-color: black;");
    // return ValidationResult.fromMessageIf(control, "not a number",
    // Severity.ERROR, condition);
    // };
    // support.registerValidator(textField, true, validator);

    final Runnable lambda = () -> {
        final double value;
        final boolean isInverted = axis.isInvertedAxis();
        if (isLowerBound) {
            value = isInverted ? axis.getMax() : axis.getMin();
        } else {
            value = isInverted ? axis.getMin() : axis.getMax();
        }
        textField.setText(Double.toString(value));
    };

    axis.invertAxisProperty().addListener((ch, o, n) -> lambda.run());
    axis.minProperty().addListener((ch, o, n) -> lambda.run());
    axis.maxProperty().addListener((ch, o, n) -> lambda.run());

    // force the field to be numeric only
    textField.textProperty().addListener((observable, oldValue, newValue) -> {
        if ((newValue != null) && !newValue.matches("\\d*")) {
            final double val;
            try {
                val = Double.parseDouble(newValue);
            } catch (NullPointerException | NumberFormatException e) {
                // not a parsable number
                textField.setText(oldValue);
                return;
            }

            if (axis.isLogAxis() && (val <= 0)) {
                textField.setText(oldValue);
                return;
            }
            textField.setText(Double.toString(val));
        }
    });

    textField.setOnKeyPressed(ke -> {
        if (ke.getCode().equals(KeyCode.ENTER)) {
            final double presentValue = Double.parseDouble(textField.getText());
            if (isLowerBound && !axis.isInvertedAxis()) {
                axis.setMin(presentValue);
            } else {
                axis.setMax(presentValue);
            }
            axis.setAutoRanging(false);

            if (axis instanceof AbstractAxis) {
                // ((AbstractAxis) axis).recomputeTickMarks();
                axis.setTickUnit(((AbstractAxis) axis).computePreferredTickUnit(axis.getLength()));
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("recompute axis tick unit to {}",
                            ((AbstractAxis) axis).computePreferredTickUnit(axis.getLength()));
                }
            }
        }
    });

    HBox.setHgrow(textField, Priority.ALWAYS);
    VBox.setVgrow(textField, Priority.ALWAYS);

    return textField;
}
 
Example 9
Source File: SelectedWidgetUITracker.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
/** Create an inline editor
 *
 *  <p>Depending on the widget's properties, it will edit
 *  the PV name or the text.
 *
 *  @param widget Widget on which to create an inline editor
 */
private void createInlineEditor(final Widget widget)
{
    // Check for an inline-editable property
    Optional<WidgetProperty<String>> check;

    // Defaulting to PV name or text property with some hard-coded exceptions.
    // Alternative if the list of hard-coded widgets grows:
    // Add Widget#getInlineEditableProperty()
    if (widget instanceof ActionButtonWidget)
        check = Optional.of(((ActionButtonWidget) widget).propText());
    else if (widget instanceof GroupWidget)
        check = Optional.of(((GroupWidget) widget).propName());
    else
        check = widget.checkProperty(CommonWidgetProperties.propPVName);
    if (! check.isPresent())
        check = widget.checkProperty(CommonWidgetProperties.propText);
    if (! check.isPresent())
        return;

    // Create text field, aligned with widget, but assert minimum size
    final MacroizedWidgetProperty<String> property = (MacroizedWidgetProperty<String>)check.get();
    inline_editor = new TextField(property.getSpecification());
    // 'Managed' text field would assume some default size,
    // but we set the exact size in here
    inline_editor.setManaged(false);
    inline_editor.setPromptText(property.getDescription()); // Not really shown since TextField will have focus
    inline_editor.setTooltip(new Tooltip(property.getDescription()));
    inline_editor.relocate(tracker.getX(), tracker.getY());
    inline_editor.resize(Math.max(100, tracker.getWidth()), Math.max(20, tracker.getHeight()));
    getChildren().add(inline_editor);

    // Add autocomplete menu if editing property PVName
    if (property.getName().equals(CommonWidgetProperties.propPVName.getName()))
        PVAutocompleteMenu.INSTANCE.attachField(inline_editor);

    // On enter or lost focus, update the property. On Escape, just close.
    final ChangeListener<? super Boolean> focused_listener = (prop, old, focused) ->
    {
        if (! focused)
        {
            if (!property.getSpecification().equals(inline_editor.getText()))
                undo.execute(new SetMacroizedWidgetPropertyAction(property, inline_editor.getText()));
            // Close when focus lost
            closeInlineEditor();
        }
    };

    inline_editor.setOnAction(event ->
    {
        undo.execute(new SetMacroizedWidgetPropertyAction(property, inline_editor.getText()));
        inline_editor.focusedProperty().removeListener(focused_listener);
        closeInlineEditor();
    });

    inline_editor.setOnKeyPressed(event ->
    {
        switch (event.getCode())
        {
        case ESCAPE:
            event.consume();
            inline_editor.focusedProperty().removeListener(focused_listener);
            closeInlineEditor();
        default:
        }
    });

    inline_editor.focusedProperty().addListener(focused_listener);

    inline_editor.selectAll();
    inline_editor.requestFocus();
}