import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.EventObject;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.CellEditorListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellEditor;
public class TreeEditorRendererDemo extends JFrame {
public TreeEditorRendererDemo() {
String[] values = new String[] {"Bier", "Limo", "Wein", "Wasser"};
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Getränke");
root.add(new DefaultMutableTreeNode(values[0]));
root.add(new DefaultMutableTreeNode(values[0]));
root.add(new DefaultMutableTreeNode(values[0]));
DefaultTreeModel model = new DefaultTreeModel(root);
JTree tree = new JTree(model);
tree.setCellRenderer(new ComboRenderer(new NodeComponent(values)));
tree.setCellEditor(new ComboEditor(tree, new NodeComponent(values)));
tree.setEditable(true);
this.getContentPane().add(tree);
}
class NodeComponent extends JPanel {
private JComboBox box;
public NodeComponent(String[] values) {
this.setLayout(new GridLayout(1, 2));
add(new JLabel("Auswahl: "));
add(box = new JComboBox(values));
box.setBackground(Color.WHITE);
}
public void setSelectedItem(Object obj) {
box.setSelectedItem(obj);
}
public Object getCurrentObject() {
return box.getSelectedItem();
}
public void addItemListener(ItemListener listener) {
box.addItemListener(listener);
}
}
class ComboRenderer extends DefaultTreeCellRenderer {
private NodeComponent nodePanel;
public ComboRenderer(NodeComponent comp) {
nodePanel = comp;
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
if (treeNode.isLeaf()) {
nodePanel.setSelectedItem(treeNode.getUserObject());
nodePanel.setBackground(super.getBackground());
return nodePanel;
}
this.setIcon(null);
return this;
}
}
class ComboEditor implements TreeCellEditor, ItemListener {
private JTree tree;
private NodeComponent nodePanel;
public ComboEditor(JTree tree, NodeComponent comp) {
this.tree = tree;
this.nodePanel = comp;
this.nodePanel.addItemListener(this);
}
public void itemStateChanged(ItemEvent e) {
tree.stopEditing();
}
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean isSelected, boolean expanded, boolean leaf, int row) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
if (treeNode.isLeaf()) {
nodePanel.setSelectedItem(treeNode.getUserObject());
return nodePanel;
}
return tree.getCellRenderer().getTreeCellRendererComponent(tree, value, true, true, false, row, true);
}
public Object getCellEditorValue() {
return nodePanel.getCurrentObject();
}
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public void addCellEditorListener(CellEditorListener l) {}
public void cancelCellEditing() {}
public void removeCellEditorListener(CellEditorListener l) {}
public boolean shouldSelectCell(EventObject anEvent) {return false;}
public boolean stopCellEditing() {return true;}
}
public static void main(String[] args) {
JFrame frame = new TreeEditorRendererDemo();
frame.setBounds(0, 0, 500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}