简体   繁体   English

Java:如何从套接字readline()生成事件?

[英]Java: How to generate an event from a socket readline()?

I have messages (lines) coming in from a tcp socket, (the messages originating from a CAN gateway). 我有来自tcp套接字的消息(行)(消息来自CAN网关)。 On selected messages, after selection and parsing, I want to update a swing GUI form (eg generated in Netbeans). 在选定的消息上,选择和解析后,我想更新一个摆动GUI表单(例如,在Netbeans中生成)。

I read that the swing update must be driven from the event handler, and there are many examples, but they are based on button pushes, slider changes, etc., and not incoming socket data, (or serial ports). 我读到摆动更新必须由事件处理程序驱动,并且有很多示例,但是它们基于按钮的按下,滑块的更改等,而不是基于传入的套接字数据(或串行端口)。 It appears that I need to make the handling of a 'readline' of a socket generate an event, however I don't find examples and given that it has to be a common problem and maybe I'm on the wrong track and there might be simpler alternatives. 看来我需要对套接字的“ readline”进行处理以生成事件,但是我没有找到示例,并且鉴于它必须是一个常见问题,也许我走错了路,并且可能是更简单的选择。

Use SwingUtilities.invokeLater() to run arbitrary code on the swing event thread. 使用SwingUtilities.invokeLater()在swing事件线程上运行任意代码。 for example: 例如:

    String stuff = socket.readLine();
    if (myStuff(stuff)) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // do any updates to swing ui here
            }
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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