Friday, April 20, 2007

Making a JTree transparent

To make a JTree completely transparent (i.e even after the tree.setOpaque(false) leaves the leaf nodes with white part.. The below code removes even that and makes it totally transparent).

DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
    public Color getBackgroundNonSelectionColor() {
        return(null);
    }

    public Color getBackground() {
        return(null);
    }
};

2 comments:

  1. thanks for this piece of code.
    its very tricky.

    ReplyDelete
  2. Ok basically this works, but using this approach disabling the tree gets you a pretty nice NullPointerException. To avoid this use
    setBackgroundNonSelectionColor(new Color(0, 0, 0, 0)); on the renderer object instead. Same effect no NPEs.

    ReplyDelete