Problem with DefaultMutableTreeNode
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, February 6, 2009 12:27 AM

Hi, I'm using DefaultMutableTreeNode to display items in tree format.The whole tree structure developed is going to be added to a JScrollPane and then to a JFrame finally. The JFrame also has a button, which when clicked will display the above said tree structure and after each click should referesh the tree contents. Here is the code: import javax.swing.*; import javax.swing.tree.*; import java.awt.*; import java.awt.event.*; public class UIForForum extends JApplet implements ActionListener { JFrame frameObj = new JFrame("Sample Frame"); JTree tree; DefaultMutableTreeNode root = new DefaultMutableTreeNode(&   More>>

Hi,
I'm using DefaultMutableTreeNode to display items in tree format.The whole tree structure developed is going to be added to a JScrollPane and then to a JFrame finally. The JFrame also has a button, which when clicked will display the above said tree structure and after each click should referesh the tree contents. Here is the code:


			
import javax.swing.*;

import javax.swing.tree.*;



import java.awt.*;

import java.awt.event.*;



public class UIForForum extends JApplet implements ActionListener {

JFrame frameObj = new JFrame("Sample Frame");

JTree tree;

DefaultMutableTreeNode root = new DefaultMutableTreeNode("RootNode");

static int buttonClicks = 0;

JPanel panelObj = new JPanel(); //to hold the tree



public static void main(String[] args) {

UIForForum uiObj = new UIForForum();

uiObj.uiGenerate();

}



public void uiGenerate() {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowUI();

}

});

}



public void createAndShowUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

frameObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frameObj.setSize(300, 450);

frameObj.setVisible(true);





JTextArea labelArea = new JTextArea("Press the "Get Information" button below.");

labelArea.setEditable(false);

labelArea.setFont(new Font("SansSerif", Font.BOLD, 13));

labelArea.setForeground(Color.DARK_GRAY);

labelArea.setLineWrap(true);



JButton infoButton = new JButton("Get Information");

infoButton.setBorder(BorderFactory.createLineBorder(Color.GRAY,6));

infoButton.setFont(new Font("Courier",Font.BOLD,20));



JPanel panel = new JPanel(new GridLayout(0,1));

panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));

panel.add(labelArea);

panel.add(infoButton);



infoButton.addActionListener(this);

frameObj.add("North",panel);

}



public void actionPerformed(ActionEvent e) {

buttonClicks++;

System.out.println("Button Click:"+buttonClicks);

if(buttonClicks > 1) {

System.out.println("In button click condition");

frameObj.remove(panelObj);

//generateFiles();

loadTree();

frameObj.validate();

}

else {

//generateFiles();

loadTree();

}

}



public void loadTree() {

int procCount = 2;

DefaultMutableTreeNode procItem = null;

DefaultMutableTreeNode procsInstalled = new DefaultMutableTreeNode("ProcessorsInstalled");

root.add(procsInstalled);

DefaultMutableTreeNode[] procItemNodes = {

new DefaultMutableTreeNode("DeviceID"),

new DefaultMutableTreeNode("Manufacturer"),

new DefaultMutableTreeNode("Name"),

new DefaultMutableTreeNode("Description"),

};

for(int i=0;i

procItem = new DefaultMutableTreeNode("ProcessorItem");

procsInstalled.add(procItem);

String[] procElements = {"abc","bcd","cde","efg"};

for(int j=0;j

procItem.add(procItemNodes[j]);

procItemNodes[j].add(new DefaultMutableTreeNode(procElements[j]));

}

}

tree = new JTree(root);

JScrollPane treeView = new JScrollPane(tree);

treeView.setPreferredSize(new Dimension(250,300));

panelObj.add(treeView);

frameObj.getContentPane().add(panelObj);

frameObj.validate();

}

}




And I'm expecting the output as follows:

1 RootNode

1.1 ProcessorsInstalled

1.1.1 ProcessorItem

1.1.1.a DeviceID

1.1.1.a.1 abc

1.1.1.b Manufacturer

1.1.1.b.1 bcd

1.1.1.c Name

1.1.1.c.1 cde

1.1.1.d Description

1.1.1.d.1 efg

1.1.2 ProcessorItem

1.1.2.a DeviceID

1.1.2.a.1 abc

1.1.2.b Manufacturer

1.1.2.b.1 bcd

1.1.2.c Name

1.1.2.c.1 cde

1.1.2.d Description

1.1.2.d.1 efg


But what i'm getting is like this:

1 RootNode

1.1 ProcessorsInstalled

1.1.1 ProcessorItem

1.1.2 ProcessorItem

1.1.2.a DeviceID

1.1.2.a.1 abc

1.1.2.a.2 abc

1.1.2.b Manufacturer

1.1.2.b.1 bcd

1.1.2.b.2 bcd

1.1.2.c Name

1.1.2.c.1 cde

1.1.2.c.2 cde

1.1.2.d Description

1.1.2.d.1 efg

1.1.2.d.2 efg


Can anyone help me please?
P.S: Numbers i have given to explain the tree structure.    <<Less
About | Sitemap | Contact