简单快速的JTree Cell Editor [英] easy and fast JTree Cell Editor

查看:128
本文介绍了简单快速的JTree Cell Editor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义TreeModel和自定义TreeRenderer的JTree。树模型包含一堆不同类型的对象。
其中一种类型的显示方式与其他类型不同:显示的文本是对象的两个字段的串联。当我编辑单元格时,我想用编辑的文本更新其中一个字段。到目前为止,我的工作得很好。

I have a JTree with a custom TreeModel and a custom TreeRenderer. The Tree Model contains a bunch of objects of different types. One of these types is displayed differently than the others: The displayed text is a concatenation of two fields of the object. When i edit the cell, I want to update one of these fields with the edited text. So far i got it working pretty well.

我的问题:当编辑时显示的文本是
2个字段的完整连接值时,即使你在事实上只是编辑其中一个字段。因此,我想在用户开始编辑时仅显示您正在编辑的一个字段的内容。

My Problem: It is confusing when the text, which is displayed while editing, is the complete concatenated value of 2 fields, even though you're in fact just editing one of the fields. So i want to display only the content of the one field you're editing when the user starts editing.

我尝试使用自定义CellEditor执行此操作,我看到了它的工作方式和方法在我的情况下似乎有点过分。我只是想在很多情况下改变显示的文本,所以我自然而然地希望实现它,而不是整个CellEditor用于我的Tree的整个内容。

I tried to do this with a custom CellEditor, and I saw the way it's supposed to work and the approach seems overkill in my case. I just want to alter the displayed text in one of many cases, so naturally I want to implement that, and not a whole CellEditor for the entire content of my Tree.

是否有更快更简单的方法,或者我必须使用自定义编辑器?

Is there a faster and easier way to do this, or do I have to use a custom editor?

谢谢

推荐答案

自定义编辑器没有办法,它是最简单的解决方案:-)另外,你需要在 data 领域中有一些方法,可以适当地解释编辑值并更新自己,fi一个自定义节点。

There is no way around a custom editor and it is the shortest solution possible :-) Additionally, you'll need some means in the data realm that can interpret the editing value as appropriate and update itself, f.i. a custom node.

Fi(稍后会发表评论,我这台机器上的蹩脚firefox正在把我推到墙上)

F.i (will comment it later, my lame firefox on this machine is driving me up the walls)

/**
 * Basic code stolen from @trashgod at
* @see http://stackoverflow.com/a/11113648/230513
*/
public class TreeEditDemo extends JPanel {

    private JTree tree;
    private DefaultMutableTreeNode root;
    private DefaultTreeCellEditor editor;

    public TreeEditDemo() {
        super.setLayout(new GridLayout());
        root = new DefaultMutableTreeNode("Nodes");
        root.add(new MyResourceNode(new Resource("one", "first")));
        root.add(new MyResourceNode(new Resource("two", "first")));
        tree = new JTree(root);
        tree.setEditable(true);
        editor = new MyTreeCellEditor(tree,
            (DefaultTreeCellRenderer) tree.getCellRenderer());
        tree.setCellEditor(editor);
        this.add(new JScrollPane(tree));
    }

    private static class MyTreeCellEditor extends DefaultTreeCellEditor {

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

        @Override
        public Component getTreeCellEditorComponent(JTree tree, Object value,
                boolean isSelected, boolean expanded, boolean leaf, int row) {
            if (value instanceof MyResourceNode) {
                value = ((MyResourceNode) value).getName();
            }
            return super.getTreeCellEditorComponent(tree, value, isSelected, expanded,
                    leaf, row);
        }

        @Override
        public boolean isCellEditable(EventObject e) {
            return super.isCellEditable(e)
                && ((TreeNode) lastPath.getLastPathComponent()).isLeaf();
        }
    }

    public static class MyResourceNode extends DefaultMutableTreeNode {

        /**
         * @param resource
         */
        public MyResourceNode(Resource resource) {
            super(resource);
        }

        @Override
        public void setUserObject(Object userObject) {
            if (userObject instanceof String) {
                setName((String) userObject);
            } else if (userObject instanceof Resource) {
                super.setUserObject(userObject);
            }    
        }

        public void setName(String name) {
            if (getUserObject() != null) {
                getUserObject().setName(name);
            }
        }

        public String getName() {
            if (getUserObject() != null) {
                return getUserObject().getName();
            }
            return null;
        }

        @Override
        public Resource getUserObject() {
            return (Resource) super.getUserObject();
        }


    }
    private static class Resource {

        String name;
        private String category;

        public Resource(String name, String category) {
            this.name = name;
            this.category = category;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
        @Override
        public String toString() {
            // BEWARE: don't do this in production code!
            return name + " (" + category + ")";
        }
    }

    private void display() {
        JFrame f = new JFrame("TreeEditorDemo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TreeEditDemo().display();
            }
        });
    }
}

这篇关于简单快速的JTree Cell Editor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆