JTree

package demo;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

/**
 * This class is a JTree subclass that displays the tree of AWT or Swing
 * component that make up a GUI.
 */
public class ComponentTree extends JTree {
 /**
  * All this constructor method has to do is set the TreeModel and
  * TreeCellRenderer objects for the tree. It is these classes (defined
  * below) that do all the real work.
  */
 public ComponentTree(Component c) {
  super(new ComponentTreeModel(c));
  setCellRenderer(new ComponentCellRenderer(getCellRenderer()));
 }

 /**
  * The TreeModel class puts hierarchical data in a form that the JTree can
  * display. This implementation interprets the containment hierarchy of a
  * Component for display by the ComponentTree class. Note that any kind of
  * Object can be a node in the tree, as long as the TreeModel knows how to
  * handle it.
  */
 static class ComponentTreeModel implements TreeModel {
  Component root; // The root object of the tree

  // Constructor: just remember the root object
  public ComponentTreeModel(Component root) {
   this.root = root;
  }

  // Return the root of the tree
  public Object getRoot() {
   return root;
  }

  // Is this node a leaf? (Leaf nodes are displayed differently by JTree)
  // Any node that isn't a container is a leaf, since they cannot have
  // children. We also define containers with no children as leaves.
  public boolean isLeaf(Object node) {
   if (!(node instanceof Container))
    return true;
   Container c = (Container) node;
   return c.getComponentCount() == 0;
  }

  // How many children does this node have?
  public int getChildCount(Object node) {
   if (node instanceof Container) {
    Container c = (Container) node;
    return c.getComponentCount();
   }
   return 0;
  }

  // Return the specified child of a parent node.
  public Object getChild(Object parent, int index) {
   if (parent instanceof Container) {
    Container c = (Container) parent;
    return c.getComponent(index);
   }
   return null;
  }

  // Return the index of the child node in the parent node
  public int getIndexOfChild(Object parent, Object child) {
   if (!(parent instanceof Container))
    return -1;
   Container c = (Container) parent;
   Component[] children = c.getComponents();
   if (children == null)
    return -1;
   for (int i = 0; i < children.length; i++) {
    if (children[i] == child)
     return i;
   }
   return -1;
  }

  // This method is only required for editable trees, so it is not
  // implemented here.
  public void valueForPathChanged(TreePath path, Object newvalue) {
  }

  // This TreeModel never fires any events (since it is not editable)
  // so event listener registration methods are left unimplemented
  public void addTreeModelListener(TreeModelListener l) {
  }

  public void removeTreeModelListener(TreeModelListener l) {
  }
 }

 /**
  * A TreeCellRenderer displays each node of a tree. The default renderer
  * displays arbitrary Object nodes by calling their toString() method. The
  * Component.toString() method returns long strings with extraneous
  * information. Therefore, we use this "wrapper" implementation of
  * TreeCellRenderer to convert nodes from Component objects to useful String
  * values before passing those String values on to the default renderer.
  */
 static class ComponentCellRenderer implements TreeCellRenderer {
  TreeCellRenderer renderer; // The renderer we are a wrapper for

  // Constructor: just remember the renderer
  public ComponentCellRenderer(TreeCellRenderer renderer) {
   this.renderer = renderer;
  }

  // This is the only TreeCellRenderer method.
  // Compute the string to display, and pass it to the wrapped renderer
  public Component getTreeCellRendererComponent(JTree tree, Object value,
    boolean selected, boolean expanded, boolean leaf, int row,
    boolean hasFocus) {
   String newvalue = value.getClass().getName(); // Component type
   String name = ((Component) value).getName(); // Component name
   if (name != null)
    newvalue += " (" + name + ")"; // unless null
   // Use the wrapped renderer object to do the real work
   return renderer.getTreeCellRendererComponent(tree, newvalue,
     selected, expanded, leaf, row, hasFocus);
  }
 }

 /**
  * This main() method demonstrates the use of the ComponentTree class: it
  * puts a ComponentTree component in a Frame, and uses the ComponentTree to
  * display its own GUI hierarchy. It also adds a TreeSelectionListener to
  * display additional information about each component as it is selected
  */
 public static void main(String[] args) {
  // Create a frame for the demo, and handle window close requests
  JFrame frame = new JFrame("ComponentTree Demo");
  frame.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });

  // Create a scroll pane and a "message line" and add them to the
  // center and bottom of the frame.
  JScrollPane scrollpane = new JScrollPane();
  final JLabel msgline = new JLabel(" ");
  frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
  frame.getContentPane().add(msgline, BorderLayout.SOUTH);

  // Now create the ComponentTree object, specifying the frame as the
  // component whose tree is to be displayed. Also set the tree's font.
  JTree tree = new ComponentTree(frame);
  tree.setFont(new Font("SansSerif", Font.BOLD, 12));

  // Only allow a single item in the tree to be selected at once
  tree.getSelectionModel().setSelectionMode(
    TreeSelectionModel.SINGLE_TREE_SELECTION);

  // Add an event listener for notifications when
  // the tree selection state changes.
  tree.addTreeSelectionListener(new TreeSelectionListener() {
   public void valueChanged(TreeSelectionEvent e) {
    // Tree selections are referred to by "path"
    // We only care about the last node in the path
    TreePath path = e.getPath();
    Component c = (Component) path.getLastPathComponent();
    // Now we know what component was selected, so
    // display some information about it in the message line
    if (c.isShowing()) {
     Point p = c.getLocationOnScreen();
     msgline.setText("x: " + p.x + "  y: " + p.y + "  width: "
       + c.getWidth() + "  height: " + c.getHeight());
    } else {
     msgline.setText("component is not showing");
    }
   }
  });

  // Now that we've set up the tree, add it to the scrollpane
  scrollpane.setViewportView(tree);

  // Finally, set the size of the main window, and pop it up.
  frame.setSize(600, 400);
  frame.setVisible(true);
 }
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值