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