Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Tree leaves(Throuble creating SWT tree leaves)
icon9.gif  Tree leaves [message #1412880] Wed, 27 August 2014 09:25 Go to next message
Nigel Hoath is currently offline Nigel HoathFriend
Messages: 14
Registered: August 2014
Location: UK
Junior Member
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 #1412979 is a reply to message #1412880] Wed, 27 August 2014 13:40 Go to previous messageGo to next message
Niraj Modi is currently offline Niraj ModiFriend
Messages: 48
Registered: November 2013
Member
Please refer the Tree section on SWT Snippets page:
http://eclipse.org/swt/snippets/

-Niraj
Re: Tree leaves [message #1443179 is a reply to message #1412979] Sun, 12 October 2014 08:59 Go to previous messageGo to next message
Nigel Hoath is currently offline Nigel HoathFriend
Messages: 14
Registered: August 2014
Location: UK
Junior Member
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 Go to previous messageGo to next message
Nigel Hoath is currently offline Nigel HoathFriend
Messages: 14
Registered: August 2014
Location: UK
Junior Member
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 Go to previous messageGo to next message
charlie schindler is currently offline charlie schindlerFriend
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 #1456128 is a reply to message #1456118] Thu, 30 October 2014 03:48 Go to previous messageGo to next message
charlie schindler is currently offline charlie schindlerFriend
Messages: 28
Registered: June 2014
Junior Member
sorry for the question
solved it. sometimes asking questions helps finding solutions Smile

                  TreeItem[] treeItems = selectedForm.getTreeWidget().getItems();
                  TreeItem[] childItems = treeItems[0].getItems();
                  final TreeItem item = (TreeItem)event.item;

                  if (item != null && item.toString().equals(childItems[0].toString()))
Re: Tree leaves [message #1456405 is a reply to message #1456128] Thu, 30 October 2014 10:20 Go to previous messageGo to next message
Nigel Hoath is currently offline Nigel HoathFriend
Messages: 14
Registered: August 2014
Location: UK
Junior Member
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.
Re: Tree leaves [message #1459966 is a reply to message #1456405] Mon, 03 November 2014 03:56 Go to previous message
charlie schindler is currently offline charlie schindlerFriend
Messages: 28
Registered: June 2014
Junior Member
Hi Nigel

Thx for sharing. interesting listener.

About your issue. I do not know, So far I do not use checkboxes in tree items.
but theoretically this should be possible, as you can set checked or not checked for each tree item.
Previous Topic:How do I contribute to SWT?
Next Topic: Ubuntu Java Swt Browser Crash While creating webkit mutiple instance
Goto Forum:
  


Current Time: Thu Mar 28 19:13:14 GMT 2024

Powered by FUDForum. Page generated in 0.02166 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top