java追加节点方法(jquery追加节点)

本文目录一览:

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/n/324747.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
ULBURULBUR
上一篇 2025-01-13 13:23
下一篇 2025-01-13 13:23

相关推荐

  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • java client.getacsresponse 编译报错解决方法

    java client.getacsresponse 编译报错是Java编程过程中常见的错误,常见的原因是代码的语法错误、类库依赖问题和编译环境的配置问题。下面将从多个方面进行分析…

    编程 2025-04-29
  • Java腾讯云音视频对接

    本文旨在从多个方面详细阐述Java腾讯云音视频对接,提供完整的代码示例。 一、腾讯云音视频介绍 腾讯云音视频服务(Cloud Tencent Real-Time Communica…

    编程 2025-04-29
  • Java Bean加载过程

    Java Bean加载过程涉及到类加载器、反射机制和Java虚拟机的执行过程。在本文中,将从这三个方面详细阐述Java Bean加载的过程。 一、类加载器 类加载器是Java虚拟机…

    编程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介绍

    本文将详细介绍Java Milvus SearchParam withoutFields的相关知识和用法。 一、什么是Java Milvus SearchParam without…

    编程 2025-04-29
  • ArcGIS更改标注位置为中心的方法

    本篇文章将从多个方面详细阐述如何在ArcGIS中更改标注位置为中心。让我们一步步来看。 一、禁止标注智能调整 在ArcMap中设置标注智能调整可以自动将标注位置调整到最佳显示位置。…

    编程 2025-04-29
  • 解决.net 6.0运行闪退的方法

    如果你正在使用.net 6.0开发应用程序,可能会遇到程序闪退的情况。这篇文章将从多个方面为你解决这个问题。 一、代码问题 代码问题是导致.net 6.0程序闪退的主要原因之一。首…

    编程 2025-04-29
  • Python中init方法的作用及使用方法

    Python中的init方法是一个类的构造函数,在创建对象时被调用。在本篇文章中,我们将从多个方面详细讨论init方法的作用,使用方法以及注意点。 一、定义init方法 在Pyth…

    编程 2025-04-29
  • Python创建分配内存的方法

    在python中,我们常常需要创建并分配内存来存储数据。不同的类型和数据结构可能需要不同的方法来分配内存。本文将从多个方面介绍Python创建分配内存的方法,包括列表、元组、字典、…

    编程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java语言中的一个版本,于2014年3月18日发布。本文将从多个方面对Java 8中某一周的周一进行详细的阐述。 一、数组处理 Java 8新特性之一是Stream…

    编程 2025-04-29

发表回复

登录后才能评论