Mudando Icone de Jtree com TreeCellRenderer

3 respostas
Alchemist

Boa Tarde Pessoal estou com um probleminha aqui que eu não consigo resolver, estou criando uma classe que implementa a TreeCellRenderer, assim utilizo ela em uma JTree:

package jsprint.gui.componentes.renderer;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.TreeCellRenderer;

public class JTreeSprintRenderer extends JPanel implements TreeCellRenderer {
	
	protected boolean selected;
	
	private boolean hasFocus;
	
	private boolean drawsFocusBorderAroundIcon;

	transient protected Icon closedIcon;

	transient protected Icon leafIcon;

	transient protected Icon openIcon;

	protected Color textSelectionColor;

	protected Color textNonSelectionColor;

	protected Color backgroundSelectionColor;

	protected Color backgroundNonSelectionColor;

	protected Color borderSelectionColor;
	
	JTextArea field = new JTextArea();

	public JTreeSprintRenderer() {
		Object value = UIManager.get("Tree.drawsFocusBorderAroundIcon");
		drawsFocusBorderAroundIcon = (value != null && ((Boolean)value).booleanValue());
		setLayout(new GridLayout(1,1));
		add(field);
		field.setBorder(BorderFactory.createEmptyBorder());
	}

	/**
	* Configures the renderer based on the passed in components.
	* The value is set from messaging value with toString().
	* The foreground color is set based on the selection and the icon
	* is set based on on leaf and expanded.
	*/
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		String stringValue = tree.convertValueToText(value, sel, expanded, leaf, row, hasFocus);
		this.hasFocus = hasFocus;
		field.setText(stringValue);
		if (sel) {
			field.setForeground(Color.white);
			field.setBackground(Color.blue);
		} else {
			field.setForeground(Color.black);
			field.setBackground(Color.white);
		}
		if (!tree.isEnabled()) {
			field.setEnabled(false);
		} else {
			field.setEnabled(true);
		}
	
		
		if (hasFocus) {
			this.setBorder(BorderFactory.createLineBorder(Color.blue));
			//this.setLeafIcon(new ImageIcon("src/jsprint/gui/image/menu/open.png"));
		} else {
			this.setBorder(BorderFactory.createEmptyBorder());
		}

		
		selected = sel;

		return this;
	}
}

O que eu gostaria de fazer e estou precisando de ajuda:

Eu gostaria de colocar um icone diferente para a raiz, para o folhas e para os ramos da classe, como eu posso fazer isto ?

3 Respostas

Alchemist

Niguém ? :S nenhuma dica ? NADA ? :frowning:

Alchemist

Consegui fazer, vou postar aqui caso alguém algum dia precise…

package jsprint.gui.componentes.renderer;

import java.awt.Color;
import java.awt.Component;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.TreeCellRenderer;

/**
 * Classe responsável por estilizar uma Jtree para sprint.
 * 
 * @author estevao.costa
 *
 */
public class JTreeSprintRenderer extends JLabel implements TreeCellRenderer {

	/**
	 * Seriakl Id.
	 */
	private static final long serialVersionUID = 2421948525968191554L;

	/**
	 * Icone de fechado.
	 */
	private Icon closedIcon;

	/**
	 * Icone da raiz.
	 */
	private Icon rootIcon;
	
	/**
	 * Icone da folha.
	 */
	private Icon leafIcon;

	/**
	 * Icone de aberto.
	 */
	private Icon openIcon;

	/**
	 * Cor do texto selecionado.
	 */
	private Color corTextoSelecionado;

	/**
	 * Cor dos nós.
	 */
	private Color corTextoNaoSelecionado;

	/**
	 * Cor de fundo.
	 */
	private Color corFundoSelecionado;

	/**
	 * Cor do fundo dos nos selecionados.
	 */
	private Color corFundoNaoSelecionado;

	
	/**
	 * Construtor.
	 */
	public JTreeSprintRenderer() {
	}
	
	/**
	 * Método responsável pela renderização do componete.
	 */
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		
		String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
		
		if (selected) {
			this.setForeground(this.corTextoSelecionado);
			this.setBackground(this.corFundoSelecionado);
		} else {
			this.setForeground(this.corTextoNaoSelecionado);
			this.setBackground(this.corFundoNaoSelecionado);
		}
		
		if (!tree.isEnabled()) {
			this.setEnabled(false);
		} else {
			this.setEnabled(true);
		}
		
        Icon icon = null;
        
        if (isRoot(row)) {
        	icon = getRootIcon();
        } else if (leaf) {
            icon = getLeafIcon();
        } else if (expanded) {
            icon = getOpenIcon();
        } else {
            icon = getClosedIcon();
        }
        
		this.setIcon(icon);
		this.setText(stringValue);
		this.repaint();
		return this;
	}

	/**
	 * Método responsável por verificar se a linha corrente é raiz ou não.
	 * @param row
	 * @return
	 */
	private boolean isRoot(final Integer row){
		return row == 0;
	}

	public Icon getRootIcon() {
		return rootIcon;
	}

	public void setRootIcon(Icon rootIcon) {
		this.rootIcon = rootIcon;
	}

	/**
	 * Sets the icon used to represent non-leaf nodes that are expanded.
	 */
	public void setOpenIcon(Icon newIcon) {
		openIcon = newIcon;
	}

	/**
	 * Returns the icon used to represent non-leaf nodes that are expanded.
	 */
	public Icon getOpenIcon() {
		return openIcon;
	}

	/**
	 * Sets the icon used to represent non-leaf nodes that are not expanded.
	 */
	public void setClosedIcon(Icon newIcon) {
		closedIcon = newIcon;
	}

	/**
	 * Returns the icon used to represent non-leaf nodes that are not expanded.
	 */
	public Icon getClosedIcon() {
		return closedIcon;
	}

	/**
	 * Sets the icon used to represent leaf nodes.
	 */
	public void setLeafIcon(Icon newIcon) {
		leafIcon = newIcon;
	}

	/**
	 * Returns the icon used to represent leaf nodes.
	 */
	public Icon getLeafIcon() {
		return leafIcon;
	}

	public Color getCorTextoSelecionado() {
		return corTextoSelecionado;
	}

	public void setCorTextoSelecionado(Color corTextoSelecionado) {
		this.corTextoSelecionado = corTextoSelecionado;
	}

	public Color getCorTextoNaoSelecionado() {
		return corTextoNaoSelecionado;
	}

	public void setCorTextoNaoSelecionado(Color corTextoNaoSelecionado) {
		this.corTextoNaoSelecionado = corTextoNaoSelecionado;
	}

	public Color getCorFundoSelecionado() {
		return corFundoSelecionado;
	}

	public void setCorFundoSelecionado(Color corFundoSelecionado) {
		this.corFundoSelecionado = corFundoSelecionado;
	}

	public Color getCorFundoNaoSelecionado() {
		return corFundoNaoSelecionado;
	}

	public void setCorFundoNaoSelecionado(Color corFundoNaoSelecionado) {
		this.corFundoNaoSelecionado = corFundoNaoSelecionado;
	}
}
L

Parabéns Alchemist!

Estava precisando disto mesmo… Me poupou algumas horinhas…

[]s

Criado 2 de agosto de 2011
Ultima resposta 2 de mai. de 2012
Respostas 3
Participantes 2