1

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.

Dilum
  • 11
  • 3
  • Post a [mcve] and try to explain the conditions a bit better. What does "*Other items are filtered into checked combo box*" mean, for example? – user1803551 May 22 '17 at 08:39
  • first item is hard code one. so "Other items" means, 2nd, 3rd ...items which filled according to user input. Those items are get through the observableList – Dilum May 22 '17 at 08:50
  • Provide a [mcve]. Question is unclear. – user1803551 May 22 '17 at 08:52
  • I need to satisfy only above 5 requirement for check combo box. My question is first time when I click check combo box, it get too much time to check. Graph is also update according to check item. but I didn't mention that code here because it is no need. Because when check combo box is check, graph is update soon. When you reproduce this issue, I only want to work check combo box according to previously asked requirement. I think this is Minimal, Complete, and Verifiable example – Dilum May 22 '17 at 09:21
  • 1
    Read the link, it is not a MCVE because we can't copy-paste it to our IDEs and run it. The part you posted is not enough for that. Maybe someone else can read your mind better. – user1803551 May 22 '17 at 09:28

0 Answers0