0

I am using ControlsFX for CheckTreeView. I have lots of elements in CheckTreeView and i dont want to traverse through all the elements in this tree ( because it takes lots of time due to number of elements in the tree). Is there a method like checkTreeView.getLastUncheckedItem(); to get the last unchecked one.

Currently I am checking the number of elements that checked and comparing it with counter.

    If (CountPrev > Count){
//Something Unchecked Do Stuff
}

But again, i cant find what is unchecked without traverse through all elements.

EDIT:

When user checks an item on CheckTreeView, I get that item by

String lastCheckedItem = checkTreeView.getCheckModel().
getCheckedItems().get(treeView.getCheckModel().getCheckedItems().size()-1).toString();

Now I need something like this for the unchecked item

  • 1
    You can implement it yourself as an action when unchecked where you just save the index of the unchecked element in a local variable or in stack if you want to keep all recent unchecked elements – Mohamed Benmahdjoub Jul 28 '17 at 07:24
  • 1
    How can i do that? Can you explain more please or send a link of an example? –  Jul 28 '17 at 07:28
  • 1
    does it uncheck (or check) when you click on the item ? or you need to click on the checkBox ? – Mohamed Benmahdjoub Jul 28 '17 at 08:04
  • 1
    It does check/uncheck when i click on them. I'm using onChanged method. Checkboxes work well, the problem I am having is I cant find the unchecked item. –  Jul 28 '17 at 08:08

3 Answers3

1

Take a ArrayList 'allItem' and Store all TreeItems, then after Store selected item in ObservableList 'Selecteditems' using getCheckedItems() method, Now remove selected item in ArrayList like below code:

Here allTreeItems is a CheckBoxTreeItem

List<String> allItem = new ArrayList<>();
for (int j = 0; j < allTreeItems.getValue().length(); j++) {
         allItem.add(allTreeItems.getChildren().get(j).getValue());
    }

if (CountPrev > Count) {
        ObservableList<TreeItem<String>> Selecteditems = checkTreeView.getCheckModel().getCheckedItems();
        allItem.remove(Selecteditems);

        System.out.println("UnChecked Item :" + allItem);

        for (int k = 0; k < allItem.size(); k++) {
            System.out.println(allItem.get(k));
        }
}
Keyur Bhanderi
  • 1,524
  • 1
  • 11
  • 17
1

Guys thank you so much for your help! I've accepted Calips' answer because of time and effort he gave for my question. This is what I've been looking for:

   checkTreeView.getCheckModel().getCheckedItems().addListener(new ListChangeListener<TreeItem<String>>() {
        @Override public void onChanged(ListChangeListener.Change<? extends TreeItem<String>> change) {
            updateText(checkedItemsLabel, change.getList());

            while (change.next()) {
                System.out.println("============================================");
                System.out.println("Change: " + change);
                System.out.println("Added sublist " + change.getAddedSubList());
                System.out.println("Removed sublist " + change.getRemoved());
                System.out.println("List " + change.getList());
                System.out.println("Added " + change.wasAdded() + " Permutated " + change.wasPermutated() + " Removed " + change.wasRemoved() + " Replaced "
                        + change.wasReplaced() + " Updated " + change.wasUpdated());
                System.out.println("============================================");
            }
        }
    });

Resource:

https://github.com/jinghai/controlsfx/blob/master/controlsfx-samples/src/main/java/org/controlsfx/samples/checked/HelloCheckTreeView.java

0
Stack<YourClassOfTheCheckModel> recentlyUnchcked = new Stack<YourClassOfTheCheckModel>();
yourTreeView.getSelectionModel().
selectedItemProperty().addListener( new ChangeListener() {

        @Override
        public void changed(ObservableValue observable, Object oldValue,
                Object newValue) {

            TreeItem<YourClassOfTheCheckModel> selectedItem = 
               (TreeItem<YourClassOfTheCheckModel>) newValue;
           CheckModel checkModel = checkTreeView.getCheckModel().
           bool checked = checkModel.isChecked (selectedItem);
           if(checked==false){
                recentlyUnchcked.push(yourObjectOfTheCheckModel);
           }
        }

      }); 

Hope this will help or give you an idea though i don't know if this will work (code not tested, have no IDE right now).

Mohamed Benmahdjoub
  • 1,270
  • 4
  • 19
  • 38