Disabling 3-Click Edit of a JTree node but keeping keyoard keys to edit for saving the old name of the node prior to edit

≡放荡痞女 提交于 2019-12-24 03:01:05

问题


I want to be able to edit the nodename with F2 and ENTER keys only, not with mouse. I added these 2 lines and they are working:

jTree1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing");
jTree1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "startEditing");

But how do I disable editing from mouse? Are there any methods like: jTree1.setToggleClickCount(2);

The reason I want to do this is that I want to keep the old name of the node, so I'll create a keylistener to listen for F2 and ENTER and keep the names that way. Does that make sense? Any thoughts?


回答1:


You can disable editing with mouse with help of TreeCellEditor, try next code:

DefaultTreeCellEditor editor = new DefaultTreeCellEditor(t, (DefaultTreeCellRenderer) t.getCellRenderer()){
    @Override
    public boolean isCellEditable(EventObject event) {
        if(event instanceof MouseEvent){
            return false;
        }
        return super.isCellEditable(event);
    }
};

that editor prevents editing with MouseEvent.

Set that editor to your JTree with next line: tree.setCellEditor(editor);

where tree is your JTree.



来源:https://stackoverflow.com/questions/21138451/disabling-3-click-edit-of-a-jtree-node-but-keeping-keyoard-keys-to-edit-for-savi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!