Last active
July 8, 2020 09:57
-
-
Save codenameone/870d4412694bca3092c4 to your computer and use it in GitHub Desktop.
Simple Tree Model and Tree component from Codename One
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StringArrayTreeModel implements TreeModel { | |
String[][] arr = new String[][] { | |
{"Colors", "Letters", "Numbers"}, | |
{"Red", "Green", "Blue"}, | |
{"A", "B", "C"}, | |
{"1", "2", "3"} | |
}; | |
public Vector getChildren(Object parent) { | |
if(parent == null) { | |
Vector v = new Vector(); | |
for(int iter = 0 ; iter < arr[0].length ; iter++) { | |
v.addElement(arr[0][iter]); | |
} | |
return v; | |
} | |
Vector v = new Vector(); | |
for(int iter = 0 ; iter < arr[0].length ; iter++) { | |
if(parent == arr[0][iter]) { | |
if(arr.length > iter + 1 && arr[iter + 1] != null) { | |
for(int i = 0 ; i < arr[iter + 1].length ; i++) { | |
v.addElement(arr[iter + 1][i]); | |
} | |
} | |
} | |
} | |
return v; | |
} | |
public boolean isLeaf(Object node) { | |
Vector v = getChildren(node); | |
return v == null || v.size() == 0; | |
} | |
} | |
Tree dt = new Tree(new StringArrayTreeModel()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage code for Tree & TreeModel.
From the Codename One project