java JTree not updating
I am having some issues with JTree. When I create my DefaultTreeModel within the same class, the JFrame updated fine. However, I want to call the update method from another class and when I do the JTree remains blank. The JTree is in the class GUI and stored like so in another class called store.
public static GUI UI = new GUI();
...
UI.init();
UI.buildJTree(xml);
However, this does nothing. I however inside the class GUI I call:
buildJTree(xml);
Everything works fine. Does anyone have any ideas as to why there might be a problem?
Build Tree method is as follows:
public void buildTree(String xml) {
try {
Console.output("Building tree");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(XML.wrap(xml.trim())));
Document doc = db.parse(is);
NodeList xml_packet = doc.getElementsByTagName("packet");
NodeList channels = xml_packet.item(0).getChildNodes().item(0).getChildNodes();
Element start = (Element) xml_packet.item(0).getFirstChild();
this.status_txt.setText(start.getAttribute("connected") + ", connected.");
MutableTreeNode root = new DefaultMutableTreeNode(start.getAttribute("server"));
for (int n = 0; n < channels.getLength(); n++) {
Element fstElmnt = (Element) channels.item(n);
Console.output("CHANNEL::" + fstElmnt.getAttribute("name"));
DefaultMutableTreeNode userss = new DefaultMutableTreeNode(fstElmnt.getAttribute("name"));
root.insert(userss, n);
NodeLi开发者_如何学JAVAst users = channels.item(n).getFirstChild().getChildNodes();
for (int usr = 0; usr < users.getLength(); usr++) {
userss.insert(new DefaultMutableTreeNode(users.item(usr).getFirstChild().getNodeValue()), usr);
}
}
model = new DefaultTreeModel(root);
this.trees.setModel(model);
for (int t = 0; t < this.trees.getRowCount(); t++) {
this.trees.expandRow(t);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Many Thanks in advance.
There's too little information in your question to be sure, but it's possible that you're running afoul of the requirement that all Swing-mutating work must be done in the event thread.
So, for example:
final Runnable swingTask = new Runnable() {
public void run() {
UI.init();
UI.buildJTree(xml);
}
};
if (EventQueue.isDispatchThread()) {
swingTask.run();
} else {
EventQueue.invokeAndWait(swingTask);
}
精彩评论