我目前有以下情况:
我想让我的应用程序听取用户输入。首先,我用一个看不见的TextField做了这个,但这搞砸了我的设计,我认为它不够复杂。所以,我现在正试图在我的上部FlowPane上创建一个onKeyPressed EventHandler,如下所示:
public String input = "";
@FXML public void listenToInput(KeyEvent key) {
if (key.getCode() != KeyCode.ENTER) {
if (key.getCode().isDigitKey() || key.getCode().isLetterKey()) {
String newChar = key.getText();
input = input.concat(newChar);
}
}
else {
this.addScanToList(input);
input = "";
}
}
这适用于纯文本和数字。但是当我按 ENTER 时,我收到以下错误:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.control.behavior.TableViewBehaviorBase.activate(TableViewBehaviorBase.java:915)
at com.sun.javafx.scene.control.behavior.TableViewBehaviorBase.callAction(TableViewBehaviorBase.java:179)
at com.sun.javafx.scene.control.behavior.BehaviorBase.callActionForEvent(BehaviorBase.java:218)
at com.sun.javafx.scene.control.behavior.TableViewBehaviorBase.callActionForEvent(TableViewBehaviorBase.java:208)
at com.sun.javafx.scene.control.behavior.BehaviorBase.lambda$new$75(BehaviorBase.java:135)
at com.sun.javafx.scene.control.behavior.BehaviorBase$$Lambda$146/1103773754.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$KeyHandler.process(Scene.java:3926)
at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3872)
at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2001)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2463)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:197)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:147)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$346(GlassViewEventHandler.java:228)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$208/996795822.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:406)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:227)
at com.sun.glass.ui.View.handleKeyEvent(View.java:540)
at com.sun.glass.ui.View.notifyKey(View.java:950)
我已经想到的地方:
编辑1:
有谁知道触发此错误的原因是什么?
编辑2:
我将onKeyPressed更改为onKeyReleased,然后我的代码正常运行,但我仍然收到错误。也许知道这一点很有帮助。
编辑3:
我尝试使用 ALT 而不是 ENTER 将扫描添加到列表中,这非常有效,因此它与 ENTER <有关/ KBD>
答案 0 :(得分:1)
经过几天的努力,我能够解决这个问题。正如我在编辑3中提到的那样,它与 ENTER 键有关。当它被按下时,看起来FXML文件正在搜索可能消耗动作的元素。不知何故,它发现的第一件事是TableView。我尝试使用此代码使其不可聚焦:
tableview.setFocusTraversable(false)
它有效。谁能解释一下JavaFX想用 ENTER 键做什么?我把注意力集中在FlowPane上,所以它应该将动作发送给适当的监听器。如果它与必须覆盖的事件处理程序有关,请告诉我。
答案 1 :(得分:0)
这就是我通常添加keyEvent Handler来监听关键事件的方法。
FlowPane pane = new FlowPane();
pane.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent key) {
if (key.getCode() != KeyCode.ENTER) {
if (key.getCode().isDigitKey() || key.getCode().isLetterKey()) {
String newChar = key.getText();
input = input.concat(newChar);
}
}
else {
this.addScanToList(input);
input = "";
}
}
});
}