JTree Custom Renderer, Tree Nodes not shown properly.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlarV
    New Member
    • Dec 2008
    • 37

    JTree Custom Renderer, Tree Nodes not shown properly.

    Hello everyone!

    I created a custom JTree Renderer for my tree because I wanted it to have an icon near each node.
    Here is a sample of my code for the custom renderer.

    Code:
    public TreeRenderer()  {
        renderer = new JPanel();
        renderer.setLayout(new BoxLayout(renderer, BoxLayout.X_AXIS));
        titleLabel = new JLabel(" ");
        titleLabel.setForeground(Color.black);
        titleLabel.setFont(new Font("Calibri", Font.PLAIN, 24));
        renderer.add(titleLabel);
        buttonLabel = new JLabel(" ");   
        renderer.add(buttonLabel);
      }
    and then I Override the getTreeCellRend ererComponent(s mall sample just to see the structure of my code) :
    Code:
    @Override
      public Component getTreeCellRendererComponent(JTree tree, Object value,
          boolean selected, boolean expanded, boolean leaf, int row,
          boolean hasFocus) {
        Component returnValue = null;
        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {     
            Object userObject = ((DefaultMutableTreeNode) value)
              .getUserObject();
          if (userObject instanceof PlugwiseNode) {  
            PlugwiseNode node = (PlugwiseNode) userObject;
            if(!node.title.equals("Home Lights"))
                titleLabel.setIcon(createImageIcon("http://bytes.com/images/plugwisenode.png",""));//oti icon 8elw to vazw edw
            else
                titleLabel.setIcon(createImageIcon("http://bytes.com/images/office lights.png",""));
            titleLabel.setText(node.title+ "   ");
            buttonLabel.setIcon(createImageIcon("http://bytes.com/images/jtree green.png",""));
            if (selected) {
              renderer.setOpaque(true);
              renderer.setBackground(backgroundSelectionColor);
            } else {
              renderer.setOpaque(false);
            }
            renderer.setEnabled(tree.isEnabled());
            returnValue = renderer;
          }
        }
        if (returnValue == null) {
          defaultRenderer.getTreeCellRendererComponent(tree,
          value, selected, expanded, leaf, row, hasFocus);
          defaultRenderer.setBackgroundSelectionColor(backgroundSelectionColor);
          defaultRenderer.setFont(new Font("Calibri", Font.PLAIN, 24));
    }
    finally, the tree is shown like this:
    the photo is also attached.

    You can see that the nodes are horizontally and vertically cut!

    Does anyone have any ideas what I should do so that the nodes appear properly? I printed the Scroll Pane,Tree, ViewPort preferred sizes and all are smaller than the size they are given.

    Thanks in advance!
    Attached Files
  • ssaraceni
    New Member
    • Sep 2008
    • 18

    #2
    You can dinamically calculate row height with:
    Code:
    FontMetrics m=new JLabel().getFontMetrics(new Font("Calibri", Font.PLAIN, 24));
    Dimension d=new Dimension(m.stringWidth("AjWtYJ"),m.getHeight()+4);
    and then set preferred row height with:
    Code:
    tree.setRowHeight(d.height);

    Comment

    Working...