473,432 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,432 software developers and data experts.

Generate JTree with Data from XML files as input

50
Hi....

How to generate Tree with Data from XML as Input.

I use Netbeans and i tried generting a tree using DefaultMutableTree node and inputs mentioned in same pane like,

DefaultMutableTreeNode classname1=new DefaultMutableTreeNode("CLASSNAME1");

But netbeans displays only the empty pane and no tree...

How to solve the two problems...

Thank you...
Mar 29 '09 #1
21 5737
JosAH
11,448 Expert 8TB
Did you do something like the following in your JFrame?

Expand|Select|Wrap|Line Numbers
  1. this.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
  2.  
You do have to hookup your JTree to its parent component.

kind regards,

Jos
Mar 29 '09 #2
uhdam
50
Yes...

I had a panelname whose contents are dislayed in contentpane.

getContentPane().add(panelname);

scrollpane.getViewport().add(treename);

panelname.add(scrollpane,BorderLayout.CENTER);

//scrollpane is name initialised for JScrollPane and
//panelname for JPanel
Mar 29 '09 #3
JosAH
11,448 Expert 8TB
@uhdam
Try to do it this way:

Expand|Select|Wrap|Line Numbers
  1. scrollpane.setViewportView(treename);
  2.  
kind regards,

Jos
Mar 29 '09 #4
uhdam
50
HI..

Still i get only an empty frame as output. The tree is not displayed.
Mar 29 '09 #5
JosAH
11,448 Expert 8TB
@uhdam
Show us a bit of relevant code: remove everything that is not directly relevant to the creation and display of your tree and produce a short, compilable example.

kind regards,

Jos
Mar 29 '09 #6
uhdam
50
package name;
import stmts;

public class Classname extends JFrame {

private JPanel topPanel;
private JTree tree1;
private JScrollPane scrollpane;

public Classname {
initComponents();

setTitle(" Classes Trial ");
setSize(400,500);
setBackground(Color.gray);

topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);

DefaultMutableTreeNode className1 = new DefaultMutableTreeNode("TreeRoot");

DefaultMutableTreeNode Methods1 = new DefaultMutableTreeNode("Methods1");
allMethods(Methods1);
className1.add(Methods1);

DefaultMutableTreeNode Attributes1 = new DefaultMutableTreeNode("Attributes1");
className1.add(Attributes1);

DefaultTreeModel treeModel1=new DefaultTreeModel(TreeRoot);
tree1=new JTree(treeModel1);

scrollpane = new JScrollPane();
scrollpane.getViewport().add(tree1);
topPanel.add(scrollpane1,BorderLayout.BEFORE_FIRST _LINE);

public void allMethods(DefaultMutableTreeNode mthd)
{
mthd.add(new DefaultMutableTreeNode("MD1"));
mthd.add(new DefaultMutableTreeNode("MD2"));
mthd.add(new DefaultMutableTreeNode("MD3"));
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Classname().setVisible(true);
}
});
}
Mar 29 '09 #7
JosAH
11,448 Expert 8TB
Your example doesn't compile: what is TreeRoot?

kind regards,

Jos
Mar 29 '09 #8
uhdam
50
its classname.tree rootnode.

Replace classname with ClassDisp and TreeRoot with Classname1;

sorry for the inconvenience..

Thankyou
Mar 29 '09 #9
uhdam
50
i worked in netbeans.. it generates some code automatically...

the initcomponents will be that way na..


/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents


should you include initcomponents code which is generated by the netbeans itself.
Mar 29 '09 #10
JosAH
11,448 Expert 8TB
@uhdam
I'd say get rid of all that crap and write the initialization stuff yourself; if you don't know what that Netbeans stuff does why use it? The general outline of your program makes sense: knit the DefaultMutableTreeNodes together and pass the root to a DefaultTreeModel; pass that model to your JTree and voila.

If you read the API documentation for the JTree class you find a link to the tutorials; follow the tutorials and you're fine.

kind regards,

Jos

ps. next time when you post an example make it as short as possible and take care that it compiles.
Mar 29 '09 #11
uhdam
50
Ok.. It compiled for me but the problem is that it displays only a blank pane. The same code i tried working in Notepad i mean java compiler and it worked perfectly well.

Thats why i couldnt understand why it doesnt get displayed here ?
Mar 29 '09 #12
JosAH
11,448 Expert 8TB
@uhdam
If most certainly didn't compile here: that TreeRoot name is undefined so the compiler complained, gave up, and didn't generate a .class file. Are you saying it did compile for you (just using javac) and you were able to run it? If so, the code you showed us wasn't the code you used yourself. Those artifacts make it impossible for us to help you properly; help us to help you.

kind regards,

Jos
Mar 29 '09 #13
uhdam
50
No sir...

That was the code i used... Any ways will verify and sned u the code which displayed properly. The change which i made while working in netbeans was that i used 4 trees to be generated using javac and only one tree for here for knowing whether it gets displayed.

will post the code..

thank you...
Mar 29 '09 #14
uhdam
50
This is the code which i used in javac.. and when sure that it worked.. i tried just for one tree in netbeans...

//imports

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

class UserDefined extends JFrame
{

//instance attributes used in this example
private JPanel topPanel1;
private JTree tree1;
private JTree tree2;
private JTree tree3;
private JTree tree4;
private JScrollPane scrollpane1;


//construction of main frame
public UserDefined()
{

//set frame characteristics

setTitle("User Defined Classes Trial ");
setSize(400,500);
setBackground(Color.gray);

//Create a panel to hold all other components
topPanel1 = new JPanel();
topPanel1.setLayout(new BorderLayout());
getContentPane().add(topPanel1);

//create a new tree control tree1
DefaultMutableTreeNode className1 = new DefaultMutableTreeNode("ClassName1");

DefaultMutableTreeNode Methods1 = new DefaultMutableTreeNode("Methods1");
allMethods(Methods1);
className1.add(Methods1);

DefaultMutableTreeNode Attributes1 = new DefaultMutableTreeNode("Attributes1");
//AllAttributes(Attributes1);
className1.add(Attributes1);


//create a new tree control tree2
DefaultMutableTreeNode className2 = new DefaultMutableTreeNode("ClassName2");

DefaultMutableTreeNode Methods2 = new DefaultMutableTreeNode("Methods2");
className2.add(Methods2);

DefaultMutableTreeNode Attributes2 = new DefaultMutableTreeNode("Attributes2");
className2.add(Attributes2);


//TREE3
DefaultMutableTreeNode className3 = new DefaultMutableTreeNode("ClassName3");

DefaultMutableTreeNode Methods3 = new DefaultMutableTreeNode("Methods3");
className3.add(Methods3);


//Tree 4
DefaultMutableTreeNode className4 = new DefaultMutableTreeNode("ClassName4");

DefaultMutableTreeNode Attributes4 = new DefaultMutableTreeNode("Attributes4");
className4.add(Attributes4);


//create a new tree control
DefaultTreeModel treeModel1=new DefaultTreeModel(className1);
tree1=new JTree(treeModel1);


DefaultTreeModel treeModel2=new DefaultTreeModel(className2);
tree2=new JTree(treeModel2);

DefaultTreeModel treeModel3=new DefaultTreeModel(className3);
tree3=new JTree(treeModel3);

DefaultTreeModel treeModel4=new DefaultTreeModel(className4);
tree4=new JTree(treeModel4);


//Add the listbox to a scrolling pane

scrollpane1 = new JScrollPane();
scrollpane1.getViewport().add(tree1);
topPanel1.add(scrollpane1,BorderLayout.BEFORE_FIRS T_LINE);


scrollpane1 = new JScrollPane();
scrollpane1.getViewport().add(tree2);
topPanel1.add(scrollpane1,BorderLayout.CENTER);


scrollpane1 = new JScrollPane();
scrollpane1.getViewport().add(tree3);
topPanel1.add(scrollpane1,BorderLayout.LINE_START) ;


scrollpane1 = new JScrollPane();
scrollpane1.getViewport().add(tree4);
topPanel1.add(scrollpane1,BorderLayout.AFTER_LINE_ ENDS);

}


//Helper method to write an entire suit of cards to the current tree node

public void allMethods(DefaultMutableTreeNode mthd)
{
mthd.add(new DefaultMutableTreeNode("MD1"));
mthd.add(new DefaultMutableTreeNode("MD2"));
mthd.add(new DefaultMutableTreeNode("MD3"));
}


//Main entry point for this example

public static void main(String args[])
{
//create the instance of the test application
UserDefined mainFrame = new UserDefined();
mainFrame.setVisible(true);
}

}

It didnt work in netbeans for me....

It displays just the empty pane...
Mar 29 '09 #15
JosAH
11,448 Expert 8TB
@uhdam
This code compiled for me too (I use Eclipse). If it doesn't work in Netbeans I bet you've been fiddling with the generated GUI code. Never trust generated code and even better: never even generate GUI code, it can't be trusted.

kind regards,

Jos

ps. It ran fine for me too, i.e. the JTrees were displayed.
Mar 29 '09 #16
uhdam
50
It works in netbeans if the generated code is commented.

I cant move to eclipse now because some code is developed in netbeans.. How do i generate tree using data stored in XML file as input..

Kindly help me with that...

Thank you..
Mar 29 '09 #17
JosAH
11,448 Expert 8TB
@uhdam
Why didn't you tell us that when you asked your question? Now, after 18 replies stuff seems to work if you comment out Netbean's crap. Everybody want clear answers in this forum; does it surprise you if we want clear questions asked?

@uhdam
There's nothing wrong with Netbeans as long as you don't let it generate GUI code for you; all GUI generators are pure crap imho.

w.r.t. your question: you have to parse your XML text and construct your tree accordingly; you effectively build a DOM out of your XML text. A SAX parser can be of help here and a DefaultHandler that is called back from your SAX parser. The handler harvests the individual nodes of the tree to your tree builder and does a bit of bookkeeping.

kind regards,

Jos
Mar 29 '09 #18
uhdam
50
sorry for this much replies. But i never thought that the generated code only would have caused the problem... I am sorry for that..

And i have generated a DTD and used DOM parser. where should the parsed information be given in this code.
Mar 29 '09 #19
JosAH
11,448 Expert 8TB
@uhdam
You have to implement your own ContentHandler (it's an interface) that builds your DefaultMutableTreeNodes for you.

kind regards,

Jos
Mar 29 '09 #20
uhdam
50
I get the output in console. I need to get as a tree. That is the need now.
Mar 29 '09 #21
JosAH
11,448 Expert 8TB
@uhdam
Do you use System.out.println( ... ) yourself? If so, don't print but construct your tree instead. If not, what are doing then?

kind regards,

Jos
Mar 29 '09 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mitch Williams | last post by:
I've been working with a JTree based on a custom TreeModel. Unfortunatly, I cant figure out how to force it to update the entire tree-- there simply dosnt seem to be a way to say "OK, your data...
2
by: Piet | last post by:
Hi there, I have managed to write a small java program that display an xml file in a JTree. This was achieved by defining a suitable tree model. THe program works...somehow. The nodes (elements...
0
by: Lucia | last post by:
hello, I habe a Java Program that displays the XML document in a Java JTree using DOM. Now I can add, delete and edit the JTree Elements. But I don't know how to show the attributes also in the...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
0
by: luvping04 | last post by:
Hey, i told to make a java program to load xml into JTREE using SAX api. i'm suppose to load the 1st layer of the jtree and hide the subtree into memory. How can i load the subtree into...
0
by: flavourofbru | last post by:
Hi, I am converting a given html document into a tree structure and displaying it using JTree and DefaultMutable classes. By using the html parser and the JTree functionality, I can display the...
2
by: msali36 | last post by:
Hi, i just want to know how can i display the parent- child information in hirarchy tree format in vb.net. for example : GrantParent parent ...
2
by: ace84 | last post by:
Hi, I am creating an application where I want to show a file system. I have never worked with them before, so i am not sure on what I need to do to accomplish my goal. So far I found a script through...
1
by: sivakrish85 | last post by:
Hi, this is krishna i am on developing FTP in java.Here i display the server Files and client Files in tree view using JTree.So my question is that once the client send the file to...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.