隐藏树节点
来源:互联网 发布:电信机房有多少端口 编辑:程序博客网 时间:2024/06/17 19:46
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode; ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class FilteredTree
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
private class PlainBelliedSneech ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String toString() ...{ return "Plain Bellied Sneech"; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
private class StarBelliedSneech ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String toString() ...{ return "Star Bellied Sneech"; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private class FilteredTreeModel
extends DefaultTreeModel
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
private boolean mShowStarBelliedSneeches= true;
private DefaultMutableTreeNode mRoot;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FilteredTreeModel(DefaultMutableTreeNode root)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
super(root);
mRoot= root;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Object getChild(Object parent, int index)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DefaultMutableTreeNode node=
(DefaultMutableTreeNode) parent;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (mShowStarBelliedSneeches)
return node.getChildAt(index);
int pos= 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i= 0, cnt= 0; i< node.getChildCount(); i++) ...{
if (((DefaultMutableTreeNode) node.getChildAt(i)).getUserObject()
instanceof PlainBelliedSneech)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (cnt++ == index) ...{
pos= i;
break;
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return node.getChildAt(pos);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int getChildCount(Object parent)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DefaultMutableTreeNode node=
(DefaultMutableTreeNode) parent;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (mShowStarBelliedSneeches)
return node.getChildCount();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int childCount= 0;
Enumeration children= node.children();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
while (children.hasMoreElements()) ...{
if (((DefaultMutableTreeNode) children.nextElement()).getUserObject()
instanceof PlainBelliedSneech)
childCount++;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return childCount;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public boolean getShowStarBelliedSneeches() ...{
return mShowStarBelliedSneeches;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void setShowStarBelliedSneeches(boolean showStarBelliedSneeches)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (showStarBelliedSneeches != mShowStarBelliedSneeches) ...{
mShowStarBelliedSneeches= showStarBelliedSneeches;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Object[] path= ...{ mRoot };
int[] childIndices= new int[root.getChildCount()];
Object[] children= new Object[root.getChildCount()];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i= 0; i< root.getChildCount(); i++) ...{
childIndices[i]= i;
children[i]= root.getChildAt(i);
}
fireTreeStructureChanged(this, path, childIndices, children);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private FilteredTree()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
final DefaultMutableTreeNode root= new DefaultMutableTreeNode("Root");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DefaultMutableTreeNode parent;
DefaultMutableTreeNode child;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i= 0; i< 2; i++) ...{
parent= new DefaultMutableTreeNode(new PlainBelliedSneech());
root.add(parent);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int j= 0; j< 2; j++) ...{
child= new DefaultMutableTreeNode(new StarBelliedSneech());
parent.add(child);
for (int k= 0; k< 2; k++)
child.add(new DefaultMutableTreeNode(new PlainBelliedSneech()));
}
for (int j= 0; j< 2; j++)
parent.add(new DefaultMutableTreeNode(new PlainBelliedSneech()));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
parent= new DefaultMutableTreeNode(new StarBelliedSneech());
root.add(parent);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int j= 0; j< 2; j++) ...{
child= new DefaultMutableTreeNode(new PlainBelliedSneech());
parent.add(child);
for (int k= 0; k< 2; k++)
child.add(new DefaultMutableTreeNode(new StarBelliedSneech()));
}
for (int j= 0; j< 2; j++)
parent.add(new DefaultMutableTreeNode(new StarBelliedSneech()));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final FilteredTreeModel model= new FilteredTreeModel(root);
JTree tree= new JTree(model);
tree.setShowsRootHandles(true);
tree.putClientProperty("JTree.lineStyle", "Angled");
tree.setRootVisible(false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JScrollPane sp= new JScrollPane(tree);
sp.setPreferredSize(new Dimension(200,400));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final JCheckBox check= new JCheckBox("Show Star Bellied Sneeches");
check.setSelected(model.getShowStarBelliedSneeches());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
check.addActionListener(new ActionListener() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void actionPerformed(ActionEvent e) ...{
model.setShowStarBelliedSneeches(check.isSelected());
}
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JPanel panel= new JPanel(new BorderLayout());
panel.add(check, BorderLayout.NORTH);
panel.add(sp, BorderLayout.CENTER);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JOptionPane.showOptionDialog(
null, panel, "Sneeches on Beeches",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null, new String[0], null
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.exit(0);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] argv) ...{
new FilteredTree();
}
}