在焦点滑块上按 Tab 时,RangeSlider 会导致 NullPointerException

RangeSlider causes NullPointerException when pressing Tab on the focused slider

我在使用 org.controlsfx.control.RangeSlider 时收到 NullPointerException。这是简单的代码示例:

package experimental_main;

import org.controlsfx.control.RangeSlider;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;


public class Experimental_start extends Application
{
    public static void main(String[] args)
    {
        launch(args);
    }


    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Button button = new Button("Click me");
        RangeSlider rangeSlider = new RangeSlider(0, 10, 2, 4);

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(rangeSlider);
        borderPane.setTop(button);

        Scene scene = new Scene(borderPane, 300, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

在RangeSlider的右拇指处于焦点状态时按Tab键或在其左拇指处于焦点状态时按Shift+Tab键时出现异常。这些按键按预期会导致 RangeSlider 失去焦点,但此操作也会导致 NullPointerException。这是异常的堆栈跟踪:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.traversal.TraversalEngine.select(Unknown Source)
at impl.org.controlsfx.skin.RangeSliderSkin.handle(RangeSliderSkin.java:150)
at impl.org.controlsfx.skin.RangeSliderSkin.handle(RangeSliderSkin.java:132)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$KeyHandler.process(Unknown Source)
at javafx.scene.Scene$KeyHandler.access00(Unknown Source)
at javafx.scene.Scene.impl_processKeyEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent3(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(Unknown Source)
at com.sun.glass.ui.View.handleKeyEvent(Unknown Source)
at com.sun.glass.ui.View.notifyKey(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(Unknown Source)
at java.lang.Thread.run(Unknown Source)

视图中按Tab(或Shift+Tab)使RangeSlider的thumb获得焦点时也没有异常。

我是做错了什么还是一个错误?是否有 RangeSlider class 实现的替代方案?

RangeSliderSkin 的这个侦听器似乎导致 NPE:

 EventHandler<KeyEvent> keyEventHandler = new EventHandler<KeyEvent>() {
            @Override public void handle(KeyEvent event) {
                if (KeyCode.TAB.equals(event.getCode())) {
                    if (lowThumb.isFocused()) {
                        if (event.isShiftDown()) {
                            lowThumb.setFocus(false);
                            new ParentTraversalEngine(rangeSlider).select(rangeSlider, Direction.PREVIOUS);
                        } else {
                            lowThumb.setFocus(false);
                            highThumb.setFocus(true);
                        }
                        event.consume();
                    } else if (highThumb.isFocused()) {
                        if(event.isShiftDown()) {
                            highThumb.setFocus(false);
                            lowThumb.setFocus(true);
                        } else {
                            highThumb.setFocus(false);
                            new ParentTraversalEngine(rangeSlider).select(rangeSlider, Direction.NEXT);
                        }
                        event.consume();
                    }
                }
            }
        };

更具体地说,new ParentTraversalEngine(rangeSlider).select(rangeSlider, Direction.NEXT);ParentTraversalEngine 在没有 Algorithm 参数的情况下初始化,因此调用 select(rangeSlider, Direction.NEXT) 会导致 NPE,因为它应该是:

Throws: java.lang.NullPointerException - if there is no algorithm

javadoc

从版本 8.40.13 开始修复了错误。