List of usage examples for javax.swing JTree JTree
@ConstructorProperties({ "model" }) public JTree(TreeModel newModel)
JTree
which displays the root node -- the tree is created using the specified data model. From source file:TreeExpansionListenerDemo.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); root.add(new DefaultMutableTreeNode("A")); root.add(new DefaultMutableTreeNode("B")); root.add(new DefaultMutableTreeNode("C")); JTree tree = new JTree(root); TreeExpansionListener treeExpandListener = new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent event) { TreePath path = event.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("Expanded: " + data); }//from ww w.j a v a2s .com public void treeCollapsed(TreeExpansionEvent event) { TreePath path = event.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("Collapsed: " + data); } }; tree.addTreeExpansionListener(treeExpandListener); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); root.add(new DefaultMutableTreeNode("A")); root.add(new DefaultMutableTreeNode("B")); root.add(new DefaultMutableTreeNode("C")); JTree tree = new JTree(root); TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() { public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException { TreePath path = treeExpansionEvent.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("WillCollapse: " + data); }//from ww w . j ava 2s. c o m public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException { TreePath path = treeExpansionEvent.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("WillExpand: " + data); } }; tree.addTreeWillExpandListener(treeWillExpandListener); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); final JTree tree = new JTree(buildDemoModel()); tree.setCellRenderer(new DefaultTreeCellRenderer() { private Icon loadIcon = UIManager.getIcon("OptionPane.errorIcon"); private Icon saveIcon = UIManager.getIcon("OptionPane.informationIcon"); @Override/* w w w . j a v a2 s.c o m*/ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean isLeaf, int row, boolean focused) { Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row, focused); if (selected) setIcon(loadIcon); else setIcon(saveIcon); return c; } }); tree.setVisibleRowCount(10); frame.add(new JScrollPane(tree)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }
From source file:PrintDescendants.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode mercury = new DefaultMutableTreeNode("Mercury"); root.add(mercury);// w ww . j a va 2s . c om DefaultMutableTreeNode venus = new DefaultMutableTreeNode("Venus"); root.add(venus); DefaultMutableTreeNode mars = new DefaultMutableTreeNode("Mars"); root.add(mars); JTree tree = new JTree(root); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); printDescendants(root); }
From source file:Main.java
public static void main(String[] args) { DefaultMutableTreeNode root = new DefaultMutableTreeNode(Boolean.TRUE); DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(Boolean.FALSE); DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(Boolean.FALSE); root.add(child1);//from w w w. j a v a2 s .c om root.add(child2); DefaultTreeModel model = new DefaultTreeModel(root); JTree tree = new JTree(model); tree.setCellRenderer(new TreeRenderer()); tree.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { TreePath pathForLocation = tree.getPathForLocation(e.getX(), e.getY()); Object lastPathComponent = pathForLocation.getLastPathComponent(); if (lastPathComponent instanceof DefaultMutableTreeNode) { Boolean oldObject = (Boolean) ((DefaultMutableTreeNode) lastPathComponent).getUserObject(); ((DefaultMutableTreeNode) lastPathComponent).setUserObject(!oldObject); model.nodeChanged((DefaultMutableTreeNode) lastPathComponent); } } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(tree, BorderLayout.CENTER); frame.setSize(800, 600); frame.setVisible(true); }
From source file:DynamicUtilTreeNodeDemo.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); Hashtable<String, Object> hashtable = new Hashtable<String, Object>(); hashtable.put("Two", new String[] { "A", "B", "C" }); Hashtable<Object, Object> innerHashtable = new Hashtable<Object, Object>(); innerHashtable.put("Two", new String[] { "A", "B", "C" }); hashtable.put("Three", innerHashtable); JTree.DynamicUtilTreeNode.createChildren(root, hashtable); JTree tree = new JTree(root); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150);/*from ww w . jav a2 s.c o m*/ frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(Main.class.getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTree tree = new JTree(createTreeModel(new File(System.getProperty("user.dir")), true)); tree.setCellRenderer(new DefaultTreeCellRenderer() { @Override//w w w . j av a2 s . c o m public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); if (value instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; if (node.getUserObject() instanceof File) { setText(((File) node.getUserObject()).getName()); } } return this; } }); tree.getSelectionModel().addTreeSelectionListener(e -> { Object object = tree.getLastSelectedPathComponent(); if (object instanceof DefaultMutableTreeNode) { Object userObject = ((DefaultMutableTreeNode) object).getUserObject(); if (userObject instanceof File) { File file = (File) userObject; System.out.println( "Selected file" + file.getAbsolutePath() + " Is directory? " + file.isDirectory()); } } }); JScrollPane pane = new JScrollPane(tree); frame.add(pane); frame.setSize(400, 600); frame.setVisible(true); }
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("DynamicUtilTreeNode Hashtable"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); Hashtable<Object, Object> hashtable = new Hashtable<Object, Object>(); hashtable.put("A", args); hashtable.put("B", new String[] { "a", "b", "c" }); Hashtable<Object, Object> innerHashtable = new Hashtable<Object, Object>(); Properties props = System.getProperties(); innerHashtable.put(props, props);//from w w w .j a v a2 s . c o m hashtable.put("C", innerHashtable); JTree.DynamicUtilTreeNode.createChildren(root, hashtable); JTree tree = new JTree(root); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { Node root = new Node("Root"); fillTree(root, 5, "tree label"); DefaultTreeModel model = new DefaultTreeModel(root); JTree tree = new JTree(model); ToolTipManager.sharedInstance().registerComponent(tree); tree.setCellRenderer(new MyTreeCellRenderer()); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new JScrollPane(tree)); f.pack();/* w w w . j a v a 2 s. co m*/ f.setVisible(true); }
From source file:TreeNodeVector.java
public static void main(final String args[]) { JFrame frame = new JFrame("JTreeSample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Vector<String> v1 = new TreeNodeVector<String>("Two", new String[] { "Mercury", "Venus", "Mars" }); Vector<Object> v2 = new TreeNodeVector<Object>("Three"); v2.add(System.getProperties()); v2.add(v1);//from ww w.ja va 2 s . co m Object rootNodes[] = { v1, v2 }; Vector<Object> rootVector = new TreeNodeVector<Object>("Root", rootNodes); JTree tree = new JTree(rootVector); frame.add(new JScrollPane(tree), BorderLayout.CENTER); frame.setSize(300, 300); frame.setVisible(true); }