Example usage for javax.swing JTree setCellRenderer

List of usage examples for javax.swing JTree setCellRenderer

Introduction

In this page you can find the example usage for javax.swing JTree setCellRenderer.

Prototype

@BeanProperty(description = "The TreeCellRenderer that will be used to draw each cell.")
public void setCellRenderer(TreeCellRenderer x) 

Source Link

Document

Sets the TreeCellRenderer that will be used to draw each cell.

Usage

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);/*  w  w w.j  a  v a  2 s .c o m*/
    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: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//from   w  ww  . j ava2 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: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//from  ww w. j ava2 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:Main.java

public static void main(String[] args) throws Exception {
    final JTree tree = new JTree(getTreeModel());
    tree.setCellRenderer(new MyTreeCellRenderer());
    HyperlinkMouseListener listener = new HyperlinkMouseListener(tree);
    tree.addMouseListener(listener);/*from w w w. j  av a 2s .  c om*/
    tree.addMouseMotionListener(listener);
    JFrame f = new JFrame(Main.class.getSimpleName());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(tree), BorderLayout.CENTER);
    f.pack();
    f.setSize(f.getWidth() + 100, f.getHeight() + 100);
    f.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();/*from  ww  w  . j a  v  a 2s . c om*/
    f.setVisible(true);
}

From source file:TreeNodeVectorA.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Employee javaBooks[] = { new Employee("A", "F", 9.99f), new Employee("B", "E", 4.99f),
            new Employee("C", "D", 9.95f) };
    Employee netBooks[] = { new Employee("AA", "CC", 9.99f), new Employee("BB", "DD", 9.99f) };
    Vector<Employee> javaVector = new TreeNodeVectorA<Employee>("A", javaBooks);
    Vector<Employee> netVector = new TreeNodeVectorA<Employee>("As", netBooks);
    Object rootNodes[] = { javaVector, netVector };
    Vector<Object> rootVector = new TreeNodeVectorA<Object>("Root", rootNodes);
    JTree tree = new JTree(rootVector);
    TreeCellRenderer renderer = new EmployeeCellRenderer();
    tree.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 300);/* w  w  w  .  ja  v a 2  s  .  c om*/
    frame.setVisible(true);
}

From source file:ToolTipTreeCellRenderer.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Tree Tips");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    JTree tree = new JTree(props);

    ToolTipManager.sharedInstance().registerComponent(tree);
    TreeCellRenderer renderer = new ToolTipTreeCellRenderer(props);

    tree.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);//from  ww  w . ja v  a  2s  . c o  m
    frame.setVisible(true);
}

From source file:TreeTips.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Tree Tips");
    Properties props = System.getProperties();
    JTree tree = new JTree(props);
    ToolTipManager.sharedInstance().registerComponent(tree);
    TreeCellRenderer renderer = new ToolTipTreeCellRenderer(props);
    tree.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);/* ww  w  .ja  v  a  2 s  . co m*/
    frame.setVisible(true);
}

From source file:TreeNodeVector.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Book Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Employee javaBooks[] = { new Employee("A", "F", 9.99f), new Employee("B", "E", 4.99f),
            new Employee("C", "D", 9.95f) };
    Employee netBooks[] = { new Employee("AA", "CC", 9.99f), new Employee("BB", "DD", 9.99f) };
    Vector<Employee> javaVector = new TreeNodeVector<Employee>("A", javaBooks);
    Vector<Employee> netVector = new TreeNodeVector<Employee>("As", netBooks);
    Object rootNodes[] = { javaVector, netVector };
    Vector<Object> rootVector = new TreeNodeVector<Object>("Root", rootNodes);
    JTree tree = new JTree(rootVector);
    TreeCellRenderer renderer = new EmployeeCellRenderer();
    tree.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 300);/*from   w ww . java2 s . c  o  m*/
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Tree Tips");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTree tree = new JTree(new String[] { "a", "b", "c" });
    ToolTipManager.sharedInstance().registerComponent(tree);
    TreeCellRenderer renderer = new ToolTipTreeCellRenderer();
    tree.setCellRenderer(renderer);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);/*ww  w .  java  2 s  .c  o  m*/
    frame.setVisible(true);
}