<html>
<body>
Try removing all the GUI/Swing stuff that has nothing to do with JDOM,
and focus on a simplified example. Maybe you can figure it out yourself
then (looking at JDOMs JavaDoc), or post the minimized example here for
further help.<br><br>
At 2006-05-18 16:51, cs_student wrote:<br>
<blockquote type=cite class=cite cite="">Hi,<br>
I need for a help ..I don't know how to add an element to the xml
file, delete from the file or edite name of any element..I'm using
JBuilder..as you know that dealing with it is so hard!<br>
<br>
this is my code<br>
package fina;<br>
import java.awt.*;<br>
import javax.swing.*;<br>
import javax.swing.tree.*;<br>
import javax.swing.event.*;<br>
import java.util.Iterator;<br>
import java.util.List;<br>
import org.jdom.*;<br>
import org.jdom.input.SAXBuilder;<br>
import java.io.File;<br>
import java.awt.event.*;<br>
import javax.swing.event.TreeSelectionEvent;<br>
import javax.swing.tree.DefaultMutableTreeNode;<br>
public class XMLTreeViewer extends JFrame<br>
implements TreeSelectionListener {<br>
//The JTree to display the XML<br>
private JTree xmlTree;<br>
//The XML document to be output to the JTree<br>
private Document xmlDoc;<br>
DefaultMutableTreeNode tn;<br>
JButton AddButton = new JButton();<br>
JTextField TextField = new JTextField();<br>
JTextArea TextArea = new JTextArea();<br>
<br>
public XMLTreeViewer(Document doc) {<br>
super();<br>
// WindowUtilities.setNativeLookAndFeel();<br>
addWindowListener(new ExitListener());<br>
this.xmlDoc = doc;<br>
setSize(600, 450);<br>
tn = new DefaultMutableTreeNode("XML");<br>
initialize();<br>
}<br>
private void initialize() {<br>
xmlTree = new JTree();<br>
xmlTree.setEditable(true);<br>
AddButton.setBackground(SystemColor.inactiveCaptionText);<br>
AddButton.setForeground(Color.black);<br>
AddButton.setText("ADD");<br>
AddButton.addMouseListener(new
java.awt.event.MouseAdapter() {<br>
public void mouseClicked(MouseEvent e)
{<br>
AddButton_mouseClicked(e);<br>
}<br>
});<br><br>
TextField.setText("");<br>
TextArea.setText("jTextArea1");<br>
getContentPane().add(new JScrollPane(xmlTree),
BorderLayout.CENTER);<br>
this.getContentPane().add(TextField,
BorderLayout.NORTH);<br>
this.getContentPane().add(AddButton,
BorderLayout.SOUTH);<br>
this.getContentPane().add(TextArea,
BorderLayout.EAST);<br>
<br>
processElement(xmlDoc.getRootElement(), tn);<br>
( (DefaultTreeModel) xmlTree.getModel()).setRoot(tn);<br>
addWindowListener(new java.awt.event.WindowAdapter() {<br>
public void windowClosing(java.awt.event.WindowEvent
e) {<br>
//release all the resource<br>
xmlTree = null;<br>
tn = null;<br>
}<br>
});<br>
setVisible(true);<br>
//add Listener to Print to Screen the xml tag selected\<br>
xmlTree.addTreeSelectionListener(this);<br>
}<br>
public void valueChanged(TreeSelectionEvent evt) {<br>
// Get all nodes whose selection status has changed<br>
TreePath[] paths = evt.getPaths();<br>
// Print the last Path Component
selected<br>
System.out.println(evt.getPath().getLastPathComponent());<br>
//print the full path from the selected tag<br>
System.out.println(evt.getPath().toString());<br>
}<br><br>
private void processElement(Element el, DefaultMutableTreeNode dmtn)
{<br>
DefaultMutableTreeNode currentNode =<br>
new
DefaultMutableTreeNode(el.getName());<br>
String text = el.getTextNormalize();<br>
if ( (text != null) && (!text.equals("")))
{<br>
currentNode.add(new
DefaultMutableTreeNode(text));<br>
}<br>
Iterator children = el.getChildren().iterator();<br>
while (children.hasNext()) {<br>
processElement( (Element) children.next(),
currentNode);<br>
}<br>
dmtn.add(currentNode);<br>
}<br><br>
public static void main(String[] args)throws Exception {<br>
SAXBuilder builder = new SAXBuilder();<br>
Document doc =
builder.build("comp.xml");<br>
XMLTreeViewer viewer = new XMLTreeViewer(doc);<br>
viewer.addWindowListener(new
java.awt.event.WindowAdapter() {<br>
public void
windowClosing(java.awt.event.WindowEvent e)<br>
{System.exit(0);}<br>
});<br>
}<br>
void AddButton_mouseClicked(MouseEvent e) {<br>
TextArea.setSize(20,20);<br>
TextArea.setText("An element will be\n
added..");<br>
TextField.getText();<br>
Object o=xmlTree.getLastSelectedPathComponent();<br>
addObject(o);<br>
// String
s=xmlTree.getLastSelectedPathComponent().toString();<br><br>
}<br>
public void addObject(Object child) {<br>
DefaultMutableTreeNode parentNode=null;<br>
TreePath parentPath=xmlTree.getSelectionPath();<br>
if(parentPath!=null){<br>
parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());<br>
addObject(parentNode, child, true);<br>
}<br>
}<br>
public void addObject(DefaultMutableTreeNode parentNode, Object
child,<br>
boolean b) {<br>
}<br>
}<br>
//---------------------------------------<br>
package fina;<br>
import javax.swing.*;<br>
import java.awt.*;<br>
/** A few utilities that simplify using windows in Swing.<br>
* 1998-99 Marty Hall,
<a href="http://www.apl.jhu.edu/~hall/java/">
http://www.apl.jhu.edu/~hall/java/</a><br>
*/<br>
public class WindowUtilities {<br>
/** Tell system to use native look and feel, as in previous<br>
* releases. Metal (Java) LAF is the default
otherwise.<br>
*/<br>
public static void setNativeLookAndFeel() {<br>
try {<br>
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());<br>
} catch(Exception e) {<br>
System.out.println("Error setting
native LAF: " + e);<br>
}<br>
}<br>
public static void setJavaLookAndFeel() {<br>
try {<br>
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());<br>
} catch(Exception e) {<br>
System.out.println("Error setting
Java LAF: " + e);<br>
}<br>
}<br>
public static void setMotifLookAndFeel() {<br>
try {<br>
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");<br>
} catch(Exception e) {<br>
System.out.println("Error setting
Motif LAF: " + e);<br>
}<br>
}<br>
/** A simplified way to see a JPanel or other Container.<br>
* Pops up a JFrame with specified Container as the
content pane.<br>
*/<br>
public static JFrame openInJFrame(Container content,<br>
int width,<br>
int height,<br>
String title,<br>
Color bgColor) {<br>
JFrame frame = new JFrame(title);<br>
frame.setBackground(bgColor);<br>
content.setBackground(bgColor);<br>
frame.setSize(width, height);<br>
frame.setContentPane(content);<br>
frame.addWindowListener(new ExitListener());<br>
frame.setVisible(true);<br>
return(frame);<br>
}<br>
/** Uses Color.white as the background color. */<br>
public static JFrame openInJFrame(Container content,<br>
int width,<br>
int height,<br>
String title) {<br>
return(openInJFrame(content, width, height, title,
Color.white));<br>
}<br>
/** Uses Color.white as the background color, and the<br>
* name of the Container's class as the JFrame
title.<br>
*/<br>
public static JFrame openInJFrame(Container content,<br>
int width,<br>
int height) {<br>
return(openInJFrame(content, width, height,<br>
content.getClass().getName(),<br>
Color.white));<br>
}<br>
}<br>
//----------------------------------------<br>
package fina;<br>
import java.awt.*;<br>
import java.awt.event.*;<br>
/** A listener that you attach to the top-level Frame or JFrame of<br>
* your application, so quitting the frame exits the
application.<br>
* 1998-99 Marty Hall,
<a href="http://www.apl.jhu.edu/~hall/java/">
http://www.apl.jhu.edu/~hall/java/</a><br>
*/<br>
public class ExitListener extends WindowAdapter {<br>
public void windowClosing(WindowEvent event) {<br>
System.exit(0);<br>
}<br>
}<br>
thank you ..<br>
Bassam</blockquote></body>
</html>