Tree leaves [message #1412880] |
Wed, 27 August 2014 09:25 |
|
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 added by user action at different times).
Thus if user clicks/selects childA2:
root
------parentA
--------------childA1
--------------childA2
----------------------grandchildA21
--------------childA3
------parentB
------parentC
--------------childC1
--------------childC2
Codewise I have:
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
Question one: do I need any further tree related imports to get this to work?
.
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 displays, is expandable, has all the right text.
.
tree.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
.
And it is here I've tried lots of options but it's like bleak mid winter - no new leaves What seems odd is that I can see the selected/clicked child text item.getText() but if I look at the index it is always -1. And I thought the index would let me create the leaf.
Question 2: 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
Performance/Load/Stress tester for large public and private sector orgs because I enjoy it.
|
|
|
|
Re: Tree leaves [message #1443179 is a reply to message #1412979] |
Sun, 12 October 2014 08:59 |
|
Been playing sometime with this and maybe I was expecting too much sophistication from a SWT tree (and SWT table for that matter).
To get it to work I have found that walking the tree is the best if not only approach.
However I still have one issue that someone who uses SWT trees might help with. The following code adds leaves at a certain node in the tree.
// Create directive leaves
for(int z=0; z<dirbits.length; z++) {
final TreeItem itemDir = new TreeItem( itemDBAB1, SWT.NULL);
itemDir.setText(dirbits[z]);
itemDir.setForeground(green);
}
As presented it appends values to the tree leaf but what I want is distinct tree leaves. Because I'm looping I'm using the same treeitem so thus the append and not create. If anyone could suggest if my code is simply wrong or could be easily changed before I go for a more convoluted solution I'd very much appreciate it.
Many thanks
Performance/Load/Stress tester for large public and private sector orgs because I enjoy it.
|
|
|
Re: Tree leaves [message #1444455 is a reply to message #1443179] |
Tue, 14 October 2014 05:17 |
|
Apologies to all who read this but it turned out to be a symptom of another problem related to incompatibilities between Swing and SWT.
I don't have the details as I had to get someone to fix this for me.
Mods do you want to delete this hread as I doubt it helps anyone?
Again sorry
Performance/Load/Stress tester for large public and private sector orgs because I enjoy it.
|
|
|
Re: Tree leaves [message #1456118 is a reply to message #1444455] |
Thu, 30 October 2014 03:35 |
charlie schindler Messages: 28 Registered: June 2014 |
Junior Member |
|
|
Nigel i got also a problem with Tree Leaves. though a bit different.
I want to catch an event a Tree Item child is selected
ParentItem
-- ChildItem
when Child Item is selected i want to do some actions
here is the listener I added:
selectedForm.getTreeWidget().addSelectionListener(new SelectionListener()
{
@Override
public void widgetSelected(SelectionEvent event)
{
// TODO Auto-generated method stub
TreeItem[] treeItems = selectedForm.getTreeWidget().getItems();
final TreeItem item = (TreeItem)event.item;
if (item != null && item.toString().equals(treeItems[0].toString()))
{
//selectedForm.getDvTextForm().forceFocus();
selectedForm.setDvTextFormStyledDVText("gaga");
}
}
@Overrid
public void widgetDefaultSelected(SelectionEvent e)
{
// TODO Auto-generated method stub
}
});
The problem is that
TreeItem[] treeItems = selectedForm.getTreeWidget().getItems();
returns only 1 item, the "ParentItem"
I can't find a way to get the Children of ParentItem
if i set treeItems[0] to treeItems[1] it get's an OutOfBound Exception
if (item != null && item.toString().equals(treeItems[0].toString()))
btw. the snippets do not help on such questions as the Tree snippets mostly cover tables and trees only with a parent
[Updated on: Thu, 30 October 2014 03:37] Report message to a moderator
|
|
|
|
Re: Tree leaves [message #1456405 is a reply to message #1456128] |
Thu, 30 October 2014 10:20 |
|
Charlie just for interest I found this was useful for my tree with leaves that could be checked or selected. The tree was:
final Tree g8_tree = new Tree(g8_main_script_tree, SWT.BORDER | SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL );
and the listener:
g8_tree.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
TreeItem item = (TreeItem) event.item;
String string = event.detail == SWT.CHECK ? "Checked" : "Selected";
if (string.matches("Selected")) {
System.out.println(" was selected.");
.....................................
if (string.matches("Checked")) {
System.out.println(" was checked.");
My outstanding SWT tree issue at the moment is can I have a tree with say level one leaves/branches with check boxes and say level two without?
Cheers Nigel
Performance/Load/Stress tester for large public and private sector orgs because I enjoy it.
|
|
|
|
Powered by
FUDForum. Page generated in 0.03998 seconds