我正在为我们的生产工厂做一个质量控制软件。我不是编程专家,因为我的专业是机械工程,但我在这里得到了许多帽子,我喜欢一个很好的挑战。无论如何,我已经阅读了很多关于RXTX的教程和示例,最后我做了一个很好的工作程序。有一些问题需要改进,但总体上它是有效的。其中一个问题是在组合框中,我列出了它为串行通信找到的“可用端口”:注意: main.ports是一个枚举
// SCAN METHOD
public void doScan(ActionEvent event) {
System.out.println("You clicked Scan");
doClearCBox();
main.ports = CommPortIdentifier.getPortIdentifiers();
//CLEAR COMBO BOX EVERY TIME YOU SCAN
while (main.ports.hasMoreElements())
{
CommPortIdentifier curPort = (CommPortIdentifier)main.ports.nextElement();
//get only serial ports
if (curPort.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
main.portMap.put(curPort.getName(), curPort);
portList.getItems().add(curPort.getName());
}
}
}
public void doClearCBox()
{
System.out.println("Clearing combo box and Enumeration");
main.ports = null;
//JUST CLEAR RANDOM VALUES OR SOMETHING?
portList.getSelectionModel().clearSelection(0);
portList.getSelectionModel().clearSelection(1);
portList.getSelectionModel().clearSelection(2);
portList.getSelectionModel().clearSelection();
}我遇到的问题是,如果你按下“扫描”按钮超过一次,它基本上重复所有的事情(例如,你会看到一个列表,上面写着COM3,COM3),如果你点击它5次,你会看到(COM3,COM3)。我的doClearCbox方法显然没有做任何事情,我想让它去填充combobox,但是我不能让它工作。非常感谢您的任何帮助。
发布于 2017-06-17 01:14:15
组合框(和其他控件)中的selectionModel管理当前选定的内容。所以
portList.getSelectionModel().clearSelection(index);只有deselects the item at index。
组合框的getItems()方法返回组合框中的项目列表。因此,如果您想清除所有项,您可以这样做
portList.getItems().clear();https://stackoverflow.com/questions/44593391
复制相似问题