Reputation: 73
I'm designing a custom time control with a TextField and want to implement the function to increase the hours or minutes when pressing the UP-Key.
This is how i intended to do it:
textField.setOnKeyPressed(event -> {
int caretPos = editableNode.getCaretPosition();
switch (event.getCode()) {
case ESCAPE:
getSkinnable().reset();
event.consume();
break;
case UP:
if (!getSkinnable().isInvalid()){
if (caretPos < 3) {
getSkinnable().increaseHour();
selectHour();
} else {
getSkinnable().increaseMinute();
selectMinute();
}
}
event.consume();
break;
}
});
The problem here is that the UP-Key on default sets the caret to the beginning of the text.
I already found this post: JavaFX Textfield - Override default behaviour when up key is released
But the event that sets the caret at the beginning of the text comes before my eventhandler so this did not help me.
Does anyone know how I can "override" this behaviour?
Upvotes: 2
Views: 249
Reputation: 73
The solution of @James_D resolved my issue.
textField.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
int caretPos = editableNode.getCaretPosition();
switch (event.getCode()) {
case ESCAPE:
getSkinnable().reset();
event.consume();
break;
case UP:
if (!getSkinnable().isInvalid()){
if (caretPos < 3) {
getSkinnable().increaseHour();
selectHour();
} else {
getSkinnable().increaseMinute();
selectMinute();
}
}
event.consume();
break;
}
});
Upvotes: 1