469,328 Members | 1,247 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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 5276
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

Post your reply

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

Similar topics

2 posts views Thread by Mitch Williams | last post: by
reply views Thread by ward | last post: by
reply views Thread by luvping04 | last post: by
reply views Thread by flavourofbru | last post: by
2 posts views Thread by ace84 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.