One first question:
Can someone give me a mailing list address for simple generic questions?

In the folowing of the question of yesterday "JTree with JCheckBox". I need
to repaint the tree if I check or uncheck the JCheckBox. I'm doing
"tree.invalidate();" but seems that don't work. 

What to do in this case?

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SpringLayout;
import javax.swing.border.Border;
import javax.swing.event.CellEditorListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

public class JTreeWithCheckBox extends JTree {
    Map m_mapSelectedMap = new HashMap();
    public JTreeWithCheckBox() {    
        TreeWithCheckBoxCell tr = new TreeWithCheckBoxCell();   
   
        setEditable(true);                 
        setCellRenderer(tr);            //set our custom renderer   
        setCellEditor(tr);              //set our custom editor
    }
    
    static class CheckChild {
        private Map m_mapSelectedMap = new HashMap();
        private boolean m_isChecked;
        public CheckChild(boolean isChecked) {
            m_isChecked = isChecked;
        }
        public boolean isChecked() {
            return m_isChecked;
        }
        public Map getSelectedMap() {
            return m_mapSelectedMap;
        }
    }
    
    static private boolean _isSelected(Object[] path, Map mapSelectedMap,
int nIndex) {
        if ((nIndex < path.length) &&
(mapSelectedMap.containsKey(path[nIndex]))) {
            CheckChild ckcThis =
((CheckChild)mapSelectedMap.get(path[nIndex]));
            if (nIndex+1 == path.length) {
                return ckcThis.isChecked();
            } else if (ckcThis.getSelectedMap().containsKey(path[nIndex+1]))
{
                return _isSelected(path, ckcThis.getSelectedMap(),
nIndex+1);            
            } else {
                return ckcThis.isChecked();                
            }
        } else {
            return false;
        }
    }

    boolean isSelected(TreePath path) {
        if (path == null) {
            return false;
        } else {
            return _isSelected(path.getPath(), m_mapSelectedMap, 0);
        }
    }

    private void _setSelected(Object[] path, Map mapSelectedMap, int nIndex,
boolean isSelected){
        if (nIndex < path.length) {
            if (nIndex+1 == path.length) {
                mapSelectedMap.put(path[nIndex], new
CheckChild(isSelected));
            } else {
                if (! mapSelectedMap.containsKey(path[nIndex])) {
                    mapSelectedMap.put(path[nIndex], new CheckChild(false));
                }
                _setSelected(path,
((CheckChild)mapSelectedMap.get(path[nIndex])).getSelectedMap(), nIndex+1,
isSelected);            
            }
        }    
    }

    void setSelected(TreePath path, boolean isSelected){
        _setSelected(path.getPath(), m_mapSelectedMap, 0, isSelected);    
    }

    class TreeWithCheckBoxCell implements TreeCellRenderer, TreeCellEditor {
        DefaultTreeCellRenderer m_dtcr = new DefaultTreeCellRenderer();
    
        public Component getTreeCellComponent(final JTree tree, Object
value, boolean isSelected, boolean expanded, boolean leaf, int nRow, boolean
hasFocus) {
            System.out.println("Creating: "+value);
            Icon icon;
            if (leaf) {
                icon = m_dtcr.getLeafIcon();    
            } else if (expanded) {
                icon = m_dtcr.getOpenIcon();
            } else {
                icon = m_dtcr.getClosedIcon();
            }
            Color clrText;
            Color clrBackground;
            Border border = null;
            if (isSelected){
                clrText       = m_dtcr.getTextSelectionColor();
                clrBackground = m_dtcr.getBackgroundSelectionColor();
                border = getBorder();        
            } else {
                clrText       = m_dtcr.getTextNonSelectionColor();
                clrBackground = m_dtcr.getBackgroundNonSelectionColor();
            }

            final JPanel pnl = new JPanel();
                
            JCheckBox cb = new JCheckBox();
            cb.setForeground(clrText);
            cb.setBackground(clrBackground);
            final TreePath path = tree.getPathForRow(nRow);
            cb.setSelected(isSelected(path));
            cb.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    JCheckBox cb = (JCheckBox)ae.getSource();
                    setSelected(path, cb.isSelected());
                    tree.invalidate();
                    pnl.invalidate();
                    cb.invalidate();
                }
            });
            
            JLabel label = new JLabel(value.toString(), icon,
m_dtcr.getHorizontalAlignment());
            label.setForeground(clrText);
            label.setBackground(clrBackground);
            if (border != null) {
                label.setBorder(border);    
            }
           
            pnl.setBackground(clrBackground);
        
            pnl.setLayout(new SpringLayout());
            pnl.add(cb);        
            pnl.add(label);        
            SpringUtilities.makeCompactGrid(pnl, 1, 2);
            return pnl;
        }
     
        public Component getTreeCellRendererComponent(JTree tree, Object
objValue, boolean isSelected, boolean isExpanded, boolean inLeaf, int nRow,
boolean hasFocus) {
            return getTreeCellComponent(tree, objValue, isSelected,
isExpanded, inLeaf, nRow, hasFocus);
        }               
    
        public Component getTreeCellEditorComponent(JTree tree, Object
objValue, boolean isSelected, boolean isExpanded, boolean inLeaf, int nRow)
{
            return getTreeCellComponent(tree, objValue, true, isExpanded,
inLeaf, nRow, true);
        }
 
        public void addCellEditorListener(CellEditorListener l) { 
        }      
    
        public void cancelCellEditing() { 
        }      

        public Object getCellEditorValue() {         
            return this;      
        }      
    
        public boolean isCellEditable(EventObject evt) {
            if (evt instanceof MouseEvent) {            
                MouseEvent mevt = (MouseEvent) evt;                        
                if (mevt.getClickCount() == 1) {               
                    return true;            
                }        
            }                  
            return false;      
        }      
    
        public void removeCellEditorListener(CellEditorListener l)      { }

    
        public boolean shouldSelectCell(EventObject anEvent) {         
            return true;      
        }      
    
        public boolean stopCellEditing() {         
            return false;      
        }               
    }  
}
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to