Java GUI 三态导航树(2)

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */


public class FilterTreeCellRenderer
    extends DefaultTreeCellRenderer
{
    private JLabel label = new JLabel();
    private JPanel panel = new JPanel();
    private JCheckBox checkBox = new JCheckBox();
    private Icon allSelectIcon = new ImageIcon(getClass().getResource("all.gif"));
    private Icon partSelectIcon = new ImageIcon(getClass().getResource("part.gif"));
    private Icon noneSelectIcon = new ImageIcon(getClass().getResource("none.gif"));

public FilterTreeCellRenderer()
    {
        setOpenIcon(null);
        setClosedIcon(null);
        setLeafIcon(null);

panel.setLayout(new FlowLayout(0, 0, 0));
        panel.setBackground(getBackgroundNonSelectionColor());
        checkBox.setPreferredSize(new Dimension(18, 18));
        checkBox.setBorderPainted(false);
        checkBox.setBackground(getBackgroundNonSelectionColor());
        label.setBackground(getBackgroundNonSelectionColor());
        panel.setBackground(getBackgroundNonSelectionColor());
        panel.add(checkBox);
        panel.add(label);
    }

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
                                                  boolean leaf, int row, boolean hasFocus)
    {
        if (value instanceof DefaultMutableTreeNode)
        {
            Object obj = ( (DefaultMutableTreeNode) value).getUserObject();
            if (obj instanceof FilterTreeCell)
            {
                FilterTreeCell cell = (FilterTreeCell) obj;
                label.setText(cell.getFilterObject().toString());
                int state = cell.getState();

switch (state)
                {
                    case FilterTreeCell.NONE_SELECT:
                        checkBox.setSelected(false);
                        checkBox.setIcon(noneSelectIcon);
                        break;

case FilterTreeCell.PART_SELECT:
                        checkBox.setSelected(false);
                        checkBox.setIcon(partSelectIcon);
                        break;

case FilterTreeCell.ALL_SELECT:
                        checkBox.setSelected(true);
                        checkBox.setIcon(allSelectIcon);
                        break;
                }

label.setEnabled(tree.isEnabled());

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpzpw.html