-
JTree/TreeModelListener
Hi guys,
I am developing an instant messaging using "smack API" and Openfire as the server.
Main GUI which contains:
-Menubar:File, Help
-status label with statusdropdownlist (available, away... etc)
-JScroller with JTree to show the users "jst like msn, yahoo etc)
-and a logout button
------------------------------------------------------------
I've created the JTree using the palette, however after i tried coding to display users who are online/offline in the JTree, i couldnt get it to work and am not sure how to do it...here is the code to show who is online/offline:
Code:
private javax.swing.JTree friendroster;
friendroster.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Online");
treeNode1.add(treeNode2);
treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Offline");
treeNode1.add(treeNode2);
friendroster.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
friendroster.setName("friendroster"); // NOI18N
friendroster.setRootVisible(false);
friendroster.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
friendrosterMouseClicked(evt);
}
});
that is the auto generated code by netbeans Ide.
-----------------------------------------------------
Code:
public void fillTree(Collection<RosterEntry> srosterEntries){
if(currUser.getAvailability().isAvailable()){
friendroster.addSelectionPath(friendroster.getSelectionPath());
friendroster.setModel((TreeModel) srosterEntries);
}
}
----------------------------------------------------------------
this is a method that i have written to test it but never got it to work, "Note: currUser is -- private User currUser; where User is another class".
Without using GUI, I used this code to display the users who are only online using the Netbeans IDE console and wont show the users who are offline...
Code:
public void displayBuddyList()
{
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for(RosterEntry r:entries)
{
System.out.println(r.getUser());
}
}
------------------------------------------------------------
And below is the TreeModelListener which i still didnt code as i dnt know wat to do
Code:
class MyTreeModelListener implements TreeModelListener {
MyTreeModelListener(){
}
public void treeNodesChanged(TreeModelEvent e) {
}
public void treeNodesInserted(TreeModelEvent e) {
}
public void treeNodesRemoved(TreeModelEvent e) {
}
public void treeStructureChanged(TreeModelEvent e) {
}
}
---------------------------------------------------------------
All am trying to do is once the user is online, show the user's friends on the JTree.
Pls help!
Thanx in advance
-
Read the API for JTree and follow the link to the Sun/Oracle tutorial on How to Use Trees.
If I understand your goals correctly, you don't need a TreeModelListener at all. Rather, you need some kind of listener or callback that is triggered when the status of currUser's "buddies" changes, and updates the tree model accordingly. If you use a DefaultTreeModel, it already has the necessary methods.
db