本文目錄一覽:
java 動態的給樹添加新節點 望高手指點啊
//先選中節點才能增加節點
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class TreeTest implements ActionListener,TreeModelListener{
JLabel label=null;
JTree tree=null;
DefaultTreeModel treeModel=null;
String nodeName=null;//原有節點名稱
public TreeTest(){
JFrame f=new JFrame(“TreeTest”);
Container contentPane=f.getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode(“資源管理器”);
tree=new JTree(root);
tree.setEditable(true);
tree.addMouseListener(new MouseHandle());
treeModel=(DefaultTreeModel)tree.getModel();
treeModel.addTreeModelListener(this);
JScrollPane scrollPane=new JScrollPane();
scrollPane.setViewportView(tree);
JPanel panel=new JPanel();
JButton b=new JButton(“新增節點”);
b.addActionListener(this);
panel.add(b);
b=new JButton(“刪除節點”);
b.addActionListener(this);
panel.add(b);
b=new JButton(“清除所有節點”);
b.addActionListener(this);
panel.add(b);
label=new JLabel(“Action”);
contentPane.add(panel,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//本方法運行新增、刪除、清除所有節點的程序代碼.
public void actionPerformed(ActionEvent ae){
if (ae.getActionCommand().equals(“新增節點”)){
DefaultMutableTreeNode parentNode=null;
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode(“新節點”);
newNode.setAllowsChildren(true);
TreePath parentPath=tree.getSelectionPath();
//取得新節點的父節點
parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());
//由DefaultTreeModel的insertNodeInto()方法增加新節點
treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount());
//tree的scrollPathToVisible()方法在使Tree會自動展開文件夾以便顯示所加入的新節點。若沒加這行則加入的新節點
//會被 包在文件夾中,你必須自行展開文件夾才看得到。
tree.scrollPathToVisible(new TreePath(newNode.getPath()));
label.setText(“新增節點成功”);
}
if (ae.getActionCommand().equals(“刪除節點”)){
TreePath treepath=tree.getSelectionPath();
if (treepath!=null){
//下面兩行取得選取節點的父節點.
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)treepath.getLastPathComponent();
TreeNode parent=(TreeNode)selectionNode.getParent();
if (parent!=null) {
//由DefaultTreeModel的removeNodeFromParent()方法刪除節點,包含它的子節點。
treeModel.removeNodeFromParent(selectionNode);
label.setText(“刪除節點成功”);
}
}
}
if (ae.getActionCommand().equals(“清除所有節點”)){
//下面一行,由DefaultTreeModel的getRoot()方法取得根節點.
DefaultMutableTreeNode rootNode=(DefaultMutableTreeNode)treeModel.getRoot();
//下面一行刪除所有子節點.
rootNode.removeAllChildren();
//刪除完後務必運行DefaultTreeModel的reload()操作,整個Tree的節點才會真正被刪除.
treeModel.reload();
label.setText(“清除所有節點成功”);
}
}
public void treeNodesChanged(TreeModelEvent e){
TreePath treePath=e.getTreePath();
DefaultMutableTreeNode node=(DefaultMutableTreeNode)treePath.getLastPathComponent();
try{
int[] index=e.getChildIndices();
node=(DefaultMutableTreeNode)node.getChildAt(index[0]);
}catch(NullPointerException exc){}
label.setText(nodeName+”更改數據為:”+(String)node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e){
System.out.println(“new node insered”);
}
public void treeNodesRemoved(TreeModelEvent e){
System.out.println(“node deleted”);
}
public void treeStructureChanged(TreeModelEvent e){
System.out.println(“Structrue changed”);
}
public static void main(String[] args){
new TreeTest();
}
class MouseHandle extends MouseAdapter{
public void mousePressed(MouseEvent e){
try{
JTree tree=(JTree)e.getSource();
int rowLocation=tree.getRowForLocation(e.getX(),e.getY());
TreePath treepath=tree.getPathForRow(rowLocation);
TreeNode treenode=(TreeNode)treepath.getLastPathComponent();
nodeName=treenode.toString();
}catch(NullPointerException ne){}
}
}
}
java w3c在指定位置添加節點
用下面的方法可以得到ID=2的child節點命名為child2,然後調用root.insertBefore(child3,child2);即可。
具體代碼如下:
Element root=doc.getDocumentElement();
Element child2=getChildElementByTagName(root,”child”,”ID”,”2″);
Element child3=doc.createElement(“child”);
root.insertBefore(child3,child2);
child3.setNodeValue(“new”);
public Element getChildElementByTagName(Element parent, String name,String attribute,String attrValue) {
Node node = parent.getFirstChild();
while(node != null) {
if(node.getNodeType() == Node.ELEMENT_NODE node.getNodeName().equals(name)){
Element n=(Element) node;
if(attrValue.equals(n.getAttribute(attribute)))return n;
}
node = node.getNextSibling();
}
return null;
}
java中的XML追加內容,追加節點,和節點的內容,還要常用的設計模式的代碼
/**
* 根據Xml文件生成Document對象
*
* @param file
* xml文件路徑
* @return Document對象
* @throws DocumentException
*/
public static Document getDocument(String path) throws DocumentException {
File file = new File(path);
SAXReader xmlReader = new SAXReader();
return xmlReader.read(file);
}
/**
* 根據輸入流生成Document對象
*
* @param is
* 輸入流
* @return Document對象
* @throws DocumentException
*/
public static Document getDocument(InputStream is) throws DocumentException {
SAXReader xmlReader = new SAXReader();
return xmlReader.read(is);
}
/**
* 根據Document得到根結點
*
* @param doc
* Document目錄
* @return 根結點
* @throws DocumentException
*/
public static Element getRoot(String path) throws DocumentException {
Document doc = getDocument(path);
return doc.getRootElement();
}
/**
* 取出當前結點下的所有子結點
*
* @param root
* 當前結點
* @return 一組Element
* @throws DocumentException
*/
@SuppressWarnings(“unchecked”)
public static ListElement getElements(String path)
throws DocumentException {
Element root = getRoot(path);
return root.elements();
}
/**
* 根據元素名稱返回一組Element
*
* @param root
* 當前結點
* @param name
* 要返回的元素名稱
* @return 一組Element
* @throws DocumentException
*/
@SuppressWarnings(“unchecked”)
public static ListElement getElementsByName(String path, String name)
throws DocumentException {
Element root = getRoot(path);
return root.elements(name);
}
/**
* 根據元素名稱返回一個元素(如果有多個元素的話,只返回第一個)
*
* @param root
* 當前結點
* @param name
* 要返回的元素名稱
* @return 一個Element元素
* @throws DocumentException
*/
public static Element getElementByName(String path, String name)
throws DocumentException {
Element root = getRoot(path);
return root.element(name);
}
/**
* 根據當前元素,返回該元素的所有屬性
*
* @param root
* 當前結點
* @return 當前結點的所有屬性
* @throws DocumentException
*/
@SuppressWarnings(“unchecked”)
public static ListAttribute getAttributes(String path)
throws DocumentException {
Element root = getRoot(path);
return root.attributes();
}
/**
* 根據屬性名稱,返回當前元素的某個屬性
*
* @param root
* 當前結點
* @return 當前結點的一個屬性
* @throws DocumentException
*/
public static Attribute getAttributeByName(String path, String name)
throws DocumentException {
Element root = getRoot(path);
return root.attribute(name);
}
public static ListElement getElementWithAttribute(String path,String attr,String value) throws DocumentException{
ListElement elist = new ArrayListElement() ;
ListElement list = getElements(path);
for(Element element:list){
if(element.attribute(attr).getText().equals(value)){
elist.add(element);
}
}
try {
elist.add(getElementSelf(path, attr, value));
} catch (Exception e) {
// TODO: handle exception
}
return elist;
}
public static Element getElementSelf(String path,String attr,String value) throws DocumentException{
Element element = null ;
ListElement list = getElements(path);
for(Element e:list){
if(e.attribute(attr).getText().equals(value)){
element = e ;
}
}
return element;
}
原創文章,作者:ULBUR,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/324747.html