How to select and unselect all checkboxes programmatically?

Hi,
I have check box tree which I create using the code in here:

http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet274.java

I used this code and created the check box tree as I wanted in one of the tab in my GUI.

The thing that I try to do is to be able to select and deselect all items in the tree by clicking a button in the GUI.

I have everything (the listener and button that triggers the event) ready to do this except the code that will select/unselect all items in the tree.

How can I expand this code so that it will have two more methods to select and unselect all the checks?

By the way, this is the code that I have finally have:

import org.eclipse.swt.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;

public class CheckListTree {
    
    public void checkPath(TreeItem item, boolean checked, boolean grayed) {
        if (item == null) return;
        if (grayed) {
            checked = true;
        } else {
            int index = 0;
            TreeItem[] items = item.getItems();
            while (index < items.length) {
                TreeItem child = items[index];
                if (child.getGrayed() || checked != child.getChecked()) {
                    checked = grayed = true;
                    break;
                }
                index++;
            }
        }
        item.setChecked(checked);
        item.setGrayed(grayed);
        checkPath(item.getParentItem(), checked, grayed);
    }

    public void checkItems(TreeItem item, boolean checked) {
        item.setGrayed(false);
        item.setChecked(checked);
        TreeItem[] items = item.getItems();
        for (int i = 0; i < items.length; i++) {
            checkItems(items[i], checked);
        }
    }

public void createCheckListTree(Composite compTab2, GridData layoutData) {
        Tree tree = new Tree(compTab2, SWT.BORDER | SWT.CHECK);
        tree.setLayoutData(layoutData);
        tree.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (event.detail == SWT.CHECK) {
                    TreeItem item = (TreeItem) event.item;
                    boolean checked = item.getChecked();
                    checkItems(item, checked);
                    checkPath(item.getParentItem(), checked, false);
                }
            }
        });
        
        for (int i = 0; i < 4; i++) {
            TreeItem itemI = new TreeItem(tree, SWT.NONE);
            itemI.setText("Check " + i);
            for (int j = 0; j < 4; j++) {
                TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
                itemJ.setText("Check " + i + " " + j);
                for (int k = 0; k < 4; k++) {
                    TreeItem itemK = new TreeItem(itemJ, SWT.NONE);
                    itemK.setText("Item " + i + " " + j + " " + k);
                }
            }
        }
}  
}

Open in new window

TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

piattndCommented:
I'm not very familiar with Java, but the way you'd do it from a logical standpoint is pass in the treeview (tree of check boxes) and go through each iteration of "TreeItem" and set the value of checked to true.

In your current chode "checkItems", it looks like you're passing to it an individual "TreeItem", not the entire tree.  You need to pass in the tree, then loop through "foreach treeItem in Tree".

Sorry I couldn't be more specific, I hope that helps!
0
StillUnAwareCommented:
I used the same initial SWT snippet to show the functionality:

/*
 * Tree snippet: implement standard tree check box behavior (SWT.CHECK)
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.3
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class Snippet274 {
	
	static void checkPath(TreeItem item, boolean checked, boolean grayed) {
	    if (item == null) return;
	    if (grayed) {
	        checked = true;
	    } else {
	        int index = 0;
	        TreeItem[] items = item.getItems();
	        while (index < items.length) {
	            TreeItem child = items[index];
	            if (child.getGrayed() || checked != child.getChecked()) {
	                checked = grayed = true;
	                break;
	            }
	            index++;
	        }
	    }
	    item.setChecked(checked);
	    item.setGrayed(grayed);
	    checkPath(item.getParentItem(), checked, grayed);
	}

	static void checkItems(TreeItem item, boolean checked) {
	    item.setGrayed(false);
	    item.setChecked(checked);
	    TreeItem[] items = item.getItems();
	    for (int i = 0; i < items.length; i++) {
	        checkItems(items[i], checked);
	    }
	}

	public static void main(String[] args) {
	    Display display = new Display();
	    Shell shell = new Shell(display);
	    shell.setLayout(new GridLayout(2, false));
	    final Tree tree = new Tree(shell, SWT.BORDER | SWT.CHECK);
	    tree.addListener(SWT.Selection, new Listener() {
	        public void handleEvent(Event event) {
	            if (event.detail == SWT.CHECK) {
	                TreeItem item = (TreeItem) event.item;
	                boolean checked = item.getChecked();
	                checkItems(item, checked);
	                checkPath(item.getParentItem(), checked, false);
	            }
	        }
	    });
	    for (int i = 0; i < 4; i++) {
	        TreeItem itemI = new TreeItem(tree, SWT.NONE);
	        itemI.setText("Item " + i);
	        for (int j = 0; j < 4; j++) {
	            TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
	            itemJ.setText("Item " + i + " " + j);
	            for (int k = 0; k < 4; k++) {
	                TreeItem itemK = new TreeItem(itemJ, SWT.NONE);
	                itemK.setText("Item " + i + " " + j + " " + k);
	            }
	        }
	    }
		Rectangle clientArea = shell.getClientArea();
	    tree.setBounds(clientArea.x, clientArea.y, 300, 300);
	    tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
	    Composite composite = new Composite(shell, SWT.NONE);
	    composite.setLayout(new GridLayout(1, false));
	    Button button1 = new Button(composite, SWT.NONE);
	    button1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
	    button1.setText("Check All");
	    button1.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent e) {
	    		for (TreeItem item : tree.getItems()) {
					checkItems(item, true);
				}
	    	}
		});
	    Button button2 = new Button(composite, SWT.NONE);
	    button2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
	    button2.setText("Uncheck All");
	    button2.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent e) {
	    		for (TreeItem item : tree.getItems()) {
					checkItems(item, false);
				}
	    	}
		});
	    
	    //shell.pack();
	    shell.open();
	    while (!shell.isDisposed()) {
	        if (!display.readAndDispatch()) display.sleep();
	    }
	    display.dispose();
	}

}

Open in new window


To check/uncheck all items within the tree you must work with tree root items: traverse through all of them and check/uncheck every branch. That's what the button listeners do
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TolgarAuthor Commented:
Can you please take a look at this related question?

http://www.experts-exchange.com/Programming/Languages/Java/Q_28225322.html

Then I will close both of them.

Thanks,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.