gpt4 book ai didi

java - NullPointerException 尝试从 JnativeHook 鼠标事件设置 javafx TextField 文本时出错

转载 作者:太空宇宙 更新时间:2023-11-04 13:47:34 25 4
gpt4 key购买 nike

我试图在 JnativeHook MoseClicked 事件发生时设置 javafx TextField 文本。但是我遇到“NullPointerException”错误。我将我的 Controller 类代码放在这里:

public class FXMLDocumentController implements Initializable, NativeMouseListener {

@FXML
private TextField txt_Search;

@Override
public void initialize(URL url, ResourceBundle rb) {

txt_Search.setText("dvdf"); //this is work and no problem is in here
Listener();
}
public void Listener() {
// Clear previous logging configurations.
LogManager.getLogManager().reset();
// Get the logger for "org.jnativehook" and set the level to off.
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());

System.exit(1);
}
// Construct the example object.
FXMLDocumentController example = new FXMLDocumentController();
// Add the appropriate listeners.
GlobalScreen.addNativeMouseListener(example);
}

@Override
public void nativeMouseClicked(NativeMouseEvent nme) {
if (nme.getClickCount() == 2) {
System.out.println("Double Click Event");
txt_Search.setText("Mouse clicked");
}

}

@Override
public void nativeMousePressed(NativeMouseEvent nme) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void nativeMouseReleased(NativeMouseEvent nme) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

并且发生此错误:

Double Click Event
Exception in thread "JNativeHook Dispatch Thread" java.lang.NullPointerException
at FXMLDocumentController.nativeMouseClicked(FXMLDocumentController.java:60)
at org.jnativehook.GlobalScreen$EventDispatchTask.processButtonEvent(Unknown Source)
at org.jnativehook.GlobalScreen$EventDispatchTask.processButtonEvent(Unknown Source)
at org.jnativehook.GlobalScreen$EventDispatchTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

更新:

I update my code and annotate txt_Search with @FXML annotation

最佳答案

@Kingtak,您从未初始化过“txt_Search”变量。您可以在此处使用 @FXML 注释,并在 fxml 文件中将 id 分配给那里的文本字段。

关于java - NullPointerException 尝试从 JnativeHook 鼠标事件设置 javafx TextField 文本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30676799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com