Overview
Warning: This class is an experimental/preview control that is not final and will change in future releases. If you have any feedback on this control, please post it to the JavaFX JIRA issue tracker.
An implementation of TreeItemBase which provides two possible approaches to building up a tree structure. The first approach is to manually build the structure when the root node is first built. This requires more memory but means that once the data structure is created there is no requirement to do anything else other than placing it in a TreeVew. An example of this approach is shown here:
var simpleTree = TreeItem {
data: "Root node";
expanded: true
children: [
TreeItem { data: "leaf 1" },
TreeItem { data: "leaf 2" },
TreeItem { data: "leaf 3" },
TreeItem {
data: "branch 1"
expanded: true
children: [
TreeItem { data: "leaf 4" },
TreeItem { data: "leaf 5" },
TreeItem { data: "leaf 6" },
TreeItem {
data: "branch 2"
expanded: true
children: [
TreeItem { data: "leaf 7" },
TreeItem { data: "leaf 8" },
TreeItem { data: "leaf 9" },
]
}
]
}
TreeItem { data: "leaf 10" }
]
}
To create a TreeView from this is then achieved by doing the following:
def simpleTreeView = TreeView {
root: simpleTree;
}
The alternative approach is to recursively build the data structure on-demand by providing functions for createChildren and isLeaf. The example shown below is a Windows-specific filesystem browser built using the TreeView control as well a TreeItem. It is a trivial change to modify this sample to browse alternative file systems (just change the reference to c:/ to suit your file system).
import javafx.stage.Stage;
import javafx.scene.Scene;
import com.javafx.preview.control.TreeItem;
import com.javafx.preview.control.TreeItemBase;
import com.javafx.preview.control.TreeView;
import java.io.File;
var tree = TreeView {
root: TreeItem {
data: new File("c:/")
expanded:true
isLeaf: isLeaf
createChildren: createChildren;
}
}
function createChildren(item:TreeItemBase):TreeItemBase[] {
def file = item.data as File;
if (file.isDirectory()) {
return for (f in file.listFiles()) {
TreeItem {
data: f
createChildren: createChildren;
isLeaf: isLeaf;
}
}
}
return [];
}
function isLeaf(item:TreeItemBase):Boolean {
def file = item.data as File;
return file.isFile();
}
Stage {
scene: Scene {
content: [ tree ]
}
}
Note how this approach differs from the first one. Both approaches have different positives and negatives, so it is important to test both approaches, if applicable, and select the most appropriate.
See Also:
TreeView, TreeItemBase
Profile: common
Variable Summary
access | name | type | Can Read | Can Init | Can Write | Default Value | description |
---|---|---|---|---|---|---|---|
public | createChildren | function(:TreeItemBase):Sequence |
A callback function used when building up the tree content automatically. A callback function used when building up the tree content automatically. There is no requirement to set this function, as you may choose to manually build and maintain the children sequence. Refer to the class documentation above to understand the difference between these two approaches. Note: If you do implement this function, you should also implement the isLeaf function. |
||||
public | isLeaf | function(:TreeItemBase):boolean |
A callback function used to determine whether an item is a leaf item (that is, whether it does not have any children). A callback function used to determine whether an item is a leaf item (that is, whether it does not have any children). Refer to the class documentation above to understand the difference between these two approaches. Note: If you do implement this function, you should also implement the createChildren function. |
||||
public | onSelected | function():Void |
A callback function to notify you as to when this TreeItem is selected. A callback function to notify you as to when this TreeItem is selected. Selection can occur through numerous means, but most commonly it is via mouse, keyboard or programmatically using the select function. |
Inherited Variables
com.javafx.preview.control.TreeItemBase
access | name | type | Can Read | Can Init | Can Write | Default Value | description |
---|---|---|---|---|---|---|---|
public | children | TreeItemBase[] |
The children of this TreeItem. The children of this TreeItem. This is writable only by subclasses so as to provide subclasses control over when and what can be added. For example, a FileTreeItem implementation might want to maintain control and update the subitems based on actual changes to the filesystem. The children may well not be initialized until expanded is set to true. This delayed computation is a critical property of TreeItems so as to be scalable to large and complex data sets. |
||||
public | data | Object |
The application-specific data represented by this TreeItem. The application-specific data represented by this TreeItem. If no custom
cellFactory is specified, the |
||||
public | expanded | Boolean |
Specifies whether the subitems of this TreeItem, if any, are visually displayed. Specifies whether the subitems of this TreeItem, if any, are visually displayed. When a TreeItem is expanded, both the item and all of its subitems are visible. When not expanded, the subitems are not visible and only the TreeItem itself can be interacted with. |
||||
public | graphic | Node |
The node to show to the left of the data object. The node to show to the left of the data object. For best effect, this tends to be a 16x16 image. |
||||
public-read | itemCount | Integer |
A count of the number of items which currently exist for this item and all children where the parent nodes have expanded set to true. |
||||
public-read protected | leaf | Boolean | subclass | subclass | |||
public-read | level | Integer | |||||
public-read | parent | TreeItemBase |
The parent of this TreeItem. The parent of this TreeItem. Each TreeItem can have no more than one parent. |
Inherited Functions
com.javafx.preview.control.TreeItemBase
- public getNextSibling() : TreeItemBase
- public getNextSibling(afterNode: TreeItemBase) : TreeItemBase
- public getPreviousSibling() : TreeItemBase
- public getPreviousSibling(beforeNode: TreeItemBase) : TreeItemBase