JTree update

When me interface class is ran, the JTree is initially empty. When I click on a JButton, lets say its called 慡tart? a method is invoked that returns a String array from an external class (called Checks.java) back to a String array in my interface class. This String array is then used to create the objects in my JTree.

I know my JTree works fines. I抳e tested it by initialising the String array in the interface class with dummy data. I also know that my method for returning the array from the other class works ok, because I can print the values to the console. Still, I can抰 get my JTree to update itself. I抳e tried a few things that I抳e found in the forum, but nothing has worked so far.

I have posted this question elsewhere, but was told to post it hear instead and perhaps include my code. So, here it is. Please don't try to run it as I've taken out lots of irrelevant code to this question...

publicclass TreeDemoExpoextends JFrameimplements TreeSelectionListener, ActionListener

{

private JTree tree;//The JTree

private String [] guidelines01;//initialised by a method that returns values from another class

protected DefaultTreeModel treeModel;

public TreeDemoExpo(String title)

{

//create buttons

/*to see what the start button does, see below*/

start =new JButton("Start");

start.addActionListener(this);

start.setSize(100, 30);

start.setLocation(360,150);

add(start);

start.setEnabled(false);

//other buttons etc go here

//...

//Create the JTree and treeModel

DefaultMutableTreeNode top =new DefaultMutableTreeNode("Root Node");

treeModel =new DefaultTreeModel(top);

createNodes(top);

tree =new JTree(top);

treeModel.nodeStructureChanged(top);

tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

tree.setShowsRootHandles(true);

JScrollPane treeView =new JScrollPane(tree);

//Create the viewing pane.

htmlPane =new JEditorPane();

htmlPane.setEditable(false);

JScrollPane htmlView =new JScrollPane(htmlPane);

//create and add other objects to add to splitpanes

//...

}

//when selecting a different object from the tree, show the new objects values...

publicvoid valueChanged(TreeSelectionEvent e)

{

DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();

if (node ==null)return;

Object nodeInfo = node.getUserObject();

if (node.isLeaf())

{

ErrorInfo errorObject = (ErrorInfo)nodeInfo;//display an element from array 'guidelines01'

displayMSG(errorObject.information);//display a message

displayMSG2(errorObject.moreInformation);//display a message

}

}

privateclass ErrorInfo

{

public String bookName;

public String information;

public String moreInformation;

public ErrorInfo(String book, String detail, String moreDetail)

{

bookName = book;

information = detail;

moreInformation = moreDetail;

}

public String toString()

{

return bookName;

}

}

//methods displayMSG and displayMSG2 go here...

//the code that creates the nodes uner the root tree

privatevoid createNodes(DefaultMutableTreeNode top)

{

DefaultMutableTreeNode category =null;

DefaultMutableTreeNode book =null;

DefaultMutableTreeNode bookSun =null;

//create the category

category =new DefaultMutableTreeNode("Clear text for all");

top.add(category);

if(guidelines01 !=null)

{

book =new DefaultMutableTreeNode("12 point or more (14 ideal)");

category.add(book);

for(int i=0; i<guidelines01.length; i++)

{

String temp = guidelines01[i];//string ='s an element of the array

String lineNumber = check(temp);//the new string equals a modified version of string temp

//string lineNumber then becomes the first parameter for the new object

bookSun =new DefaultMutableTreeNode(new ErrorInfo(lineNumber, guidelines01[i]+" the offening code","to avoid this..."));

book.add(bookSun);

}

}

}

publicvoid actionPerformed(ActionEvent e)

{

if(e.getSource() == start)

{

/*when the start button is pressed, a new object of another class is created (Checks.java).

The class returns an array, via a method, to array 'guidelines01' as shown below*/

//create an object of the other class

Checks bee =new Checks(readAFile, inputFileName);

//initialise the array with a call to a method in class Checks

guidelines01 = bee.returnArray01();

/*each element of this array becomes part of the object created in the JTree. Look 23 lines above.

The problem is, the JTree doesn't become updated. This is where i need help.*/

}

}

publicstaticvoid main(String[] args)

{

//Create and set up the window.

JFrame frame =new TreeDemoExpo("A Title");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(620,420);

frame.setLocation(50, 50);

frame.setVisible(true);

}

}

>

[8763 byte] By [firellia] at [2007-10-3 3:09:41]
# 1
hi!have you tried with treeDidChange() of javax.swing.JTree try that after you update the nodes of JTree:)
Aniruddha-Herea at 2007-7-14 21:00:24 > top of Java-index,Desktop,Core GUI APIs...