Java禁用JTree/TransferHandler的剪切操作

Java禁用JTree/TransferHandler的剪切操作,java,export,jtree,transfer,cut,Java,Export,Jtree,Transfer,Cut,我已经为我的JTree创建了一个自定义TransferHandler,因此通过在canImport中选中support.isDrop仅支持移动和粘贴来禁用复制,但我不知道如何禁用剪切操作 看起来我必须用exportDone的方法来做决定,但到目前为止运气不好。到目前为止,我的方法看起来是这样的,但拖动和剪切都与移动操作相关联 protected void exportDone(JComponent source, Transferable data, int action) { if(a

我已经为我的JTree创建了一个自定义TransferHandler,因此通过在canImport中选中support.isDrop仅支持移动和粘贴来禁用复制,但我不知道如何禁用剪切操作

看起来我必须用exportDone的方法来做决定,但到目前为止运气不好。到目前为止,我的方法看起来是这样的,但拖动和剪切都与移动操作相关联

protected void exportDone(JComponent source, Transferable data, int action) {
    if(action == TransferHandler.MOVE) {
        try {
            List<TreePath> list = ((TestTreeList) data.getTransferData(TestTreeList.testTreeListFlavor)).getNodes();

            int count = list.size();
            for(int i = 0; i < count; i++) {
                TestTreeNode        node    = (TestTreeNode) list.get(i).getLastPathComponent();
                DefaultTreeModel    model   = (DefaultTreeModel) tree.getModel();
                model.removeNodeFromParent(node);
            }
            tree.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        } catch (UnsupportedFlavorException e) {
            Log.logException(e);
        } catch (IOException e) {
            Log.logException(e);
        }
    }
}

您还可以通过从ActionMap中删除操作来禁用用户界面中的剪切、复制和粘贴

JTree tree = new JTree();
...
tree.getActionMap().put( "cut", null );
tree.getActionMap().put( "copy", null );
tree.getActionMap().put( "paste", null );

这会阻止有人使用此树作为源进行复制、剪切或粘贴。

JTree WHEN_FOCUSED InputMap,但不是第一代:InputMap可以有父祖父母、曾祖父母等。InputMap

tree.getInputMap( JComponent.WHEN_FOCUSED ).getParent().remove( KeyStroke.getKeyStroke( KeyEvent.VK_X, KE.CTRL_DOWN_MASK ) )
注意:为了避免太多的麻烦,您可能还想知道,在不同类型的InputMap之间有一个层次结构或更准确的协商顺序:首先协商以_为中心,然后协商以_为中心,最后在以_为中心的窗口中协商以_为中心。如果您在JComponent的WHEN_祖先输入映射中放置Ctrl-X,可能希望它会覆盖已经存在的内容,那么如果在同一JComponent的WHEN_焦点输入映射中存在Ctrl-X,则这将被掩盖

通过制作一个简单的方法来探索给定组件的所有层次结构,至少显示层次结构中的所有键绑定,可以获得很多启示:在给定窗口中显示所有WHEN_in_FOCUSED_窗口按键稍微复杂一些

我是一个Jython用户,但这应该是可以理解的:可以用Java编写一个类似但不可避免地不那么优雅的实用程序

def show_ancestor_comps( comp, method ):
    height = 0
    while comp:
        if method:
            # this method can return True if it wants the ancestor exploration to stop
            if method( comp, height ):
                return
        height = height + 1
        comp = comp.parent

''' NB this can be combined with the previous one: show_ancestor_comps( comp, show_all_inputmap_gens_key_value_pairs ):
gives you all the InputMaps in the entire Window/Frame, etc. ''' 
def show_all_inputmap_gens_key_value_pairs( component, height ):
    height_indent = '  ' * height
    if not isinstance( component, javax.swing.JComponent ):
        logger.info( '%s# %s not a JComponent... no InputMaps' % ( height_indent, type( component ), ))
        return
    logger.info( '%s# InputMap stuff for component of type %s' % ( height_indent, type( component ), ))
    map_types = [ 'when focused', 'ancestor of focused', 'in focused window' ]
    for i in range( 3 ):
        im = component.getInputMap( i )
        logger.info( '%s# focus type %s' % ( height_indent, map_types[ i ], ))
        generation = 1
        while im: 
            gen_indent = '  ' * generation
            logger.info( '%s%s# generation %d InputMap %s' % ( height_indent, gen_indent, generation, im, )) 
            if im.keys():
                for keystroke in im.keys():
                    logger.info( '%s%s# keystroke %s value %s' % ( height_indent, gen_indent + '  ', keystroke, im.get( keystroke )))
            im = im.parent
            generation += 1

谢谢你的回复,但这似乎不适合我。我已经在原始树以及TransferHandler存储的引用中完成了这项工作。我还考虑过将删除节点的操作与插入一起移动到importData方法,但是在同一个方法中处理这两个操作是很困难的。这将消除“cut”删除exportDone方法中的节点的任何机会。对于堆栈溢出,请给出一些解释。
    ActionMap actionMap = tree.getActionMap();
    actionMap.put( "cut", null );
    actionMap.put( "copy", null );
    actionMap.put( "paste", null );

    actionMap.getParent().put( "cut", null );
    actionMap.getParent().put( "copy", null );
    actionMap.getParent().put( "paste", null );