JavaFX TabPane禁用按键切换选项卡 [英] JavaFX TabPane disable tab switching by keys
问题描述
我有一个Tab
,其中包含一些内容:ScrollBar
和其他内容.
I have a Tab
with some content: ScrollBar
and other.
ScrollBar
具有用于键的事件处理程序: left 和 right .
The ScrollBar
has event handler for keys: left and right.
但是如果我按这些按钮,则会切换Tabs
,因为TabPane
也有一个键处理程序.
But if I press these buttons, Tabs
are switched, because TabPane
also has a key handler.
如何为TabPane
禁用默认事件处理程序或更改切换策略?
How to disable default event handler for TabPane
or change switching policy?
推荐答案
The problem is that a ScrollBar
is not focus traversable by default and key events are only fired for focused Node
s.
您可以设置ScrollBar
焦点可遍历:
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);
});
在这种情况下,只要将ScrollBar
放在焦点上,就会处理关键事件.
In this case whenever the ScrollBar
is focused, the key events are handled.
这篇关于JavaFX TabPane禁用按键切换选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!