/** Window for showing Tree. */ protected JFrame frame; /** Tree used for the example. */ protected JTree tree; /** Tree model. */ protected DefaultTreeModel treeModel;
/** *ConstructsanewinstanceofSampleTree.
*/ public SampleTree() { // Trying to set Nimbus look and feel try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName()); break;
}
}
} catch (Exception ignored) {
}
JMenuBar menuBar = constructMenuBar();
JPanel panel = new JPanel(true);
frame = new JFrame("SampleTree");
frame.getContentPane().add("Center", panel);
frame.setJMenuBar(menuBar);
frame.setBackground(Color.lightGray);
/* Create the JTreeModel. */
DefaultMutableTreeNode root = createNewNode("Root");
treeModel = new SampleTreeModel(root);
/* Create the tree. */
tree = new JTree(treeModel);
/* Enable tool tips for the tree, without this tool tips will not
be picked up. */
ToolTipManager.sharedInstance().registerComponent(tree);
/* Make the tree use an instance of SampleTreeCellRenderer for
drawing. */
tree.setCellRenderer(new SampleTreeCellRenderer());
/* Make tree ask for the height of each row. */
tree.setRowHeight(-1);
/* Put the Tree in a scroller. */
JScrollPane sp = new JScrollPane();
sp.setPreferredSize(new Dimension(300, 300));
sp.getViewport().add(tree);
/* And show it. */
panel.setLayout(new BorderLayout());
panel.add("Center", sp);
panel.add("South", constructOptionsPanel());
/** Constructs a JPanel containing check boxes for the different
* options that tree supports. */
@SuppressWarnings("serial") private JPanel constructOptionsPanel() {
JCheckBox aCheckbox;
JPanel retPanel = new JPanel(false);
JPanel borderPane = new JPanel(false);
aCheckbox = new JCheckBox("show top level handles");
aCheckbox.setSelected(tree.getShowsRootHandles());
aCheckbox.addChangeListener(new ShowHandlesChangeListener());
retPanel.add(aCheckbox);
aCheckbox = new JCheckBox("show root");
aCheckbox.setSelected(tree.isRootVisible());
aCheckbox.addChangeListener(new ShowRootChangeListener());
retPanel.add(aCheckbox);
aCheckbox = new JCheckBox("editable");
aCheckbox.setSelected(tree.isEditable());
aCheckbox.addChangeListener(new TreeEditableChangeListener());
aCheckbox.setToolTipText("Triple click to edit");
retPanel.add(aCheckbox);
borderPane.add(retPanel, BorderLayout.CENTER);
/* Create a set of radio buttons that dictate what selection should
be allowed in the tree. */
ButtonGroup group = new ButtonGroup();
JPanel buttonPane = new JPanel(false);
JRadioButton button;
// NOTE: This will be enabled in a future release. // Create a label and combobox to determine how many clicks are // needed to expand. /* JPanelclickPanel=newJPanel(); Object[]values={"Never",newInteger(1), newInteger(2),newInteger(3)}; finalJComboBoxclickCBox=newJComboBox(values);
/* Determine where to create the new node. */ if (lastItem != null) {
parent = (DefaultMutableTreeNode) lastItem.getParent(); if (parent == null) {
parent = (DefaultMutableTreeNode) treeModel.getRoot();
lastItem = null;
}
} else {
parent = (DefaultMutableTreeNode) treeModel.getRoot();
} if (parent == null) { // new root
treeModel.setRoot(createNewNode("Added " + Integer.toString(
addCount++)));
} else { int newIndex; if (lastItem == null) {
newIndex = treeModel.getChildCount(parent);
} else {
newIndex = parent.getIndex(lastItem) + 1;
}
/* Let the treemodel know. */
treeModel.insertNodeInto(createNewNode("Added " + Integer.
toString(addCount++)),
parent, newIndex);
}
}
} // End of SampleTree.AddAction
/** *InsertActionisusedtoinsertanewitembeforetheselecteditem.
*/ class InsertAction extends Object implements ActionListener {
/** Number of nodes that have been added. */ publicint insertCount;
/* Let the treemodel know. */
treeModel.insertNodeInto(createNewNode("Inserted " + Integer.
toString(insertCount++)),
parent, newIndex);
}
}
} // End of SampleTree.InsertAction
// The remove process consists of the following steps: // 1 - find the shallowest selected TreePath, the shallowest // path is the path with the smallest number of path // components. // 2 - Find the siblings of this TreePath // 3 - Remove from selected the TreePaths that are descendants // of the paths that are going to be removed. They will // be removed as a result of their ancestors being // removed. // 4 - continue until selected contains only null paths. while ((shallowest = findShallowestPath(selected)) != null) {
removeSiblings(shallowest, selected);
}
}
}
/** *RemovesthesiblingTreePathsof<code>path</code>,thatare *locatedin<code>paths</code>.
*/ privatevoid removeSiblings(TreePath path, TreePath[] paths) { // Find the siblings if (path.getPathCount() == 1) { // Special case, set the root to null for (int counter = paths.length - 1; counter >= 0; counter--) {
paths[counter] = null;
}
treeModel.setRoot(null);
} else { // Find the siblings of path.
TreePath parent = path.getParentPath();
MutableTreeNode parentNode = (MutableTreeNode) parent.
getLastPathComponent();
ArrayList<TreePath> toRemove = new ArrayList<TreePath>();
// First pass, find paths with a parent TreePath of parent for (int counter = paths.length - 1; counter >= 0; counter--) { if (paths[counter] != null && paths[counter].getParentPath().
equals(parent)) {
toRemove.add(paths[counter]);
paths[counter] = null;
}
}
// Second pass, remove any paths that are descendants of the // paths that are going to be removed. These paths are // implicitly removed as a result of removing the paths in // toRemove int rCount = toRemove.size(); for (int counter = paths.length - 1; counter >= 0; counter--) { if (paths[counter] != null) { for (int rCounter = rCount - 1; rCounter >= 0;
rCounter--) { if ((toRemove.get(rCounter)).isDescendant(
paths[counter])) {
paths[counter] = null;
}
}
}
}
// Sort the siblings based on position in the model if (rCount > 1) {
Collections.sort(toRemove, new PositionComparator());
} int[] indices = newint[rCount];
Object[] removedNodes = new Object[rCount]; for (int counter = rCount - 1; counter >= 0; counter--) {
removedNodes[counter] = (toRemove.get(counter)).
getLastPathComponent();
indices[counter] = treeModel.getIndexOfChild(parentNode,
removedNodes[counter]);
parentNode.remove(indices[counter]);
}
treeModel.nodesWereRemoved(parentNode, indices, removedNodes);
}
}
publicvoid stateChanged(ChangeEvent e) {
tree.setShowsRootHandles(((JCheckBox) e.getSource()).isSelected());
}
} // End of class SampleTree.ShowHandlesChangeListener
publicvoid stateChanged(ChangeEvent e) {
tree.setRootVisible(((JCheckBox) e.getSource()).isSelected());
}
} // End of class SampleTree.ShowRootChangeListener
publicvoid stateChanged(ChangeEvent e) {
tree.setEditable(((JCheckBox) e.getSource()).isSelected());
}
} // End of class SampleTree.TreeEditableChangeListener
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.