简体   繁体   中英

RxTx on JavaFX - Clearing comboBox

I'm making a QC-softare for our manufacturing facilities. I am not a specialist in programming as I majored in Mechanical Engineering but I get go wear a lot of hats here and I love a good challenge. Anyways, I've been reading a lot of tutorials on RXTX and examples and finally made a good working program. There's some issues that need polishing but overall it works. One of these issues is on the combobox where I list the "available ports" that it finds for serial COMM communication: Note: main.ports is an Enumeration

// 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();
    }

The problem I encounter is that if you press the "Scan" Button more than once it basically repeats everything (so for example you will see a list that says COM3, COM3) and if you click it 5 times you'll see (COM3, COM3, COM3, COM3, COM3). My doClearCbox method does nothing apparently, I want it to de-populate the combobox and I can't get it to work. Any help is greatly appreciated

The selectionModel in a combo box (and other controls) manages what is currently selected. So

portList.getSelectionModel().clearSelection(index);

just deselects the item at index .

The combo box's getItems() method returns the list of items in the combo box. So if you want to clear all the items, you do

portList.getItems().clear();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM