Problemns with JTree
Hi! I'm having problems with the Tree cell renderer. I must render a tree that have leafs that is also JPanels compounded by a checkbox, an icon and a the text label. It's supposed to alter the state of the checkbox when clicked or when press the spacebar over the cell, but it isn't working...
If I set the tree as editable (down in the code, in main method), the checkbox is selected only after the second click, but the cell is not being highlighted and cannot collapse the category nodes. Otherwise, when set the Tree as NOT editable, the highlight of cell and the rest works, but obviously I cannot edit the checkboxes...
Can someone help me, please?
package test;
import com.interact.sas.ResourceLocator;
import com.interact.sas.dms.*;
import com.interact.sas.ui.UserIconLabel;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.EventObject;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.tree.*;
publicclass DocumentControllerTreeTest
{
staticclass MyTreeCellEditorextends AbstractCellEditorimplements TreeCellEditor, TreeCellRenderer
{
privatestatic Icon iconCategory = ResourceLocator.getIcon("sas/dms/tb_categories.png" );
privatestatic Border noFocusBorder =new EmptyBorder( 1, 1, 1, 1 );
private JPanel cell =new JPanel();
private JCheckBox checkRequired =new JCheckBox();
private UserIconLabel userIconLabel =new UserIconLabel();
private DocumentControllerTree.Item currentItem =null;
public MyTreeCellEditor()
{
userIconLabel.setOpaque(false );
checkRequired.setOpaque(false );
cell.setLayout(new BorderLayout() );
cell.add( checkRequired, BorderLayout.WEST );
cell.add( userIconLabel, BorderLayout.CENTER );
cell.addKeyListener(new KeyAdapter()
{
publicvoid keyPressed( KeyEvent e )
{
if ( e.getKeyCode() == KeyEvent.VK_SPACE )
{
checkRequired.setSelected( ! checkRequired.isSelected() );
e.consume();
}
}
} );
cell.addMouseListener(new java.awt.event.MouseAdapter()
{
publicvoid mouseClicked(java.awt.event.MouseEvent evt)
{
checkRequired.setSelected( ! checkRequired.isSelected() );
}
});
}
/**
* getCellEditorValue
*
* @return Object
*/
public Object getCellEditorValue()
{
currentItem.getController().setRequired( checkRequired.isSelected() );
returnnew Boolean( currentItem.getController().isRequired() );
}
/**
* getTreeCellEditorComponent
*
* @param JTree tree,
* @param Object value,
* @param boolean isSelected,
* @param boolean expanded,
* @param boolean leaf,
* @param int row
* @return Component
*/
public Component getTreeCellEditorComponent( JTree tree,
Object value,
boolean isSelected,
boolean expanded,
boolean leaf,
int row)
{
return getTreeCellRendererComponent( tree, value, isSelected, expanded, leaf, row,true );
}
/**
* getTreeCellRendererComponent
*
* @param JTree tree,
* @param Object value,
* @param boolean selected,
* @param boolean expanded,
* @param boolean leaf,
* @param int row,
* @param boolean hasFocus
* @return Component
*/
public Component getTreeCellRendererComponent( JTree tree,
Object value,
boolean isSelected,
boolean isExpanded,
boolean isLeaf,
int row,
boolean hasFocus )
{
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
Object object = node.getUserObject();
if ( objectinstanceof DocumentControllerTree.Item )
{
currentItem = (DocumentControllerTree.Item) object;
checkRequired.setSelected( currentItem.getController().isRequired() );
//checkRequired.setSelected( !checkRequired.isSelected() );
int action = currentItem.getController().getAction();
if ( action == DocumentController.ACTION_APROVE ||
action == DocumentController.ACTION_VERIFY )
{
checkRequired.setVisible(true );
}
else
{
checkRequired.setVisible(false );
}
userIconLabel.setFont( tree.getFont() );
userIconLabel.setUser( currentItem.getUser() );
userIconLabel.setText( currentItem.getUser().getName() );
}
else
{
// nunca poderia chegar aqui ...
checkRequired.setVisible(false );
userIconLabel.setFont( tree.getFont() );
userIconLabel.setIcon( ! isLeaf ? iconCategory :null );
userIconLabel.setText( object.toString() );
}
if ( isSelected )
{
cell.setBackground( UIManager.getColor("Tree.selectionBackground") );
cell.setForeground( UIManager.getColor("Tree.selectionForeground") );
}
else
{
cell.setBackground( tree.getBackground() );
cell.setForeground( tree.getForeground() );
}
Border border = noFocusBorder;
if (hasFocus)
{
if (isSelected)
{
border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
}
if (border ==null)
{
border = UIManager.getBorder("List.focusCellHighlightBorder");
}
}
cell.setBorder(border);
return cell;
}
}
publicstaticvoid main( String[] args )
{
try
{
DocumentControllerTree tree =new DocumentControllerTree();
DocumentManager dm = DocumentManager.getInstance();
DocumentCategory dc = dm.getCategory( 32 );
Vector<DocumentController> vdc = dm.getControllers( dc );
tree.setCellRenderer(new MyTreeCellEditor() );
tree.setCellEditor(new MyTreeCellEditor() );
tree.setCategory( dc );
tree.setControllers( vdc );
// This must be true
//tree.setEditable( true );
JScrollPane scroller =new JScrollPane( tree );
scroller.setPreferredSize(new Dimension( 240, 320 ) );
JOptionPane.showMessageDialog( null, scroller );
}
catch ( Exception e )
{
e.printStackTrace();
}
System.exit( 0 );
}
}

