Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Creating SWT tree leaves

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Creating SWT tree leaves

    Hi. Old to IT, new to Java. Having trouble adding leaves to my tree. Developing a stand alone Java app using mainly SWT and Eclipse JavaEE on Windows.

    The tree is like this:

    root
    ------parentA
    --------------childA1
    --------------childA2
    --------------childA3
    ------parentB
    ------parentC
    --------------childC1
    --------------childC2

    My requirement: when a child is selected/clicked I want to create a grandchild (there could be from zero to say 12 grandchildren). Thus if user clicks/selects childA2:

    root
    ------parentA
    --------------childA1
    --------------childA2
    ----------------------grandchildA21
    --------------childA3
    ------parentB
    ------parentC
    --------------childC1
    --------------childC2

    Codewise I have:

    import javax.swing.tree.DefaultMutableTreeNode;

    This I've tried but presumably there is a SWT approach that avoids swing?

    import org.eclipse.swt.widgets.Tree;
    import org.eclipse.swt.widgets.TreeItem;
    .
    .
    final Tree tree = new Tree(shell, SWT.VIRTUAL | SWT.BORDER | SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE );
            tree.setHeaderVisible(true);
            tree.setBounds(975, 00, 375, 600);
    .
    .
     void populate_tree() {
                	for(int x=1;x<number_of_lines;x++) {
                		if ( script_type[x].matches("S")) {
                                    final TreeItem itemI = new TreeItem(tree, SWT.NULL);
                			itemI.setText(script_type[x] + "=" + x + "=" + script_item[x]);
                			x++;
                			while (! script_type[x].equals("S")) {
                				if ( script_type[x].equals("AB1")||script_type[x].equals("DB1")) {
                					TreeItem itemJ = new TreeItem(itemI, SWT.NULL);
                					itemJ.setText(script_type[x] + "=" + x + "=" + script_itemnumber[x] + "=" + script_item[x]); 
                					x++;
                					TreeItem itemK = new TreeItem(itemI, SWT.NULL);
                					itemK.setText(script_type[x] + "=" + x + "=" + script_itemnumber[x] + "=" + script_item[x]);
                				}  
                				x++;
                				if(x>number_of_lines) break;
                                    }
                			x--;     
                		} 
                	}
                }
     
            }
    .
    .
    .
                    tree.addListener(SWT.Selection, new Listener() {
            	public void handleEvent(Event event) {
    .
    .

    And it is here I've tried lots of options but while I do not get errors I do not get leaves. What seems odd is that I can see the selected/clicked child text but if I look at the index it is always 1. And I thought the index would let me create the leaf.

        TreeItem item = (TreeItem)event.item;
    	            System.out.println("index=" + item.indexOf(item));

    If anyone can give me any guidance or link to a good example that actually works while I still have hair it would be greatly appreciated.

    Cheers
    Last edited by nigele2; August 25th, 2014 at 04:24 AM. Reason: tipo


Similar Threads

  1. Draw tree, clouds, leaves with recursion
    By BiggerBore in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 16th, 2013, 03:39 PM
  2. Moving sprite leaves a trail
    By chris2307 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 26th, 2011, 09:01 AM
  3. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  4. Help creating expression tree
    By nellaf in forum Java SE APIs
    Replies: 1
    Last Post: December 3rd, 2009, 08:54 AM
  5. Creating an Expression Tree
    By vluong in forum Collections and Generics
    Replies: 0
    Last Post: November 28th, 2009, 11:41 PM

Tags for this Thread