How to re-draw a jTree, from a Thread object?
I am using a jTree
structure to display the structure of the project the user is currently using. Users can, of course, open another project, and then, the software is supposed to display the new project's structure. I am using DefaultTreeModel
to add/delete nodes, and updating the jTree
structure. And since this task can take seconds to be achieved, it was implemented in a new Thread
. Here's the code :
class DrawTreeThread extends Thread {
@Override
public void run() {
System.out.println("drawing the tree");
model = (DefaultTreeModel) jTree1.getModel();
Fichier obs,
open,
opens = new Fichier(Batiment.data.getAbsolutePath() + "\\Open"),
obss = new Fichier(Batiment.data.getAbsolutePath() + "\\Obs"),
rooms = new Fichier(Batiment.data.getAbsolutePath() + "\\Rooms");
Fichier bd = new Fichier(Batiment.data.getAbsolutePath() + "\\BuildingData" + Fichier.EXT);
int size = rooms.listFiles().length;
int nbrEtage = bd.readInt("etage");
DefaultMutableTreeNode[] roomz = new DefaultMutableTreeNode[size];
DefaultMutableTreeNode bat = new DefaultMutableTreeNode(Batiment.data.getAbsoluteName());
DefaultMutableTreeNode[] obstacles = new DefaultMutableTreeNode[size];
DefaultMutableTreeNode[] ouvertures = new DefaultMutableTreeNode[size];
DefaultMutableTreeNode[] etages = new DefaultMutableTreeNode[nbrEtage];
jTree1 = new JTree(bat);
for (int etageCounter = 0; etageCounter < nbrEtage; etageCounter++) {
String name = "RDC";
if (etageCounter != 0) {
name += " + " + etageCounter;
}
etages[etageCounter] = new DefaultMutableTreeNode(name);
bat.add(etages[etageCounter]);
model.insertNodeInto(etages[etageCounter], bat, 0);
}
for (int i = 0; i < size; i++) {
roomz[i] = new DefaultMutableTreeNode(rooms.listFiles()[i].getAbsoluteName());
obs = new Fichier(obss.getAbsolutePath() + "\\" + rooms.listFiles()[i].getAbsoluteName());
open = new Fichier(opens.getAbsolutePath() + "\\" + rooms.listFiles()[i].getAbsoluteName());
etages[rooms.listFiles()[i].readInt("etage")].add(roomz[i]);
obstacles[i] = new DefaultMutableTreeNode("Obstacles (" + obs.listFiles().length + ")");
ouvertures[i] = new DefaultMutableTreeNode("Ouvertures (" + open.listFiles().length + ")");
roomz[i].add(obstacles[i]);
roomz[i].add(ouvertures[i]);
DefaultMutableTreeNode[] Obs = new DefaultMutableTreeNode[obs.listFiles().length];
for (int j = 0; j < obs.listFiles().length; j++) {
Obs[j] = new DefaultMutableTreeNode(obs.listFiles()[j].getAbsoluteName());
obstacles[i].add(Obs[j]);
}
DefaultMutableTreeNode[] Open = new DefaultMutableTreeNode[open.listFiles().length];
for (int j = 0; j < open.listFiles().length; j++) {
Open[j] = new DefaultMutableTreeNode(open.lis开发者_开发问答tFiles()[j].getAbsoluteName());
ouvertures[i].add(Open[j]);
}
}
model.nodeStructureChanged(bat);
model.reload();
jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
jTree1.expandRow(0);
System.out.println("drawing the tree with success");
}
}
the new DrawTreeThread().start()
is called from a button ActionListner()
, but nothing is happening. What's so wrong?
jTree1 = new JTree(bat);
This does nothing. All you have done is create a new JTree object in memory. You haven't actually added the tree to the GUI.
There is no need to create a new tree. Instead you create a new TreeModel and update the existing tree by using:
tree.setModel(...);
Also, when you invoke the above method you should wrap it in a SwingUtilities.invokeLater() to the GUI is updated on the EDT.
精彩评论