JavaFX上的RxTx - 清除comboBox

时间:2017-06-16 15:54:30

标签: java javafx netbeans

我为我们的制造工厂制作了QC-softare。我不是编程方面的专家,因为我主修机械工程,但我在这里戴了很多帽子,我喜欢一个很好的挑战。 无论如何,我已经阅读了很多关于RXTX和示例的教程,最后做了一个很好的工作程序。有些问题需要抛光,但总体而言是有效的。 其中一个问题出在组合框中,我列出了#34;可用端口"它找到串行COMM通信: 注意: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,COM3,COM3,COM3)。 我的doClearCbox方法显然没有做任何事情,我希望它能够解组组合框,我无法让它工作。非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

组合框(和其他控件)中的selectionModel管理当前选择的内容。所以

portList.getSelectionModel().clearSelection(index);

只是deselects the item at index

组合框的getItems()方法返回组合框中的项目列表。因此,如果您要清除所有项目,请执行

portList.getItems().clear();
相关问题