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

Painting problem in my TreeCellRenderer

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • 1.2.0
    • client-libs

      I created my own tree cell renderer. There is a painting problem in the following code.

      When you run this, you will find that JLabel bottom is not seen in the tree. If you look at carefully, tree-node bullet and JLabel is not aligned. JLabel is below than the node bullet. This might be overlooked but this problem is not tiny when you override paint() method in MyTreeCellRenderer class.

      Uncomment paint() method and run again. You will see JLabel's text is printed twice in different location. super.paint(g) and alabel.paint(g) paints differently. If I comment out only super.paint(g), then no duplicated text and correct alignment it's perfect.
       

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

      class MyTreeCellRenderer extends JPanel implements TreeCellRenderer
      {
          JLabel alabel = new JLabel("a label");

          public MyTreeCellRenderer(){
              super(false);
              setLayout(new FlowLayout());
              setBackground(Color.white);

              add(alabel);
          }

          /*
          public void paint(Graphics g){
              super.paint(g);
              alabel.paint(g);
          }
          */

          public Component getTreeCellRendererComponent(JTree tree,
                                                        Object value,
                                                        boolean selected,
                                                        boolean expanded,
                                                        boolean leaf,
                                                        int row,
                                                        boolean hasFocus){

              DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
              alabel.setText((String)(node.getUserObject()));

              return this;
          }
      }

      public class PaintTest extends JFrame
      {
          JTree tree = null;
          MyTreeCellRenderer renderer = null;

          public PaintTest(){
              DefaultMutableTreeNode top = new DefaultMutableTreeNode("TOP");
              DefaultMutableTreeNode catagory;
              DefaultMutableTreeNode composer;
              DefaultMutableTreeNode style;

              // Classical
              catagory = new DefaultMutableTreeNode("Classical");
              top.add(catagory);

      // Beethoven
      catagory.add(composer = new DefaultMutableTreeNode("Beethoven"));
      composer.add(style = new DefaultMutableTreeNode("Concertos"));
      style.add(new DefaultMutableTreeNode("No. 1 - C Major"));
      style.add(new DefaultMutableTreeNode("No. 2 - B-Flat Major"));

      composer.add(style = new DefaultMutableTreeNode("Quartets"));
      style.add(new DefaultMutableTreeNode("Six String Quartets"));
      style.add(new DefaultMutableTreeNode("Three String Quartets"));
      style.add(new DefaultMutableTreeNode("Grosse Fugue for String Quartets"));

      // Mozart
      catagory.add(composer = new DefaultMutableTreeNode("Mozart"));
      composer.add(style = new DefaultMutableTreeNode("Concertos"));
      style.add(new DefaultMutableTreeNode("Piano Concerto No. 12"));
      style.add(new DefaultMutableTreeNode("Piano Concerto No. 17"));


              renderer = new MyTreeCellRenderer();
              tree = new JTree(top);
              tree.setCellRenderer(renderer);

              getContentPane().add(tree);
              setSize(300,300);
              show();
          }


          public static void main(String[] args){
              new PaintTest();
          }
      }

            stewilso Steve Wilson (Inactive)
            bkimsunw Bae-chul Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: