Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4108836

Swing JTree node not resized with text changes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.2.0
    • client-libs

      The node renderer is not automatically resized when the caption text of an item of the tree changes. If your create a node and change its caption into a longer caption, you won't be able to see the whole caption until you destroy and create the node again. This occurs on Solaris with JDK1.1.5(k) and swing-pre-1.0 (Jan 30).

      The following example creates a node with an empty caption "", then changes the
      text afterwards.

      import com.sun.java.swing.* ;
      import com.sun.java.swing.tree.* ;
      import java.awt.* ;
      import java.awt.event.* ;


      public class TreeTest extends JFrame implements ActionListener
      {
        JButton addButton1 ;
        JButton addButton2 ;
        DefaultMutableTreeNode root ;
        DefaultTreeModel treeModel ;


        public static void main(String[] args)
        {
          TreeTest panel = new TreeTest() ;
          panel.setVisible(true) ;
        }

        public TreeTest()
        {
          super("test") ;
          getContentPane().setLayout(null) ;
          setBounds(0,0,600,400) ;

          root = new DefaultMutableTreeNode("root") ;
          treeModel = new DefaultTreeModel(root) ;
          JTree tree = new JTree(treeModel) ;
          tree.setBounds(10,100,300,200) ;
          getContentPane().add(tree) ;

          addButton1 = new JButton("add1") ;
          addButton1.setBounds(0,30,100,25) ;
          addButton1.addActionListener(this) ;
          getContentPane().add(addButton1) ;

          addButton2 = new JButton("add2") ;
          addButton2.setBounds(110,30,100,25) ;
          addButton2.addActionListener(this) ;
          getContentPane().add(addButton2) ;

        }

        public void actionPerformed(ActionEvent e)
        {
          if (e.getSource()==addButton1)
          {
            TreeData treeData = new TreeData("abcdefghij") ;
            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(treeData) ;
            treeModel.insertNodeInto(newNode,root,0) ;
            treeData.setCaption("aaaa") ;
          }
          else
          if (e.getSource()==addButton2)
          {
            TreeData treeData = new TreeData("") ;
            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(treeData) ;
            treeModel.insertNodeInto(newNode,root,0) ;
            treeData.setCaption("bbbb") ;
          }
        }
      }


      class TreeData
      {
        String caption ;
        public TreeData(String caption)
        {
          this.caption = caption ;
        }

        public void setCaption(String caption)
        {
          this.caption = caption ;
        }

        public String toString()
        {
          return caption ;
        }
      }

      This bug was sent into swing-feedback by:
      Yannick Saillet, Software Engineer,
      IBM Deutschland, Education&Training (Germany)
      E-mail: ###@###.###
      or: ###@###.###

            tsantossunw Tom Santos (Inactive)
            nschorrsunw Nancy Schorr (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: