【发布时间】:2018-08-07 22:45:03
【问题描述】:
我正在使用 controlsfx ToggleSwitch 来模拟串行端口连接的开/关按钮。问题是当我尝试打开一个已经在使用的端口时。 我将其设置为 false,这会再次触发事件。所以它调用不同的 if 块。它会自行循环并重新开始。知道如何克服这个问题吗?谢谢。
portSwitch.selectedProperty().addListener(((observable, oldValue, newValue) -> {
if (newValue) {// try to connect to the port
openPort=port.open();
if (openPort) {
portSwitch.selectedProperty().set(true);//enable the switch
} else {
portSwitch.selectedProperty().set(false);//port is already in use. turn off the switch
}
} else {//disconnecting from the port
if(!port.isOpen()) //if the port is succelly closed
{
portSwitch.selectedProperty().set(false);//turn off the switch
}else{//Could not close the port.
portSwitch.selectedProperty().set(true);//So let the switch stay on
}
}
}));
【问题讨论】:
-
我要告诉你,你可能不应该手动设置
selectedProperty。 -
如果连接不成功,我想不出另一种方法来关闭它。
标签: java javafx javafx-8 controlsfx