Java Swing - Tree
package jlib5.swing; import javax.swing.*; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; public class Tree { public JTree ctrl; JPopupMenu menu; public Tree(String rootName, Runnable handler) { DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootName); /* root.add( new DefaultMutableTreeNode("하위1") ); root.add( new DefaultMutableTreeNode("하위2") ); */ ctrl = new JTree(root); if( handler != null ) { setTreeHandler(handler); } } public void showTreeRoot() { ctrl.setRootVisible(true);} public void hideTreeRoot() { ctrl.setRootVisible(false);} public void expandAll() { for (int i = 0; i < ctrl.getRowCount(); i++) { ctrl.expandRow(i); } } public void clearAll() { ctrl.getSelectionModel().clearSelection(); DefaultTreeModel model = (DefaultTreeModel) ctrl.getModel(); DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot(); root.removeAllChildren(); model.reload(); //model.setRoot(null); } public void clear(DefaultMutableTreeNode node) { DefaultTreeModel model = (DefaultTreeModel) ctrl.getModel(); node.removeAllChildren(); model.reload(); //model.setRoot(null); } public void addContextMenu(String name, Runnable handler) { JMenuItem item = new JMenuItem(name); item.addActionListener( (e) -> handler.run() ); if( this.menu == null ) { this.menu = new JPopupMenu(); this.menu.add(item); ctrl.setComponentPopupMenu(this.menu); } else { menu.add(item); } } public void setTreeHandler(Runnable runnable) { ctrl.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener(){ public void valueChanged(TreeSelectionEvent event) { runnable.run(); } }); } public String getTreeSelectionPath() { return ctrl.getSelectionModel().getSelectionPath().toString(); } public DefaultMutableTreeNode addTreeToRoot(String name) { DefaultTreeModel model = (DefaultTreeModel) ctrl.getModel(); DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot(); DefaultMutableTreeNode node = new DefaultMutableTreeNode(name); model.insertNodeInto(node, root, root.getChildCount()); //model.insertNodeInto(node, root, model.getChildCount(root)); model.reload(root); return node; } public DefaultMutableTreeNode addTree(DefaultMutableTreeNode parent, String name) { DefaultTreeModel model = (DefaultTreeModel) ctrl.getModel(); DefaultMutableTreeNode node = new DefaultMutableTreeNode(name); model.insertNodeInto(node, parent, parent.getChildCount()); model.reload(parent); return node; } public DefaultMutableTreeNode getSelectedNode() { return (DefaultMutableTreeNode)ctrl.getLastSelectedPathComponent(); } public String[] getSelectedTreePath() { TreePath path = ctrl.getSelectionPath(); if( path != null ) { int count = path.getPathCount(); String[] nodes = new String[count]; for( int i = 0; i < count; i++ ) { nodes[count-i-1] = path.getLastPathComponent().toString(); path = path.getParentPath(); } return nodes; } return null; } public String getTreeToString(DefaultMutableTreeNode node, String prefix) { StringBuilder sb = new StringBuilder(); if( prefix == null ) { prefix = ""; sb.append(node.toString()).append("\n| \n"); } if( node.getChildCount() == 0 ) { return prefix + node.toString() + "\n"; } else { int num = node.getChildCount(); for (int i = 0; i < num; i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i); if( i == num-1 ) { sb.append(prefix).append("`--").append(child).append("\n"); if (child.getChildCount() > 0) { sb.append(getTreeToString(child, prefix + " ")); } } else { sb.append(prefix).append("|--").append(child).append("\n"); if (child.getChildCount() > 0) { sb.append(getTreeToString(child, prefix + "| ")); } } } return sb.toString(); } } }
댓글 없음:
댓글 쓰기