troubleshooting Question

How to save select checkbox items to an XML file in Java?

Avatar of Tolgar
Tolgar asked on
Java
8 Comments1 Solution682 ViewsLast Modified:
Hi,
I have java code and this code creates GUI. In this GUI. I have check boxes in a Tree.

The thing that I would like to do is to save the selected check box items to an XML file.

This is the class and the methods that I use to create the tree with check boxes:

public class CheckListTree {
    private Tree tree;
    private static CheckListTree instance = null;
   
    
    private CheckListTree() {
    }
    
    public static CheckListTree getInstance()
    {
        if (instance == null) {
            instance = new CheckListTree();
        }
        //System.out.println("instance: " + instance);
        return instance;
    }
    
    
    public void unselectAll (){
        System.out.println("tree: " + tree);
        for (TreeItem item : tree.getItems()) {   
            //System.out.println("item: " + item);
            checkItems(item, false);
        }
        
    }
    
    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, List<HashMap<Integer, String>> checkList)  {
            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);
                    }
                }
            });
                  
            
            TreeItem item_Standards = new TreeItem(tree, SWT.NONE);
            item_Standards.setText("My List");
            
            Iterator<HashMap<Integer, String>> checkListIter = checkList.iterator();
            while(checkListIter .hasNext()) {
              HashMap<Integer, String> amap =   (HashMap<Integer, String>) checkListIter .next();
              Set<?> keysetString = (Set<?>) amap .keySet();
              Iterator<?> iter = keysetString.iterator();
              while( iter.hasNext() ) {
                Integer keyVal = (Integer) iter.next();
                //System.out.println("keyVal " + keyVal);
                String valFromKey = amap.get(keyVal);
                //System.out.println("valFromKey " + valFromKey);
                TreeItem item_Substandards = new TreeItem(item_Standards, SWT.NONE);
                item_Substandards.setText(valFromKey);
              } 
            }
    } 
    
    public void saveCheckedList2XML(){
    //HERE I WANT TO SAVE THE SELECTED ITEMS TO A PLACE of USER'S choice   
        
    }

This is the listener I use for this button:

        }else if(toolItem == Gui.item222){
            CheckListTree.getInstance().saveCheckedList2XML();

Can you please help me how I can do it in my code above?

Thanks
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros