checkboxes. This is a problem with CheckBoxTree's 'dig in' mode in which
it selects the parent node if all of the child nodes are selected. But,
if you programmatically select the child nodes and not just the parent
node, it causes this stack overflow.
- Code: Select all
import java.awt.Frame;
import java.util.Enumeration;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import com.jidesoft.swing.CheckBoxTree;
import com.jidesoft.swing.CheckBoxTreeSelectionModel;
/**
* TO REPRODUCE BUG:
*
* Run this code, click on a checkbox, wait for stack overflow error in
* AWT event queue.
*
* Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.util.AbstractCollection.<init>(AbstractCollection.java:49)
at java.util.AbstractSet.<init>(AbstractSet.java:46)
at java.util.HashSet.<init>(HashSet.java:85)
at com.jidesoft.swing.CheckBoxTreeSelectionModel.addSelectionPaths(Unknown Source)
at com.jidesoft.swing.CheckBoxTreeSelectionModel.isPathSelected(Unknown Source)
at com.jidesoft.swing.CheckBoxTreeSelectionModel.addSelectionPaths(Unknown Source)
at com.jidesoft.swing.CheckBoxTreeSelectionModel.isPathSelected(Unknown Source)
at com.jidesoft.swing.CheckBoxTreeSelectionModel.addSelectionPaths(Unknown Source)
at com.jidesoft.swing.CheckBoxTreeSelectionModel.isPathSelected(Unknown Source)
...
*/
@SuppressWarnings( "serial" )
public class CheckBoxTreeBug extends JPanel
{
CheckBoxTreeBug()
{
DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Contents" );
root.add( new DefaultMutableTreeNode( "Llama" ) );
root.add( new DefaultMutableTreeNode( "Aardvark" ) );
root.add( new DefaultMutableTreeNode( "Platypus" ) );
root.add( new DefaultMutableTreeNode( "Rutabaga" ) );
DefaultTreeModel treeModel = new DefaultTreeModel( root );
CheckBoxTree tree = new CheckBoxTree( treeModel );
CheckBoxTreeSelectionModel selmodel = tree.getCheckBoxTreeSelectionModel();
//
// Select all child nodes of the root
//
Enumeration<?> kids = root.children();
while( kids.hasMoreElements() )
{
DefaultMutableTreeNode kid = (DefaultMutableTreeNode) kids.nextElement();
selmodel.addSelectionPath( new TreePath( kid.getPath() ) );
}
add( tree );
}
public static void main( String[] args )
{
JDialog dialog = new JDialog( (Frame) null, "CheckBoxTreeBug" );
dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
dialog.getContentPane().add( new CheckBoxTreeBug() );
dialog.pack();
dialog.setVisible( true );
}
}