lundi 22 mai 2017

JavaFX checkComboBox listeners are working slow only first time when click new item

In my application I want to add checked combo box. There first item is hard code. Other items are filtered into checked combo box using for-loop by getting items in observable list. There are two listeners for hard code item and other items which filled according to observable list values. The most important requirements are,

  • When we click hard code item again and again it should NOT be unchecked and graph is updated according to it
  • When we click other items, hard code item should be unchecked and graph update related items values.
  • We can check multiple items form other observable item list and when we check multiple items, graph should update according to it.
  • When no item is checked in observable list values, automatically checked hard-code item and update graph.
  • When we checked hard code item, while checked other item/items, all other items should clear and check only hard-code item.

part of the code is shown in below

    Machine hardCordMachine = new Machine(0,"All Machines");
    machinesChkBox.getItems().clear();
    machinesChkBox.getItems().addAll(machineList);
    machinesChkBox.getItems().add(0, hardCordMachine);
    machinesChkBox.getCheckModel().check(0);

    machinesChkBox.setConverter(new StringConverter<Machine>() {
        @Override
        public String toString(Machine object) {
            if(object != null)
                return object.getName();
            else
                return "All Machines";
        }

        @Override
        public Machine fromString(String string) {
            return null;
        }
    });

    machinesChkBox.getItemBooleanProperty(0).addListener(allMachineListener);

    for (Machine machine : observableMachineList) {
        machinesChkBox.getItemBooleanProperty(machine).addListener(machineListener);
    }

    ChangeListener<Boolean> machineListener = (observable, oldValue, newValue) -> {
        if(newValue){
            if(machinesChkBox.getCheckModel().isChecked(0)){
                machinesChkBox.getCheckModel().clearCheck(0);
            }
            updateByMachines(machinesChkBox.getCheckModel().getCheckedItems());
        }else {
            if (!machinesChkBox.getCheckModel().isChecked(0) && machinesChkBox.getCheckModel().getCheckedItems().size() > 0) {
                updateByMachines(machinesChkBox.getCheckModel().getCheckedItems());
            }else if(machinesChkBox.getCheckModel().isChecked(0) && machinesChkBox.getCheckModel().getCheckedItems().size() > 1){
                machinesChkBox.getCheckModel().clearChecks();
            }else if (machinesChkBox.getCheckModel().getCheckedItems().size() == 0) { 
                machinesChkBox.getCheckModel().check(0);
                LogsByMachine = null; // clear other machine's graph data
                showHistory(); // update data according to hard code item
            }
        }
    };

    ChangeListener<Boolean> allMachineListener = new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            if ((machinesChkBox.getCheckModel().getCheckedItems().size() == 1 && machinesChkBox.getCheckModel().getCheckedItems().get(0).getMachineId() == 0)) {
                machinesChkBox.getItems().clear();
                machinesChkBox.getItems().addAll(machineList);
                machinesChkBox.getItems().add(0, tempMachine);
                machinesChkBox.getCheckModel().check(0);
                for (Machine machine : machineList) {
                    machinesChkBox.getItemBooleanProperty(machine).addListener(machineListener);
                }
                machinesChkBox.getItemBooleanProperty(0).addListener(this);
            } else {
                if (newValue) {
                    if (machinesChkBox.getCheckModel().getCheckedItems().size() > 1) {
                        Iterator<Machine> machineIterator = machinesChkBox.getCheckModel().getCheckedItems().iterator();
                        while (machineIterator.hasNext()) {
                            Machine machine = machineIterator.next();
                            if (machine.getMachineId() != 0) {
                                machinesChkBox.getCheckModel().clearCheck(machine);
                            }
                        }
                    }
                } else if (!newValue && machinesChkBox.getCheckModel().getCheckedItems().size() == 0) {
                    machinesChkBox.getCheckModel().check(0);
                }
            }
        }
    };

Using above code I can fulfill the requirement need. But the problem is when run application and tick one of the observable list items, it get too much time to check the item and update graph. This happen only first time when check on item. when we click another item in second time, it work on proper time. So I need to find another way to write the above program with fulfilling above requirements.




Aucun commentaire:

Enregistrer un commentaire