Java Code Examples for javafx.scene.control.TextField.addEventFilter()

The following are Jave code examples for showing how to use addEventFilter() of the javafx.scene.control.TextField class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: CSS-Editor-FX   File: OptionsController.java   Source Code and License Vote up 6 votes
private void createField() {
  field = new TextField(getString());
  field.setEditable(false);
  field.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
    if (e.getCode() == KeyCode.ENTER) {
      try {
        commitEdit(KeyCombination.valueOf(field.getText()));
      } catch (Exception ee) {
        cancelEdit();
      }
    } else if (e.getCode() == KeyCode.ESCAPE) {
      cancelEdit();
    } else {
      field.setText(convert(e).toString());
    }
    e.consume();
  });
}
 
Example 2
Project: fwm   File: HotkeyController.java   Source Code and License Vote up 5 votes
public static void giveSearchBarHotkey(TextField scene){
	scene.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
		public void handle(final KeyEvent keyEvent) {
			if (KeyCode.ENTER == keyEvent.getCode()) {
				try {
					App.getMainController().open(App.getMainController().getSearchList().getList().getItems().get(0));
					keyEvent.consume();
				} catch (Exception e) {
					
				}
			}
		}
	});
}
 
Example 3
Project: HueSense   File: UIUtils.java   Source Code and License Vote up 5 votes
public static void setTextFieldNumbersOnly(TextField field, int min, int max) {
    field.addEventFilter(KeyEvent.KEY_TYPED, (e) -> {

        if (e.getCharacter().matches("[0-9]")) {
            String text = field.getText();
            if (e.getCode() == KeyCode.BACK_SPACE && text.length() > 0) {
                text = text.substring(0, text.length() - 2);
            } else {
                text = text + e.getCharacter();
            }
            try {
                long value = Long.parseLong(text);
                if (value < min) {
                    e.consume();
                    field.setText(String.valueOf(min));
                } else if (value > max) {
                    e.consume();
                    field.setText(String.valueOf(max));
                }
            } catch (NumberFormatException ex) {
                e.consume();
            }
        } else {
            e.consume();
        }
    });
}
 
Example 4
Project: Himalaya-JavaFX   File: InitFXMLController.java   Source Code and License Vote up 5 votes
void lauch(Board b) {

        board = b;

        p1Label.setText("Joueur " + board.getPlayers().get(0).getColor());
        p2Label.setText("Joueur " + board.getPlayers().get(1).getColor());
        p3Label.setText("Joueur " + board.getPlayers().get(2).getColor());
        setPlayerInitialPosition(board.getPlayers().get(0), p1TextField);
        setPlayerInitialPosition(board.getPlayers().get(1), p2TextField);
        setPlayerInitialPosition(board.getPlayers().get(2), p3TextField);

        if (board.getPlayers().size() == 4) {
            p4Label.setText("Joueur " + board.getPlayers().get(3).getColor());
            setPlayerInitialPosition(board.getPlayers().get(3), p4TextField);
        } else {
            p4Label.setVisible(false);
            p4TextField.setVisible(false);
        }

        tf.add(p1TextField);
        tf.add(p2TextField);
        tf.add(p3TextField);
        tf.add(p4TextField);

        for (TextField textField : tf) {
            textField.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent inputevent) {
                    if (!inputevent.getCharacter().matches("\\d")) {
                        inputevent.consume();
                    }
                }
            });
        }
    }
 
Example 5
Project: fx-animation-editor   File: TextFieldHelper.java   Source Code and License Vote up 5 votes
public static void executeOnFocusLostOrEnter(TextField textField, Runnable runnable) {
    textField.focusedProperty().addListener((v, o, n) -> {
        if (!n) {
            runnable.run();
        }
    });
    textField.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
        if (event.getCode().equals(KeyCode.ENTER)) {
            runnable.run();
        }
    });
}