JavaFX TabPane disable tab switching by keys

99封情书 提交于 2019-12-23 15:40:09

问题


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 Nodes.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!