Results 1 to 2 of 2
  1. #1
    sQu3aKy is offline Member
    Join Date
    Apr 2010
    Rep Power

    Default 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 is the code to show who is online/offline:

    Java 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");
            treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Offline");
            friendroster.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
            friendroster.setName("friendroster"); // NOI18N
            friendroster.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
    that is the auto generated code by netbeans Ide.

    Java Code:
    public void fillTree(Collection<RosterEntry> srosterEntries){
                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...

    Java 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)

    And below is the TreeModelListener which i still didnt code as i dnt know wat to do

    Java Code:
    class MyTreeModelListener implements TreeModelListener {
        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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    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.


Similar Threads

  1. Split the JTree
    By zed420 in forum New To Java
    Replies: 1
    Last Post: 01-03-2010, 04:42 AM
  2. Jtree Help
    By miladirooni in forum New To Java
    Replies: 1
    Last Post: 10-28-2009, 01:00 AM
  3. zip to JTree
    By icsbcn in forum AWT / Swing
    Replies: 8
    Last Post: 09-01-2009, 04:26 PM
  4. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  5. JTree Problem
    By ngpamela in forum XML
    Replies: 0
    Last Post: 12-10-2008, 05:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts