|
I know how to create a tree. I guess what I
have in mind is odd and no one else really care about doing it that way. I
have no trouble with the concept of tree and programming, just haven't used Java
language for a few years.
I will just do what everyone else do. That
is, MyFolder = new TreeItem(tree, style) and create the rest of my
structure (ie. Items) under MyFolder. You see, originally, I was thinking
to allow having Items at the same level as MyFolder, but that would mean I have
to handle the case of creating Items by calling items = new TreeItem(tree,
style) instead of only have to call items = new TreeItem(MyFolder,
style). So I'd only need to call new TreeItem(tree,style) once at the
begining, which is exactly what I have implemented. Now that I've
described my whole idea without ever using the words root or cast, I hope we are
clear on that.
Note: Of course in the structure under MyFolder, an
Item can be a folder as well.
My conclusion is that the answer to my original
question is simply "No".
You don't cast anything to anything. Nor does SWT create a
default root item for you. What you do is as
follows:
... Tree tree =
new Tree(parent, SWT.BORDER | SWT.SINGLE |
SWT.FULL_SELECTION);
TreeItem root1 = new
TreeItem(tree, SWT.NONE); root1.setText("This is the
first root item");
TreeItem root2 = new
TreeItem(tree, SWT.NONE); root2.setText("This is the
second root item");
TreeItem child1 = new
TreeItem(root2, SWT.NONE); child1.setText("This item
is the first child item"); ...
This
code will create a tree structure as follows:
| -
This is the first root item | - This is the second root
item | - This is the first
child item
As you see, there are litterally two root items, not one
invisible root item. This simplifies things conceptually and saves on
memory. As to the overloaded constructors for TreeItem, I suggest that
you do as I do when I work with Tree(s): create a method to add a TreeItem to
a Tree based on data passed to it, and then overload the method for adding a
TreeItem to another TreeItem. You can litterally cut and paste your code
from one method to the other as long as you keep your parameter names
straight.
Daniel
Joel Chen wrote:
I am not sure what you mean, but I am not that
good with SWT and a bit rusty with Java itself, however I see no remedy to
my situation from what you said. The fact that there are two ways I
can create a TreeItem like:
new TreeItem( Tree, style )
or
new TreeItem( TreeItem, style )
and that you can't cast Tree to TreeItem is
kind of clunky and I have to have two versions of some of my code to deal
with it.
If Tree creates a default root item that's
invisible, then I don't have this problem. Even then, you can still
have multiple "root" items under the default root item, right?
I am sure I am missing something here since no
one seem to complain about this, but I see no clear way around
it.
You can create multiple root tree items on the Tree
itself. Unlike with Swing, you can have more than one root.
Thus, accomplishing in actuality what Swing accomplishes by
illusion.
Daniel
Joel Chen wrote:
Is is possible to create a TreeItem that's invisble to serve as the root
item? I found myself end up with a bunch of functions with identical body
but one takes Tree and another takes TreeItem in its parameters. I could
get around this by just creating a rootTreeItem but now I lost some space in
the left margin in my tree display.
|