Any component as leaf for JTree problems

hi,
I'm trying to get arbitary components as leaves in a JTree and it looks like its very nearly there, but there are two (related?) problems.
1) If you click around on the top 3 tree components then it gets stuck in a loop of rendering and causes a stack exception to be thrown.
2) For the embedded JTree component, expanding its nodes doesn't update the parent tree
both may be because current the row height is set at the wrong time (in the TreeCellRenderer)
any help would be really appreciated!
thanks,
asjf
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.util.*;
public class ComponentTree
     public static void main(String args[])
          try{(new UIManager()).setLookAndFeel((new UIManager()).getSystemLookAndFeelClassName());}
          catch(Exception e){}
          DefaultMutableTreeNode c0 = new DefaultMutableTreeNode(new JButton("root"));
          DefaultMutableTreeNode c1 = new DefaultMutableTreeNode(new JButton("c1"));
          DefaultMutableTreeNode c2 = new DefaultMutableTreeNode(new JList(new Object [] {"A","B","C"}));
          DefaultMutableTreeNode c3 = new DefaultMutableTreeNode(new JTable(new Object [][] {{"Active","true"},{"User","root"}}, new Object [] {"Name","Value"}));
          DefaultMutableTreeNode c4 = new DefaultMutableTreeNode(new JTree());
          DefaultMutableTreeNode c5 = new DefaultMutableTreeNode(new JList(new Object [] {"D","E","F"}));
          DefaultMutableTreeNode c6 = new DefaultMutableTreeNode(new JCheckBox("Active",true));
          DefaultTreeModel dtm = new DefaultTreeModel(c0);
               dtm.insertNodeInto(c6,c0,0);
               dtm.insertNodeInto(c1,c0,0);
               dtm.insertNodeInto(c2,c0,0);
               dtm.insertNodeInto(c5,c2,0);
               dtm.insertNodeInto(c3,c1,0);
               dtm.insertNodeInto(c4,c3,0);
          JTree tree = new JTree(dtm);
               tree.setEditable(true);
               tree.setCellRenderer(new DefaultTreeCellRenderer()
                    public Component getTreeCellRendererComponent(     JTree tree,     Object value, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus)
                         System.out.println("Render row "+row);
                         Component c = (Component) ((DefaultMutableTreeNode) value).getUserObject();
                         if(c instanceof JCheckBox)
                              c.setBackground(UIManager.getColor("Tree.textBackground"));
                         tree.setRowHeight(c.getHeight());                    
                         return c;     
               tree.setCellEditor(new DefaultTreeCellEditor(tree,(DefaultTreeCellRenderer)tree.getCellRenderer())
                    public boolean isCellEditable(EventObject evt){return true;}
                    public Component getTreeCellEditorComponent(     JTree tree,     Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
                         return (Component) ((DefaultMutableTreeNode) value).getUserObject();
          JFrame frame = new JFrame("ComponentTree");
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.getContentPane().add(tree);     
               frame.pack();
               frame.show();
}

thanks!
this seems an awful lot of code to get this working - it should be possible to have a single CellEditor/Renderer??
also running the code - its very hard to select checkbox items, and the JLists seem to disable themselves when the main tree updates?
its also not general to any Component (?) (although we probably only want JComponent) so we can't add a JTree as a leaf?
I'm guessing there is a simpler way to get this working?

Similar Messages

Maybe you are looking for

  • J2EE cannot start  ?

    Hi, Recently, my j2ee enginee cannot start dispacher and j2ee server always in yellow colour (Starting framework forever). This is the error i got from server.log : Thread[SAPEngine_EnquReader,5,main]##0#0#Error#1#com.sap.engine.core.locking.impl3.Lo

  • Acer Aspire One will only connect via Fon now on H...

    Since activating Fon on my BT Home Hub my Acer Aspire One - running Linux Linpus - will no longer log on to the 'normal' wireless connection on my hub, only Fon, which has limited bandwidth, etc. Anybody know why? Have a solution? I've 'proved' it's

  • Transfer a message of high dimension from client to client

    What i must use to di it? Ftp? SocketChannel? Thanks.

  • Opening older InCopy files

    I am trying to run InCopy CS3 on a 2.66 gHz Intel iMac, 4 GB RAM, 320 GB HD, and InCopy is crashing when opening certain older files, such as those created in CS. (We need to be able to open archived files that were created in earlier versions.) Ther

  • E63 cant sign into MSN Messenger out of the blue

    I had the phone for almost a year, and out of the blue, I cannot sign in to my msn account. This is the message I got windows live is currently unable to connect. this may be because your phone is in the offline mode or you do not have a data connect