问题
I have a Tab
with some content: ScrollBar
and other.
The ScrollBar
has event handler for keys: left and right.
But if I press these buttons, Tabs
are switched, because TabPane
also has a key handler.
How to disable default event handler for TabPane
or change switching policy?
回答1:
The problem is that a ScrollBar
is not focus traversable by default and key events are only fired for focused Node
s.
You can set the ScrollBar
focus traversable:
sb.setFocusTraversable(true);
sb.setOnKeyPressed(e -> {
if ( e.getCode().equals(KeyCode.RIGHT))
sb.setValue(sb.getValue()+0.01);
else if(e.getCode().equals(KeyCode.LEFT))
sb.setValue(sb.getValue()-0.01);
});
In this case whenever the ScrollBar
is focused, the key events are handled.
来源:https://stackoverflow.com/questions/42256969/javafx-tabpane-disable-tab-switching-by-keys