Code
tree.setCellEditor(new DefaultTreeCellEditor(
tree, (DefaultTreeCellRenderer) tree.getCellRenderer()) {
@Override public boolean isCellEditable(EventObject e) {
return !(e instanceof MouseEvent) && super.isCellEditable(e);
}
// @Override protected boolean canEditImmediately(EventObject e) {
// // ((MouseEvent) e).getClickCount() > 2
// return (e instanceof MouseEvent) ? false : super.canEditImmediately(e);
// }
});
tree.setEditable(true);
tree.setComponentPopupMenu(new TreePopupMenu());
// ...
public TreePopupMenu() {
super();
add(new JMenuItem(new AbstractAction("Edit") {
@Override public void actionPerformed(ActionEvent e) {
JTree tree = (JTree) getInvoker();
if (path != null) {
tree.startEditingAtPath(path);
}
}
}));
add(new JMenuItem(new AbstractAction("Edit Dialog") {
@Override public void actionPerformed(ActionEvent e) {
JTree tree = (JTree) getInvoker();
if (path == null) {
return;
}
Object node = path.getLastPathComponent();
if (node instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode leaf = (DefaultMutableTreeNode) node;
textField.setText(leaf.getUserObject().toString());
int result = JOptionPane.showConfirmDialog(
tree, textField, "Rename",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String str = textField.getText();
if (!str.trim().isEmpty()) {
((DefaultTreeModel) tree.getModel()).valueForPathChanged(path, str);
}
}
}
}
}));
// ...
References
No comments:
Post a Comment