在指定行的代码中,节点添加剂量工作,而上面的syso行正常打印表。如果我将tables.add(new ...("blabla")
放在category.add(triggers);
行下,它将起作用。< / p>
DefaultMutableTreeNode tables,procedures,functions,triggers;
public void loadConnections(){
for(Database db:SavedData.databases){
db.connect();
DefaultMutableTreeNode category = new DefaultMutableTreeNode(db.name);
((DefaultMutableTreeNode)(((DefaultTreeModel)treeConnections.getModel()).getRoot())).add(category);
tables = new DefaultMutableTreeNode("Tables");
procedures = new DefaultMutableTreeNode("Procedures");
functions = new DefaultMutableTreeNode("Functions");
triggers = new DefaultMutableTreeNode("Triggers");
category.add(tables);
category.add(functions);
category.add(procedures);
category.add(triggers);
}
treeConnections.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
TreePath tp = treeConnections.getPathForLocation(me.getX(), me.getY());
if (tp != null){
if(tp.getPathCount()==3){
String dbname=tp.getParentPath().getLastPathComponent().toString();
String function=tp.getLastPathComponent().toString();
Database db=SavedData.getConnection(dbname);
if(function.equals("Tables")){
new Thread(new Runnable() {
public void run() {
try{
ArrayList<String> tablesList=db.connectionInterface.getTables();
tables.removeAllChildren();
for(String table:tablesList){
System.out.println(table);
tables.add(new DefaultMutableTreeNode(table)); ->>>>>>>>>>>>>>>here
}
}catch(Exception ex){ex.printStackTrace();}
}
}).start();
}
}
}
}
});
}
答案 0 :(得分:0)
正如copeg所说,我尝试了DefaultTreeModel.inserNodeInto方法以及其他一些更改并且有效
DefaultMutableTreeNode tables,procedures,functions,triggers;
public void loadConnections(){
for(Database db:SavedData.databases){
db.connect();
DefaultMutableTreeNode category = new DefaultMutableTreeNode(db.name);
((DefaultMutableTreeNode)(((DefaultTreeModel)treeConnections.getModel()).getRoot())).add(category);
tables = new DefaultMutableTreeNode("Tables");
procedures = new DefaultMutableTreeNode("Procedures");
functions = new DefaultMutableTreeNode("Functions");
triggers = new DefaultMutableTreeNode("Triggers");
category.add(tables);
category.add(functions);
category.add(procedures);
category.add(triggers);
((DefaultTreeModel)treeConnections.getModel()).insertNodeInto(new DefaultMutableTreeNode("fyh"), tables, 0);
}
treeConnections.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
TreePath tp = treeConnections.getPathForLocation(me.getX(), me.getY());
if (tp != null){
if(tp.getPathCount()==3){
String dbname=tp.getParentPath().getLastPathComponent().toString();
String function=tp.getLastPathComponent().toString();
Database db=SavedData.getConnection(dbname);
if(function.equals("Tables")){
new Thread(new Runnable() {
public void run() {
try{
ArrayList<String> tablesList=db.connectionInterface.getTables();
DefaultTreeModel model = (DefaultTreeModel) treeConnections.getModel();
TreePath path = treeConnections.getNextMatch("Tables", 0, Position.Bias.Forward);
MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();
node.remove(0);
while(node.getChildCount()>0){
node.remove(0);
}
model.reload();
int index=0;
for(String table:tablesList){
MutableTreeNode newNode = new DefaultMutableTreeNode(table);
model.insertNodeInto(newNode, node,index++);
treeConnections.expandPath(path);
}
}catch(Exception ex){ex.printStackTrace();}
}
}).start();
}
}
}
}
});
}