Loadable Tree Model from drop down list.

Any idea on how to load a tree model onto a frame from a drowdown list. Now these will be in xml format. So I need load the xml files into a list. then when I drop down to a certain xml doc build the tree?This is a splitPane tree on left, and panels on right.
[273 byte] By [orozcoma] at [2007-11-26 13:02:07]
# 1
Same way you would build a tree normally.Please ask a specific question. http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
zadoka at 2007-7-7 17:04:43 > top of Java-index,Desktop,Core GUI APIs...
# 2

I thought I was pretty clear. I guess not. Ok, let me show you what I mean.

Lets say I have two xml files.

Test.xml, and Test2.xml

They are structured about the same way except that they have different data.

I would like to create a tree out of this xml doc.

so now I have a drop down menu with Test.xml, and Test2.xml in my frame. when I click on Test2.xml a JTree is automatically created in the left hand side of my JSplitPane.

I even have some code that sort of does this. Here it is:

import java.awt.*;

import java.awt.event.*;

import java.util.List;

import javax.swing.*;

import javax.swing.tree.*;

import javax.swing.event.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;

/**

*

* @author orozcom

*/

public class XMLTreeDemo extends JFrame

{

XMLTreeDemo(String title)

{

super(title);

try

{

DocumentBuilderFactory IDocumentBuilderFactory

= DocumentBuilderFactory.newInstance();

DocumentBuilder IDocumentBuilder

= IDocumentBuilderFactory.newDocumentBuilder();

//Document IDocument = IDocumentBuilder.parse("c:/test1.xml");

Document IDocument = IDocumentBuilder.parse("c:/test1.xml");

Node root = IDocument.getDocumentElement();

XmlTreeModel model = new XmlTreeModel(root);

JTree IJTree = new JTree();

IJTree.setModel(model);

getContentPane().add(new JScrollPane(IJTree),BorderLayout.CENTER);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

catch (Exception e)

{

System.err.println(e);

}

}

public static void main(String[] args)

{

XMLTreeDemo IJTreeDemo = new XMLTreeDemo("Xml tree demo");

IJTreeDemo.pack();

IJTreeDemo.setVisible(true);

}

}

class XmlTreeModel implements TreeModel

{

protected Node root;

public XmlTreeModel(Node root)

{

this.root = root;

}

public Object getRoot()

{

return (Object)this.root;

}

public boolean isLeaf(Object node)

{

if ((((Node)node).getNodeType() == 7) || (((Node)node).getNodeType()

== 1)) return false;

return true;

}

public int getChildCount(Object parent)

{

return ((Node)parent).getChildNodes().getLength();

}

public Object getChild(Object parent,int index)

{

Node child = ((Node)parent).getChildNodes().item(index);

return (Object)child;

}

public int getIndexOfChild(Object parent, Object child)

{

NodeList childs = ((Node)parent).getChildNodes();

if (childs.getLength() == 0) return -1;

for (int i=0; i<childs.getLength(); i++)

{

if (childs.item(i) == (Node)child) return i;

}

return -1;

}

public void valueForPathChanged(TreePath path, Object newValue)

{

}

public void addTreeModelListener(TreeModelListener l)

{

}

public void removeTreeModelListener(TreeModelListener l)

{

}

}

It isn't the greatest, but with a little modification I think it will work pretty good.

Here is one of the xml docs.

><TSConfig _CfgName="test" _CfgDescription="Test CLX Configuration" _ScnrVer="0.19">

- <DrvInst _DrvLib="CLXBP" _DrvLibVer="0.00" _DrvName="Drv1" ModuleFamily="2" Path="p:1,s:0" RspToMs="5000">

- <StructDef _StructName="TIMER" _ExtType="F83" _EleSize="40">

<StructMbr _MbrName="CTL" _MbrDaOfs="0" _BaseType="BYTE_BOOL" _ExtType="C1" _EleSize="1" _xDim="32" />

<StructMbr _MbrName="PRE" _MbrDaOfs="32" _BaseType="INT32" _ExtType="C4" _EleSize="4" />

<StructMbr _MbrName="ACC" _MbrDaOfs="36" _BaseType="INT32" _ExtType="C4" _EleSize="4" />

</StructDef>

<TagInst _TagName="Test_INT" _BaseType="INT16" _ExtType="C3" _EleSize="2" />

<TagInst _TagName="Test_DINT" _BaseType="INT32" _ExtType="C4" _EleSize="4" />

<TagInst _TagName="autoFillTag" _BaseType="INT16" _ExtType="C3" _EleSize="2" />

<TagInst _TagName="AutoFillTest_2" _BaseType="INT32" _ExtType="C4" _EleSize="4" />

<TagInst _TagName="Test_REAL" _BaseType="FLOAT32" _ExtType="CA" _EleSize="4" />

<TagInst _TagName="Test_SINT" _BaseType="INT8" _ExtType="C2" _EleSize="1" />

<TagInst _TagName="MVI_RecvMsgData" _BaseType="INT8" _ExtType="C2" _EleSize="1" _xDim="500" />

<TagInst _TagName="Timer1" _BaseType="STRUCT" _ExtType="F83" _EleSize="40" />

</DrvInst>

- <DrvInst _DrvLib="CLXBP" _DrvLibVer="0.00" _DrvName="Logix5563" ModuleFamily="2" Path="p:1,s:5" RspToMs="5000">

<TagInst _TagName="dummy1" _BaseType="INT16" _ExtType="C3" _EleSize="2" />

</DrvInst>

- <Trigger _TrigName="Trig1" _Type="Poll" _Period="1000" _Variable="1" _Condition="NEQ" _Reference="0">

<Action _TLName="RunEvery1000mS" />

<Action _TLName="RunEvery100mS" />

</Trigger>

- <TransferList _TLName="RunEvery1000mS">

<Transfer _XferId="1" _Dest="Drv1\Test_INT" _Source="1234" />

<Transfer _XferId="2" _Dest="Drv1\Test_DINT" _Source="12345678" />

<Transfer _XferId="3" _Dest="Drv1\Test_DINT" _Source="Drv1\Test_INT" />

<Transfer _XferId="4" _Source="56" _Dest="Drv1\Test_SINT" />

</TransferList>

- <Trigger _TrigName="Trig2" _Type="Poll" _Period="500" _Variable="1" _Condition="NEQ" _Reference="0">

<Action _TLName="RunEvery500mS" />

</Trigger>

<TransferList _TLName="RunEvery500mS" />

<TransferList _TLName="RunEvery5000mS" />

<TransferList _TLName="RunEvery100mS" />

<TransferList _TLName="RunEvery200mS" />

- <Trigger _TrigName="Trig3" _Type="Poll" _Period="5000" _Variable="1" _Condition="NEQ" _Reference="0">

<Action _TLName="RunEvery200mS" />

</Trigger>

- <DrvInst _DrvLib="CLXBP" _DrvLibVer="0.00" _DrvName="ENET" ModuleFamily="1" Path="p:1,s:2" RspToMs="5000">

- <DevInst _DevName="PLC5_40E" ModuleFamily="3" Path="p:1,s:2,p:2,t:10.0.104.105" RspToMs="5000">

<TagInst _TagName="n7_0" _BaseType="INT16" RegName="N7:0" xDim="0" _EleSize="2" />

</DevInst>

</DrvInst>

</TSConfig>

orozcoma at 2007-7-7 17:04:43 > top of Java-index,Desktop,Core GUI APIs...
# 3
> I thought I was pretty clear. I guess not. Ok, let me> show you what I mean.My comment is unchanged: What is your specific question?
zadoka at 2007-7-7 17:04:43 > top of Java-index,Desktop,Core GUI APIs...
# 4
question: Add xml's to Dropdown list. When xml is selected. Tree is created.I hope that question is clear enough
orozcoma at 2007-7-7 17:04:43 > top of Java-index,Desktop,Core GUI APIs...
# 5
So your question is really about how to use a combo box? You can find that out here: http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html
JayDSa at 2007-7-7 17:04:43 > top of Java-index,Desktop,Core GUI APIs...