下面列出了javax.swing.tree.DefaultTreeModel#insertNodeInto ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(this);
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultTreeModel model = new DefaultTreeModel(root);
this.tree = new JTree();
this.tree.setModel(model);
JFrame frame = new JFrame(getClass().getSimpleName());
frame.add(this.tree);
model.addTreeModelListener(this); // frame is not visible yet
model.insertNodeInto(child, root, root.getChildCount());
model.removeNodeFromParent(child);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(this);
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultTreeModel model = new DefaultTreeModel(root);
this.tree = new JTree();
this.tree.setModel(model);
JFrame frame = new JFrame(getClass().getSimpleName());
frame.add(this.tree);
model.addTreeModelListener(this); // frame is not visible yet
model.insertNodeInto(child, root, root.getChildCount());
model.removeNodeFromParent(child);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(this);
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultTreeModel model = new DefaultTreeModel(root);
this.tree = new JTree();
this.tree.setModel(model);
JFrame frame = new JFrame(getClass().getSimpleName());
frame.add(this.tree);
model.addTreeModelListener(this); // frame is not visible yet
model.insertNodeInto(child, root, root.getChildCount());
model.removeNodeFromParent(child);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(this);
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultTreeModel model = new DefaultTreeModel(root);
this.tree = new JTree();
this.tree.setModel(model);
JFrame frame = new JFrame(getClass().getSimpleName());
frame.add(this.tree);
model.addTreeModelListener(this); // frame is not visible yet
model.insertNodeInto(child, root, root.getChildCount());
model.removeNodeFromParent(child);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(this);
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultTreeModel model = new DefaultTreeModel(root);
this.tree = new JTree();
this.tree.setModel(model);
JFrame frame = new JFrame(getClass().getSimpleName());
frame.add(this.tree);
model.addTreeModelListener(this); // frame is not visible yet
model.insertNodeInto(child, root, root.getChildCount());
model.removeNodeFromParent(child);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(this);
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultTreeModel model = new DefaultTreeModel(root);
this.tree = new JTree();
this.tree.setModel(model);
JFrame frame = new JFrame(getClass().getSimpleName());
frame.add(this.tree);
model.addTreeModelListener(this); // frame is not visible yet
model.insertNodeInto(child, root, root.getChildCount());
model.removeNodeFromParent(child);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(this);
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultTreeModel model = new DefaultTreeModel(root);
this.tree = new JTree();
this.tree.setModel(model);
JFrame frame = new JFrame(getClass().getSimpleName());
frame.add(this.tree);
model.addTreeModelListener(this); // frame is not visible yet
model.insertNodeInto(child, root, root.getChildCount());
model.removeNodeFromParent(child);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
public AquaLocationSourceList(LocationHistory directoryStack,
GraphicCache graphicCache) {
super(new DefaultTreeModel(new DefaultMutableTreeNode("root")));
treeModel = (DefaultTreeModel) getModel();
root = (DefaultMutableTreeNode) treeModel.getRoot();
this.directoryStack = directoryStack;
this.graphicCache = graphicCache;
treeModel.insertNodeInto(devices, root, 0);
treeModel.insertNodeInto(places, root, 1);
addTreeSelectionListener(treeSelectionListener);
directoryStack.addChangeListener(directoryStackListener);
setExpandsSelectedPaths(true);
setCellRenderer(new SourceListLocationRenderer(graphicCache, this));
getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
setUI(new SourceListTreeUI(this));
expandPath(new TreePath(devices.getPath()));
expandPath(new TreePath(places.getPath()));
}
public boolean moveToIndex(int index, boolean updateSelection) {
boolean result = false;
DefaultTreeModel model = getTreeModel();
Editor p = getEditorParent();
if (p != null && model != null) {
index = Math.min(Math.max(0, index), p.getChildCount());
if (index != p.getIndex(this)) {
p.setModified(true);
model.removeNodeFromParent(this);
model.insertNodeInto(this, p, index);
result = true;
if (updateSelection)
select();
}
}
return result;
}
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(this);
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultTreeModel model = new DefaultTreeModel(root);
this.tree = new JTree();
this.tree.setModel(model);
JFrame frame = new JFrame(getClass().getSimpleName());
frame.add(this.tree);
model.addTreeModelListener(this); // frame is not visible yet
model.insertNodeInto(child, root, root.getChildCount());
model.removeNodeFromParent(child);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
public static void appendCatalogNode(MutableTreeNode parentNode, DefaultTreeModel treeModel, InvCatalogRef catalogRef) {
final DefaultMutableTreeNode catalogNode = new DefaultMutableTreeNode(catalogRef.getName());
final String catalogPath = catalogRef.getURI().toASCIIString();
final CatalogNode opendapNode = new CatalogNode(catalogPath, catalogRef);
opendapNode.setCatalogUri(catalogPath);
catalogNode.add(new DefaultMutableTreeNode(opendapNode));
treeModel.insertNodeInto(catalogNode, parentNode, parentNode.getChildCount());
}
protected static void add(@Nullable final DefaultTreeModel model, final InspectionTreeNode child, final InspectionTreeNode parent) {
if (model == null) {
insertByIndex(child, parent);
}
else {
if (parent.getIndex(child) < 0) {
model.insertNodeInto(child, parent, child.getParent() == parent ? parent.getChildCount() - 1 : parent.getChildCount());
}
}
}
public void addProfileToTree(Profile profile, boolean selectRow) {
// Panels
ProfilePanel profilePanel = new ProfilePanel(profile);
RepositoryPanel repoPanel = new RepositoryPanel(profile);
FoldersPanel foldersPanel = new FoldersPanel(profile);
profilePanel.load();
repoPanel.load();
foldersPanel.load();
// Add to tree
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
DefaultMutableTreeNode profileNode = new DefaultMutableTreeNode(new ShowPanelTreeItem(profile.getName(), "profile", profilePanel));
profileNode.add(new DefaultMutableTreeNode(new ShowPanelTreeItem(resourceBundle.getString("sd_remote_storage"), "repository", repoPanel)));
profileNode.add(new DefaultMutableTreeNode(new ShowPanelTreeItem(resourceBundle.getString("sd_sync_folders"), "folders", foldersPanel)));
model.insertNodeInto(profileNode, root, root.getChildCount() - 1);
if (selectRow) {
int profileNodeIndex = tree.getRowForPath(new TreePath(profileNode.getPath()));
tree.expandRow(profileNodeIndex);
tree.setSelectionRow(profileNodeIndex);
}
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
// Extract transfer data.
DefaultMutableTreeNode[] nodes = null;
try {
Transferable t = support.getTransferable();
nodes = (DefaultMutableTreeNode[]) t.getTransferData(nodesFlavor);
} catch (UnsupportedFlavorException ufe) {
System.out.println("UnsupportedFlavor: " + ufe.getMessage());
} catch (java.io.IOException ioe) {
System.out.println("I/O error: " + ioe.getMessage());
}
// Get drop location info.
JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
int childIndex = dl.getChildIndex();
TreePath dest = dl.getPath();
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)
dest.getLastPathComponent();
JTree tree = (JTree) support.getComponent();
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
// Configure for drop mode.
int index = childIndex; // DropMode.INSERT
if (childIndex == -1) { // DropMode.ON
index = parent.getChildCount();
}
// Add data to model.
for (DefaultMutableTreeNode node : nodes) {
model.insertNodeInto(node, parent, index++);
}
return true;
}
void addTargetsNode(@Nonnull Node node, @Nonnull DefaultTreeModel treeModel) {
ApplicationManager.getApplication().assertIsDispatchThread();
int index;
synchronized (this) {
index = getNodeInsertionIndex(node, getSwingChildren());
myChildren.add(index, node);
}
treeModel.insertNodeInto(node, this, index);
}
private void createTree(
Map<Long, Set<Long>> dependencies,
DefaultTreeModel dependencyTreeModel,
MutableTreeNode parent,
Set<Long> codeSourceIds,
Set<Long> shownSourceIds) {
int i = 0;
for (Long codeSourceId : codeSourceIds) {
try {
CodeSource codeSource = DaoFactory.getCodeSourceDao().get(codeSourceId);
MutableTreeNode dependencyNode = new DefaultMutableTreeNode(codeSource.getSource());
dependencyTreeModel.insertNodeInto(dependencyNode, parent, i);
if (!shownSourceIds.contains(codeSourceId)) {
shownSourceIds.add(codeSourceId);
createTree(dependencies, dependencyTreeModel, dependencyNode, dependencies.get(codeSourceId), shownSourceIds);
}
} catch (DaoException e) {
Main.getInstance().processException(e);
}
i++;
}
}
private TreeModel buildChangesModel() {
myRoot = new DefaultMutableTreeNode(ROOT_NODE_VALUE); // not null for TreeState matching to work
DefaultTreeModel model = new DefaultTreeModel(myRoot);
final List<ShelvedChangeList> changeLists = new ArrayList<ShelvedChangeList>(myShelveChangesManager.getShelvedChangeLists());
Collections.sort(changeLists, ChangelistComparator.getInstance());
if (myShelveChangesManager.isShowRecycled()) {
ArrayList<ShelvedChangeList> recycled =
new ArrayList<ShelvedChangeList>(myShelveChangesManager.getRecycledShelvedChangeLists());
Collections.sort(recycled, ChangelistComparator.getInstance());
changeLists.addAll(recycled);
}
myMoveRenameInfo.clear();
for(ShelvedChangeList changeList: changeLists) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(changeList);
model.insertNodeInto(node, myRoot, myRoot.getChildCount());
final List<Object> shelvedFilesNodes = new ArrayList<Object>();
List<ShelvedChange> changes = changeList.getChanges(myProject);
for(ShelvedChange change: changes) {
putMovedMessage(change.getBeforePath(), change.getAfterPath());
shelvedFilesNodes.add(change);
}
List<ShelvedBinaryFile> binaryFiles = changeList.getBinaryFiles();
for(ShelvedBinaryFile file: binaryFiles) {
putMovedMessage(file.BEFORE_PATH, file.AFTER_PATH);
shelvedFilesNodes.add(file);
}
Collections.sort(shelvedFilesNodes, ShelvedFilePatchComparator.getInstance());
for (int i = 0; i < shelvedFilesNodes.size(); i++) {
final Object filesNode = shelvedFilesNodes.get(i);
final DefaultMutableTreeNode pathNode = new DefaultMutableTreeNode(filesNode);
model.insertNodeInto(pathNode, node, i);
}
}
return model;
}
@Override public void drop(DropTargetDropEvent dtde) {
// System.out.println("drop");
// if (!isWebStart()) {
// try {
// draggingObject = dtde.getTransferable().getTransferData(FLAVOR);
// } catch (Exception ex) {
// rejectDrag(dtde);
// return;
// }
// } else {
// draggingObject = getSelectionPath().getLastPathComponent();
// }
Object draggingObject = getSelectionPath().getLastPathComponent();
Point pt = dtde.getLocation();
TreePath path = getPathForLocation(pt.x, pt.y);
if (Objects.isNull(path) || !(draggingObject instanceof MutableTreeNode)) {
dtde.dropComplete(false);
return;
}
// System.out.println("drop path is " + path);
MutableTreeNode draggingNode = (MutableTreeNode) draggingObject;
DefaultMutableTreeNode targetNode = (DefaultMutableTreeNode) path.getLastPathComponent();
if (targetNode.equals(draggingNode)) {
// Cannot move the node to the node itself
dtde.dropComplete(false);
return;
}
dtde.acceptDrop(DnDConstants.ACTION_MOVE);
DefaultTreeModel model = (DefaultTreeModel) getModel();
model.removeNodeFromParent(draggingNode);
TreeNode parent = targetNode.getParent();
if (parent instanceof MutableTreeNode && targetNode.isLeaf()) {
model.insertNodeInto(draggingNode, (MutableTreeNode) parent, parent.getIndex(targetNode));
} else {
model.insertNodeInto(draggingNode, targetNode, targetNode.getChildCount());
}
dtde.dropComplete(true);
dropTargetNode = null;
draggedNode = null;
repaint();
}
/**
* Inserts a child into a parent node at a specified index.
*
* @param child the child node
* @param parent the parent node
* @param index the index
* @return true if added
*/
protected boolean insertChildAt(final LibraryTreeNode child, LibraryTreeNode parent, int index) {
if (tree==null || parent.getChildCount()<index) return false;
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
model.insertNodeInto(child, parent, index);
return true;
}
public JComponent generateReport() {
Map<Long, Set<Long>> dependencies = DaoFactory.getReportDao().getDependencies();
JTree dependencyTree = new JTree();
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Dependency tree");
DefaultTreeModel dependencyTreeModel = new DefaultTreeModel(rootNode);
int i = 0;
for (Long codeSourceId : dependencies.keySet()) {
try {
CodeSource codeSource = DaoFactory.getCodeSourceDao().get(codeSourceId);
MutableTreeNode dependencyNode = new DefaultMutableTreeNode(codeSource.getSource());
dependencyTreeModel.insertNodeInto(dependencyNode, rootNode, i);
createTree(
dependencies,
dependencyTreeModel,
dependencyNode,
dependencies.get(codeSourceId),
new LinkedHashSet<Long>(Arrays.asList(codeSourceId)));
} catch (DaoException e) {
Main.getInstance().processException(e);
}
i++;
}
dependencyTree.setModel(dependencyTreeModel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setViewportView(dependencyTree);
return scrollPane;
}