Java Tutorial/Swing/JTree Editor Renderer

Материал из Java эксперт
Версия от 15:31, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Changing and Removing the Default Icons in a JTree Component

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeCellRenderer;
public class Main {
  public static void main(String[] argv) throws Exception {
    Icon leafIcon = new ImageIcon("leaf.gif");
    Icon openIcon = new ImageIcon("open.gif");
    Icon closedIcon = new ImageIcon("closed.gif");
    JTree tree = new JTree();
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    renderer.setLeafIcon(leafIcon);
    renderer.setClosedIcon(closedIcon);
    renderer.setOpenIcon(openIcon);

    JFrame f = new JFrame();
    f.add(new JScrollPane(tree));
    f.setSize(300, 300);
    f.setVisible(true);
  }
}





Creating a Custom Renderer

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
class TreeNodeVector<E> extends Vector<E> {
  String name;
  TreeNodeVector(String name) {
    this.name = name;
  }
  TreeNodeVector(String name, E elements[]) {
    this.name = name;
    for (int i = 0, n = elements.length; i < n; i++) {
      add(elements[i]);
    }
  }
  public String toString() {
    return "[" + name + "]";
  }
}
class Employee {
  public String firstName;
  public String lastName;
  public float salary;
  public Employee(String f, String l, float s) {
    this.firstName = f;
    this.lastName = l;
    this.salary = s;
  }
}
class EmployeeCellRenderer implements TreeCellRenderer {
  JLabel firstNameLabel = new JLabel(" ");
  JLabel lastNameLabel = new JLabel(" ");
  JLabel salaryLabel = new JLabel(" ");
  JPanel renderer = new JPanel();
  DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
  Color backgroundSelectionColor;
  Color backgroundNonSelectionColor;
  public EmployeeCellRenderer() {
    firstNameLabel.setForeground(Color.BLUE);
    renderer.add(firstNameLabel);
    
    lastNameLabel.setForeground(Color.BLUE);
    renderer.add(lastNameLabel);
    
    salaryLabel.setHorizontalAlignment(JLabel.RIGHT);
    salaryLabel.setForeground(Color.RED);
    renderer.add(salaryLabel);
    renderer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    backgroundSelectionColor = defaultRenderer.getBackgroundSelectionColor();
    backgroundNonSelectionColor = defaultRenderer.getBackgroundNonSelectionColor();
  }
  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
      boolean expanded, boolean leaf, int row, boolean hasFocus) {
    Component returnValue = null;
    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
      Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
      if (userObject instanceof Employee) {
        Employee e = (Employee) userObject;
        firstNameLabel.setText(e.firstName);
        lastNameLabel.setText(e.lastName);
        salaryLabel.setText("" + e.salary);
        if (selected) {
          renderer.setBackground(backgroundSelectionColor);
        } else {
          renderer.setBackground(backgroundNonSelectionColor);
        }
        renderer.setEnabled(tree.isEnabled());
        returnValue = renderer;
      }
    }
    if (returnValue == null) {
      returnValue = defaultRenderer.getTreeCellRendererComponent(tree, value, selected, expanded,
          leaf, row, hasFocus);
    }
    return returnValue;
  }
}
public class EmployeeTree {
  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);
    frame.setVisible(true);
  }
}





Creating a DefaultCellEditor: JCheckBox

import java.awt.BorderLayout;
import java.util.Properties;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeCellEditor;
public class TreeEditJCheckBox {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    JTree tree = new JTree(props);
        
    
    JCheckBox checkBox = new JCheckBox("To be or not to be");
    TreeCellEditor editor = new DefaultCellEditor(checkBox);
    tree.setEditable(true);
    tree.setCellEditor(editor);
    
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





Creating a DefaultCellEditor: JComboBox

import java.awt.BorderLayout;
import java.util.Properties;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeCellEditor;
public class TreeEditJComboBox {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    JTree tree = new JTree(props);
        
    
    JComboBox comboBox = new JComboBox(new String[]{"A","B","C"});
    TreeCellEditor editor = new DefaultCellEditor(comboBox);
    tree.setEditable(true);
    tree.setCellEditor(editor);
    
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





Creating a DefaultCellEditor: JTextField

import java.awt.BorderLayout;
import java.util.Properties;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.TreeCellEditor;
public class TreeEditJTextField {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    JTree tree = new JTree(props);
        
    
    JTextField textField = new JTextField();
    TreeCellEditor editor = new DefaultCellEditor(textField);
    tree.setEditable(true);
    tree.setCellEditor(editor);
    
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





Creating an Editor Just for Leaf Nodes

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import java.util.Vector;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
class TreeNodeVector<E> extends Vector<E> {
  String name;
  TreeNodeVector(String name) {
    this.name = name;
  }
  TreeNodeVector(String name, E elements[]) {
    this.name = name;
    for (int i = 0, n = elements.length; i < n; i++) {
      add(elements[i]);
    }
  }
  public String toString() {
    return "[" + name + "]";
  }
}
class LeafCellEditor extends DefaultTreeCellEditor {
  public LeafCellEditor(JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor) {
    super(tree, renderer, editor);
  }
  public boolean isCellEditable(EventObject event) {
    boolean returnValue = super.isCellEditable(event);
    if (returnValue) {
      Object node = tree.getLastSelectedPathComponent();
      if ((node != null) && (node instanceof TreeNode)) {
        TreeNode treeNode = (TreeNode) node;
        returnValue = treeNode.isLeaf();
      }
    }
    return returnValue;
  }
}
class CheckBoxNode {
  String text;
  boolean selected;
  public CheckBoxNode(String text, boolean selected) {
    this.text = text;
    this.selected = selected;
  }
  public boolean isSelected() {
    return selected;
  }
  public void setSelected(boolean newValue) {
    selected = newValue;
  }
  public String getText() {
    return text;
  }
  public void setText(String newValue) {
    text = newValue;
  }
  public String toString() {
    return getClass().getName() + "[" + text + "/" + selected + "]";
  }
}
class CheckBoxNodeRenderer implements TreeCellRenderer {
  private JCheckBox leafRenderer = new JCheckBox();
  private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();
  protected JCheckBox getLeafRenderer() {
    return leafRenderer;
  }
  public CheckBoxNodeRenderer() {
    Font fontValue;
    fontValue = UIManager.getFont("Tree.font");
    if (fontValue != null) {
      leafRenderer.setFont(fontValue);
    }
    Boolean booleanValue = (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
    leafRenderer.setFocusPainted((booleanValue != null) && (booleanValue.booleanValue()));
  }
  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
      boolean expanded, boolean leaf, int row, boolean hasFocus) {
    Component returnValue;
    if (leaf) {
      String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, false);
      leafRenderer.setText(stringValue);
      leafRenderer.setSelected(false);
      leafRenderer.setEnabled(tree.isEnabled());
      if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
        Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
        if (userObject instanceof CheckBoxNode) {
          CheckBoxNode node = (CheckBoxNode) userObject;
          leafRenderer.setText(node.getText());
          leafRenderer.setSelected(node.isSelected());
        }
      }
      returnValue = leafRenderer;
    } else {
      returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree, value, selected, expanded,
          leaf, row, hasFocus);
    }
    return returnValue;
  }
}
class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {
  CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
  ChangeEvent changeEvent = null;
  JTree tree;
  public CheckBoxNodeEditor(JTree tree) {
    this.tree = tree;
  }
  public Object getCellEditorValue() {
    JCheckBox checkbox = renderer.getLeafRenderer();
    CheckBoxNode checkBoxNode = new CheckBoxNode(checkbox.getText(), checkbox.isSelected());
    return checkBoxNode;
  }
  public boolean isCellEditable(EventObject event) {
    boolean returnValue = false;
    if (event instanceof MouseEvent) {
      MouseEvent mouseEvent = (MouseEvent) event;
      TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
      if (path != null) {
        Object node = path.getLastPathComponent();
        if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
          DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
          Object userObject = treeNode.getUserObject();
          returnValue = ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
        }
      }
    }
    return returnValue;
  }
  public Component getTreeCellEditorComponent(JTree tree, Object value, boolean selected,
      boolean expanded, boolean leaf, int row) {
    Component editor = renderer.getTreeCellRendererComponent(tree, value, true, expanded, leaf,
        row, true);
    ItemListener itemListener = new ItemListener() {
      public void itemStateChanged(ItemEvent itemEvent) {
        if (stopCellEditing()) {
          fireEditingStopped();
        }
      }
    };
    if (editor instanceof JCheckBox) {
      ((JCheckBox) editor).addItemListener(itemListener);
    }
    return editor;
  }
}
public class CheckBoxNodeTreeSample {
  public static void main(String args[]) {
    JFrame frame = new JFrame("CheckBox Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    CheckBoxNode accessibilityOptions[] = { new CheckBoxNode("A", false),
        new CheckBoxNode("B", true) };
    CheckBoxNode browsingOptions[] = { new CheckBoxNode("C", true), new CheckBoxNode("D", true),
        new CheckBoxNode("E", true), new CheckBoxNode("F", false) };
    Vector<CheckBoxNode> accessVector = new TreeNodeVector<CheckBoxNode>("G", accessibilityOptions);
    Vector<CheckBoxNode> browseVector = new TreeNodeVector<CheckBoxNode>("H", browsingOptions);
    Object rootNodes[] = { accessVector, browseVector };
    Vector<Object> rootVector = new TreeNodeVector<Object>("Root", rootNodes);
    JTree tree = new JTree(rootVector);
    CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
    tree.setCellRenderer(renderer);
    tree.setCellEditor(new CheckBoxNodeEditor(tree));
    tree.setEditable(true);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





Creating a Proper ComboBox Editor for a Tree

import java.awt.BorderLayout;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
public class TreeEdit {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object array[] = {Boolean.TRUE, Boolean.FALSE, "Hello"};
    JTree tree = new JTree(array);
    tree.setEditable(true);
    tree.setRootVisible(true);
    
    
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
    String elements[] = { "A", "B", "C", "D"} ;
    JComboBox comboBox = new JComboBox(elements);
    comboBox.setEditable(true);
    TreeCellEditor comboEditor = new DefaultCellEditor(comboBox);
    TreeCellEditor editor = new DefaultTreeCellEditor(tree, renderer, comboEditor);
    tree.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





Editing Tree Nodes with triple clicking

import java.awt.BorderLayout;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
public class TreeEdit {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    JTree tree = new JTree(props);
    tree.setEditable(true);    
    
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





extends JLabel implements TreeCellRenderer

import java.awt.BorderLayout;
import java.awt.ruponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
public class TreeCellRendererImplementation extends JFrame {
  public TreeCellRendererImplementation() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("+");
    root.add(new DefaultMutableTreeNode(new Integer(3)));
    DefaultMutableTreeNode node = new DefaultMutableTreeNode("*");
    node.add(new DefaultMutableTreeNode("string"));
    node.add(new DefaultMutableTreeNode(new Short((short) 5)));
    root.add(node);
    TreeModel tm = new DefaultTreeModel(root);
    JTree tree = new JTree(tm);
    tree.setShowsRootHandles(true);
    tree.setCellRenderer(new MyRenderer());
    getContentPane().add(tree, BorderLayout.CENTER);
    setSize(400, 300);
    setVisible(true);
  }
  private class MyRenderer extends JLabel implements TreeCellRenderer {
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected,
        boolean isExpanded, boolean isLeaf, int row, boolean hasFocus) {
      Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
      setText(value.toString() + " [" + userObject.getClass().getName() + "]");
      return this;
    }
  }
  public static void main(String[] args) {
    TreeCellRendererImplementation m = new TreeCellRendererImplementation();
  }
}





Flush the internal cache of Row height

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
public class TreeRowHeightCache {
  public static void main(String[] argv) {
    JTree tree = new JTree();
    if (tree.getRowHeight() <= 0) {
      tree.setRowHeight(1);
    }
    tree.setRowHeight(0);
    JFrame frame = new JFrame("Image");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





Getting Default TreeCellRenderer From JTree

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
class TreeNodeVector<E> extends Vector<E> {
  String name;
  TreeNodeVector(String name) {
    this.name = name;
  }
  TreeNodeVector(String name, E elements[]) {
    this.name = name;
    for (int i = 0, n = elements.length; i < n; i++) {
      add(elements[i]);
    }
  }
  public String toString() {
    return "[" + name + "]";
  }
}
public class ChangingTreeCellRendererBackgroundForegroundColor {
  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);
    Object rootNodes[] = {v1, v2 };
    Vector<Object> rootVector = new TreeNodeVector<Object>("Root", rootNodes);
    JTree tree = new JTree(rootVector);
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
    Color backgroundSelection = renderer.getBackgroundSelectionColor();
    renderer.setBackgroundSelectionColor(renderer.getBackgroundNonSelectionColor());
    renderer.setBackgroundNonSelectionColor(backgroundSelection);
    Color textSelection = renderer.getTextSelectionColor();
    renderer.setTextSelectionColor(renderer.getTextNonSelectionColor());
    renderer.setTextNonSelectionColor(textSelection);
    
    frame.add(new JScrollPane(tree), BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setVisible(true);
    
  }
}





Rendering an image in a JTable column

import java.awt.BorderLayout;
import java.awt.ruponent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(new BorderLayout());
    MyTableModel model = new MyTableModel();
    JTable table = new JTable(model);
    table.setRowHeight(80);
    table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
    JScrollPane pane = new JScrollPane(table);
    frame.getContentPane().add(BorderLayout.CENTER, pane);
    frame.setSize(500, 400);
    frame.setVisible(true);
  }
}
class MyTableModel extends AbstractTableModel {
  public Object getValueAt(int row, int column) {
    return "" + (row * column);
  }
  public int getColumnCount() {
    return 4;
  }
  public int getRowCount() {
    return 5;
  }
}
class ImageRenderer extends DefaultTableCellRenderer {
  JLabel lbl = new JLabel();
  ImageIcon icon = new ImageIcon(getClass().getResource("sample.png"));
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    lbl.setText((String) value);
    lbl.setIcon(icon);
    return lbl;
  }
}





Setting the Activation Click Count for a Table Cell Editor in a JTable Component

import java.awt.ruponent;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTable table = new JTable();
    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellEditor(new MyTableCellEditor());
  }
}
class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
  public boolean stopCellEditing() {
    String s = (String) getCellEditorValue();
    return super.stopCellEditing();
  }
  JTextField field = new JTextField();
  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
   
    return field;
  }
  
  public boolean isCellEditable(EventObject evt) {
      if (evt instanceof MouseEvent) {
          int clickCount;
          clickCount = 2;
          return ((MouseEvent)evt).getClickCount() >= clickCount;
      }
      return true;
  }
  public Object getCellEditorValue() {
    return null;
  }
}





Use UIManager to change the default icon for JTree

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
public class Main {
  public static void main(String[] argv) throws Exception {
    Icon leafIcon = new ImageIcon("leaf.gif");
    Icon openIcon = new ImageIcon("open.gif");
    Icon closedIcon = new ImageIcon("closed.gif");
    UIManager.put("Tree.leafIcon", leafIcon);
    UIManager.put("Tree.openIcon", openIcon);
    UIManager.put("Tree.closedIcon", closedIcon);
    
    JTree tree = new JTree();
    JFrame f = new JFrame();
    f.add(new JScrollPane(tree));
    f.setSize(300, 300);
    f.setVisible(true);
  }
}





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();
  }
}





Working with Tree Tooltips: using a Tooltip Cell Renderer

import java.awt.BorderLayout;
import java.awt.ruponent;
import java.util.Dictionary;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
class ToolTipTreeCellRenderer implements TreeCellRenderer {
  DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
  Dictionary tipTable;
  public ToolTipTreeCellRenderer(Dictionary tipTable) {
    this.tipTable = tipTable;
  }
  public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
      boolean expanded, boolean leaf, int row, boolean hasFocus) {
    renderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
    if (value != null) {
      Object tipKey;
      if (value instanceof DefaultMutableTreeNode) {
        tipKey = ((DefaultMutableTreeNode) value).getUserObject();
      } else {
        tipKey = tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
      }
      renderer.setToolTipText((String) tipTable.get(tipKey));
    }
    return renderer;
  }
}
public class TreeTips {
  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);
    frame.setVisible(true);
  }
}