I have filtered successfully a JTree using the View filtered approach (in the tree render, setting preferred component size to 0 for the items filtered out). Please see [Filtering on a JTree
I have a scaling environment with more than 1 million tree items.
The problem is that when some tree items are filtered out, the performance decreases considerably (expanding tree, scrolling is very slow).
My code is the following:
public static class TreeRenderer extends DefaultTreeCellRenderer
{
@Override
public Component getTreeCellRendererComponent( final JTree tree, final Object value, final boolean selected,
final boolean expanded, final boolean leaf, final int row, final boolean hasFocus )
{
// Invoke default Implementation, setting all values of this
super.getTreeCellRendererComponent( tree, value, selected, expanded, leaf, row, hasFocus );
if( !isNodeVisible( (DefaultMutableTreeNode)value ) )
{
setPreferredSize( new Dimension( 0, 0 ) );
}
else
{
setPreferredSize( new Dimension( 200, 15 ) );
}
return this;
}
}
public static boolean isNodeVisible( final DefaultMutableTreeNode value )
{
// In this example all Nodes without a UserObject are invisible
return value.getUserObject() != null;
}
If I change the values (width, height) of setPreferredSize(new Dimension(0,0)) by other than 0, the performance increases to normal.
What is the deal about setting a zero-sized component in a Jtree? What am I missing? Is there I way I can sort this out using still the View approach?
Thanks