1

我正在尝试在 JnativeHook MoseClicked 事件发生时设置 javafx TextField 文本。但我遇到“NullPointerException”错误。我将控制器类代码放在这里:

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

}

并且正在发生此错误:

在 org.jnativehook.GlobalScreen$EventDispatchTask 的 org.jnativehook.GlobalScreen$EventDispatchTask.processButtonEvent(Unknown Source)的 FXMLDocumentController.nativeMouseClicked(FXMLDocumentController.java:60)
的线程“JNativeHook Dispatch Thread”java.lang.NullPointerException中双击事件异常 。 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) 在 java.lang.Thread.run(Thread.java:745)






更新:

我更新了我的代码并使用 @FXML 注释来注释 txt_Search

4

2 回答 2

0

@Kingtak 你从来没有初始化你的 'txt_Search' 变量。你可以在这里使用@FXML 注释,并在 fxml 文件中将 id 分配给那里的文本字段。

于 2015-06-06T06:45:21.190 回答
0

您应该返回并查看使用 wiki 的使用swing部分。这个库产生的事件默认不会在 Swing 事件派发线程上操作!GlobalScreen.setEventDispatcher(new SwingDispatchService());您必须在注册钩子之前包装对摆动组件的访问或使用。有关更多信息,请阅读有关事件调度​​线程Swing 线程安全的信息。

于 2015-06-07T01:01:47.440 回答