Search code examples
javaswingjtree

Where to go with my JTree now. Custom UserObjects or another way to achieve what is needed?


I have a jTree which I populate the nodes from a toString method of List of objects. I have about 10 different kind of objects in that List.

I'm having problems with changing the node name. Normally, when I need to reach an object of a node, I search for the node.toString() in the List to find out where is the object located in the list, so I can do myList.get(i). But when I change the node name, it won't find anything in the List because it doesn't update the objects name. And I can't find a way to save the old name prior to changing node name.

I also tried using my objects as UserObject of the node but when I edit the name, it converts the UserObject back to String

How should I continue?


Solution

  • Thanks to everyone, I managed to tackle this (hopefully) last problem with this particular JTree. Here is how I did it from THIS example:

      private static class MyTreeCellEditor extends DefaultTreeCellEditor {
    
            public MyTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
                super(tree, renderer);
            }
            Text newText;
            Real newReal;
            Size newSize;
            Integer newInt;
            Boolean newBool;
            Table newTable;
    
            @Override
            public Component getTreeCellEditorComponent(JTree jtree, Object o, boolean bln, boolean bln1, boolean bln2, int i) {
                return super.getTreeCellEditorComponent(jtree, o, bln, bln1, bln2, i); //To change body of generated methods, choose Tools | Templates.
            }
    
        @Override
        public Object getCellEditorValue() {
            String value = (String) super.getCellEditorValue();
            DefaultMutableTreeNode testNode = (DefaultMutableTreeNode) super.tree.getLastSelectedPathComponent();
    
            Object objToChange = testNode.getUserObject();
    
            while (objToChange instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) objToChange;
                objToChange = parentNode.getUserObject();
            }
    
            if (testNode.getChildCount() > 0) {
                value += " (" + testNode.getChildCount() + ")";
            }
    
            if (objToChange instanceof Text) {
                newText = (Text) objToChange;
                newText.setName(value);
                return newText;
            } else if (objToChange instanceof Real) {
                newReal = (Real) objToChange;
                newReal.setName(value);
                return newReal;
            } else if (objToChange instanceof Size) {
                newSize = (Size) objToChange;
                newSize.setName(value);
                return newSize;
            } else if (objToChange instanceof Integer) {
                newInt = (Integer) objToChange;
                newInt.setName(value);
                return newInt;
            } else if (objToChange instanceof Boolean) {
                newBool = (Boolean) objToChange;
                newBool.setName(value);
                return newBool;
            } else if (objToChange instanceof Table) {
                newTable = (Table) objToChange;
                newTable.setName(value);
                return newTable;
            } else {
                return new Text("unexpected object 02");
            }
        }
    

    Text, Real, Size etc. are my custom objects that populate the nodes.

    Edit: Just noticed that Object objToChange = testNode.getUserObject(); might return a TreeNode. So I've put a while loop for that.