How to display Roster in a JTable

Does anyone have some sample code on the best way to display a real time view of the Roster (with presence updates) in a swing JTable?

Thanks

Hi rkrikorian,

I know you asked for this a couple of days ago but hopefully you can still use it:

import java.util.Iterator;

import javax.swing.JDialog;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

import org.jivesoftware.smack.Roster;

import org.jivesoftware.smack.RosterEntry;

import org.jivesoftware.smack.RosterListener;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

public class RosterReflector extends JDialog {

private static String server = “localhost”;

private static String user = “test”;

private static String password = “none”;

private JScrollPane scrollPane = null;

private DefaultTableModel model = new DefaultTableModel();

private JTable tableRoster = new JTable(model);

private XMPPConnection con = null;

private Roster roster;

public static void main(String[] args) {

new RosterReflector();

}

public RosterReflector() {

scrollPane = new JScrollPane(tableRoster);

getContentPane().add(scrollPane);

model.addColumn(“Roster”);

setTitle(“RosterReflector”);

setSize(300, 200);

setVisible(true);

try {

XMPPConnection.DEBUG_ENABLED = false;

con = new XMPPConnection(server);

con.login(user, password);

roster = con.getRoster();

roster.addRosterListener(new MyRosterListner());

updateRosterTable();

} catch (XMPPException e) {

e.printStackTrace();

}

}

private void updateRosterTable() {

model.setRowCount(0);

Iterator entries = roster.getEntries();

while (entries.hasNext()) {

RosterEntry re = (RosterEntry) entries.next();

model.addRow(new Object[]{re.getName()});

}

}

private class MyRosterListner implements RosterListener {

public void rosterModified() {

updateRosterTable();

}

public void presenceChanged(String XMPPAddress) {

//not used

}

}

}

/code

Enjoy,

Ryan

Thanks. This looks like a good starting point.

Hi Ryan,

I tried compiling your code above, but I keep getting the following error, and it won’'t allow the build. Could you give me some insight as to why?

$PATH\src\RosterReflector.java:63: RosterReflector.MyRosterListner is not abstract and does not override abstract method entriesDeleted(java.util.Collection) in org.jivesoftware.smack.RosterListener

It is in regards to the following part of your code:

private class MyRosterListner implements RosterListener {

public void rosterModified() {

updateRosterTable();

}

/code

Thanks,

Mike

Hi Mike,

The RosterListener interface has changed since I originally posted the example up above. To make the example work with the current version of Smack (2.1.0) replace the MyRosterListener above with the one below.

private class MyRosterListner implements RosterListener {

public void rosterModified() {

updateRosterTable();

}

public void presenceChanged(String XMPPAddress) {

//not used

}

public void entriesAdded(Collection arg0) {

updateRosterTable();

}

public void entriesUpdated(Collection arg0) {

updateRosterTable();

}

public void entriesDeleted(Collection arg0) {

updateRosterTable();

}

}

/code

Hope that helps,

Ryan

Thank you, Ryan. That did the trick.

Regards,

Mike

Glad to hear it.

Cheers,

Ryan