简体   繁体   中英

JavaFX TabPane disable tab switching by keys

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM