Using DefaultTreeCellRenderer : Tree Renderer Editor « Swing JFC « Java






Using DefaultTreeCellRenderer

  
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

public class Main extends JFrame {
  private JTree m_simpleTree;

  private DefaultMutableTreeNode m_rootNode;

  public Main() {
    m_rootNode = new DefaultMutableTreeNode("A");

    DefaultMutableTreeNode enggNode = new DefaultMutableTreeNode("B");
    DefaultMutableTreeNode markNode = new DefaultMutableTreeNode("C");
    DefaultMutableTreeNode hrNode = new DefaultMutableTreeNode("D");

    m_rootNode.add(enggNode);
    m_rootNode.add(markNode);
    m_rootNode.add(hrNode);
    m_simpleTree = new JTree(m_rootNode);

    DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer) m_simpleTree.getCellRenderer();

    cellRenderer.setOpenIcon(new ImageIcon("open.gif"));
    cellRenderer.setClosedIcon(new ImageIcon("closed.gif"));
    cellRenderer.setLeafIcon(new ImageIcon("leaf.gif"));

    cellRenderer.setBackgroundNonSelectionColor(new Color(255, 255, 221));
    cellRenderer.setBackgroundSelectionColor(new Color(0, 0, 128));
    cellRenderer.setBorderSelectionColor(Color.black);
    cellRenderer.setTextSelectionColor(Color.white);
    cellRenderer.setTextNonSelectionColor(Color.blue);

    JScrollPane scrollPane = new JScrollPane(m_simpleTree);

    add(scrollPane);
  }
  public static void main(String[] arg) {
    Main m = new Main();

    m.setVisible(true);
    m.setSize(new Dimension(450, 300));
    m.validate();
  }
}

   
    
  








Related examples in the same category

1.Tree Cell RendererTree Cell Renderer
2.CheckBox Node Tree SampleCheckBox Node Tree Sample
3.Combobox Tree Node EditorCombobox Tree Node Editor
4.extends DefaultTreeCellEditor to create Tree Leaf editorextends DefaultTreeCellEditor to create Tree Leaf editor
5.Tree Changed RendererTree Changed Renderer
6.Build a tree and populate it with custom renderers and editorsBuild a tree and populate it with custom renderers and editors
7.Scrollable Table Cell Renderer
8.This program demonstrates cell rendering and listening to tree selection events.This program demonstrates cell rendering and listening to tree selection events.