extends DefaultTreeCellEditor to create Tree Leaf editor : Tree Renderer Editor « Swing JFC « Java






extends DefaultTreeCellEditor to create Tree Leaf editor

extends DefaultTreeCellEditor to create Tree Leaf editor
  
import java.awt.BorderLayout;
import java.util.EventObject;

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;
import javax.swing.tree.TreeNode;

public class EditLeafSample {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Editable Tree");
    JTree tree = new JTree();
    tree.setEditable(true);

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree
        .getCellRenderer();
    TreeCellEditor editor = new LeafCellEditor(tree, renderer);
    tree.setCellEditor(editor);

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

class LeafCellEditor extends DefaultTreeCellEditor {

  public LeafCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
    super(tree, renderer);
  }

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

           
         
    
  








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.Tree Changed RendererTree Changed Renderer
5.Build a tree and populate it with custom renderers and editorsBuild a tree and populate it with custom renderers and editors
6.Using DefaultTreeCellRenderer
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.