TextArea不更新其他设备上的数据

时间:2019-03-02 20:26:20

标签: java events javafx listener fxml

我正在使用FXML文件开发我的第一个JavaFX应用程序。 我的程序包含以下几个元素:

  • addDeviceButton(addMyoButton)
  • 很少有TextArea,它将具有从设备收集的打印数据。

工作简短说明:

  1. 按下addDecideButton应该开始从中收集数据 DataController(DeviceListener),
  2. 收集的数据和系统 通信应打印到TextAreas。

我有两个问题:

  1. 无法在TextArea中打印,
  2. 应用程序冻结(按addMyoButton后无响应)

在1)我不知道怎么了。 2)我认为我不应该将运行dataCollector放在负责按钮事件的方法中。但是,我不确定如何正确地做到这一点。

您有什么建议,想法如何写这些片段吗? 是FXML或方法出现问题吗?

负责这部分的FXML文件如下:

<TextArea fx:id="communicateTextField" 
prefHeight="300.0" prefWidth="300.0" >

<Button fx:id="addMyoButton"
onAction="#addMyo" >

方法addMyo(),在按下按钮后运行。

public void addMyo(ActionEvent event) {
    try {
        Hub hub = new Hub("com.example.hello-myo");

        communicateTextField.setText("Attempting to find a Myo...");
        Myo myo = hub.waitForMyo(10000);

        if (myo == null) {
            throw new RuntimeException("Unable to find a Myo!");
            communicateTextField.setText(""Unable to find a Myo!");
        }

        communicateTextField.setText("Connected to a Myo armband!");
        DeviceListener dataCollector = new DataCollector();
        hub.addListener(dataCollector);

            while (true) {
            hub.run(1000 / 20);
            communicateTextField.setText(dataCollector);
        }
    } catch (Exception e) {
        System.err.println("Error: ");
        e.printStackTrace();
        System.exit(1);
    }
}

Method for printing communicates to TextArea
public void printCommunicate(String communicate) {
    communicateTextField.setText(communicate);
}

0 个答案:

没有答案