Creating an Editor Just for Leaf Nodes : JTree Editor Renderer « Swing « Java Tutorial






Creating an Editor Just for Leaf Nodes
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
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);
  }
}








14.69.JTree Editor Renderer
14.69.1.Getting Default TreeCellRenderer From JTreeGetting Default TreeCellRenderer From JTree
14.69.2.extends JLabel implements TreeCellRendererextends JLabel implements TreeCellRenderer
14.69.3.Creating a Custom RendererCreating a Custom Renderer
14.69.4.Working with Tree Tooltips: using a Tooltip Cell RendererWorking with Tree Tooltips: using a Tooltip Cell Renderer
14.69.5.Editing Tree Nodes with triple clickingEditing Tree Nodes with triple clicking
14.69.6.Creating a DefaultCellEditor: JTextFieldCreating a DefaultCellEditor: JTextField
14.69.7.Creating a DefaultCellEditor: JComboBoxCreating a DefaultCellEditor: JComboBox
14.69.8.Creating a DefaultCellEditor: JCheckBoxCreating a DefaultCellEditor: JCheckBox
14.69.9.Creating a Proper ComboBox Editor for a TreeCreating a Proper ComboBox Editor for a Tree
14.69.10.Creating an Editor Just for Leaf NodesCreating an Editor Just for Leaf Nodes
14.69.11.Rendering an image in a JTable column
14.69.12.Setting the Activation Click Count for a Table Cell Editor in a JTable Component
14.69.13.Using DefaultTreeCellRenderer
14.69.14.Changing and Removing the Default Icons in a JTree Component
14.69.15.Use UIManager to change the default icon for JTree
14.69.16.Flush the internal cache of Row height