1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| public class JTreeSelectionExample {
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree(createTree());
tree.setScrollsOnExpand(true);
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
for(KeyListener keyListener : tree.getKeyListeners()) {
tree.removeKeyListener(keyListener);
}
tree.addKeyListener(createKeyListener(tree));
frame.add(new JScrollPane(tree));
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static KeyListener createKeyListener(JTree tree) {
return new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
TreeNode node;
TreePath current = tree.getSelectionPath();
if( current==null ) {
node = (TreeNode) tree.getModel().getRoot();
}
else {
node = getNextSibling((TreeNode) current.getLastPathComponent());
if ( node==null ) {
node = (TreeNode) tree.getModel().getRoot();
}
}
TreeNode nextNode = getNextNode(node, c);
if( nextNode==null ) {
nextNode = getNextNode((TreeNode)tree.getModel().getRoot(), c);
}
if( nextNode==null ) {
return;
}
TreePath path = createTreePath(nextNode);
tree.setSelectionPath(path);
tree.scrollPathToVisible(path);
}
private TreePath createTreePath(TreeNode node) {
Deque<TreeNode> stack = new ArrayDeque<>();
while(node!=null) {
stack.push(node);
node=node.getParent();
}
return new TreePath(stack.toArray());
}
private TreeNode getNextSibling(TreeNode node) {
TreeNode parent = node.getParent();
while ( parent!=null ) {
int index = parent.getIndex(node)+1;
if ( index<parent.getChildCount() ) {
return parent.getChildAt(index);
}
node = parent;
parent = node.getParent();
}
return null;
}
private TreeNode getNextNode(TreeNode node, char c) {
return getNextNode(node, c, true);
}
private TreeNode getNextNode(TreeNode node, char c, boolean nextSibling) {
if ( match(node,c) ) {
return node;
}
for(int i=0; i<node.getChildCount(); i++) {
TreeNode child = getNextNode(node.getChildAt(i), c, false);
if ( child!=null ) {
return child;
}
}
if (nextSibling) {
TreeNode parent = node.getParent();
while ( parent!=null ) {
int index = parent.getIndex(node)+1;
for(int i=index; i<parent.getChildCount(); i++) {
TreeNode child = getNextNode(parent.getChildAt(i), c, false);
if ( child!=null ) {
return child;
}
}
node = parent;
parent = node.getParent();
}
}
return null;
}
private boolean match(TreeNode node, char c) {
String s = node.toString();
return !s.isEmpty() && Character.toLowerCase(s.charAt(0))==Character.toLowerCase(c);
}
};
}
private static MutableTreeNode createTree() throws IOException {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
createTree(root,FileSystemView.getFileSystemView().getHomeDirectory().toPath(),2);
return root;
}
private static void createTree(DefaultMutableTreeNode parent, Path dir, int depth) throws IOException {
StreamSupport.stream(Files.newDirectoryStream(dir).spliterator(),false)
.map(path-> createNode(path, depth))
.forEach(parent::add);
}
private static MutableTreeNode createNode(Path path, int depth) {
FileNode node = new FileNode(path);
if( depth>0 && Files.isDirectory(path) ) {
try {
createTree(node, path, depth-1);
} catch (IOException e) {
// ignore
}
}
return node;
}
private static class FileNode extends DefaultMutableTreeNode {
public FileNode(Path path) {
super(path.getFileName());
}
}
} |
Partager