CheckboxTreeViewer on checked Children node get Parent node value

I am using JFace CheckboxTreeViewer and adding on ICheckStateListener to get checked elemets,

My CheckboxTreeViewer structure is as follows,


My Requirement is that when I am checked a Children node to get its Related parent node

for example

when I checked Child8 then get Parent Node p2 when I checked Child2 then get Parent Node p1

how to achieve this?

You get the element that has changed from the CheckStateChangedEvent passed to the listener by calling the getElement method:

public void checkStateChanged(CheckStateChangedEvent event) {

  Object changed = event.getElement();

This is the object that your tree content provider provided. So you can get its parent by asking the content provider:

  ITreeContentProvider provider = (ITreeContentProvider)viewer.getContentProvider();

  Object parent = provider.getParent(changed);

where viewer is the CheckboxTreeViewer.