首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RxTx on JavaFX - Clearing comboBox

RxTx on JavaFX - Clearing comboBox
EN

Stack Overflow用户
提问于 2017-06-16 23:54:30
回答 1查看 108关注 0票数 0

我正在为我们的生产工厂做一个质量控制软件。我不是编程专家,因为我的专业是机械工程,但我在这里得到了许多帽子,我喜欢一个很好的挑战。无论如何,我已经阅读了很多关于RXTX的教程和示例,最后我做了一个很好的工作程序。有一些问题需要改进,但总体上它是有效的。其中一个问题是在组合框中,我列出了它为串行通信找到的“可用端口”:注意: main.ports是一个枚举

代码语言:javascript
复制
// 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,但是我不能让它工作。非常感谢您的任何帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-17 01:14:15

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

代码语言:javascript
复制
portList.getSelectionModel().clearSelection(index);

只有deselects the item at index

组合框的getItems()方法返回组合框中的项目列表。因此,如果您想清除所有项,您可以这样做

代码语言:javascript
复制
portList.getItems().clear();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44593391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档