JList Problem
Hi all,
I have developed a simple chat application and I am having problems updating the onlineUserlist dynamically after a user logs in
or a user logs off.
As you can see below the contacstFrame class implements the ListSelectionListener, ListDataListener
My problem is that i don't know how to tell the model that a user have logged or a user have logged off so it can update dynamically.
Any suggestions...?
package teamC;
//package teamc;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuBar;
import javax.swing.*;
import java.io.*;
import javax.swing.JOptionPane;
/**
*
Title:
*
*
Description:
*
*
Copyright: Copyright (c) 2006
*
*
Company:
*
* @author not attributable
* @version 1.0
*/
public class ContactsFrame extends JFrame implements ListSelectionListener, ListDataListener
{
public static JPanel contentPane;
JMenuBar contactsMenuBar = new JMenuBar();
JMenu jFileMenu = new JMenu();
JMenu jContactsMenu = new JMenu();
JMenu jToolsMenu = new JMenu();
JMenu jHelpMenu = new JMenu();
JMenuItem register = new JMenuItem();
JMenuItem changePassword = new JMenuItem();
JMenuItem chat = new JMenuItem();
JMenuItem logOff = new JMenuItem();
JMenuItem exit = new JMenuItem();
JMenuItem addContact = new JMenuItem();
JMenuItem searchForContact = new JMenuItem();
JMenuItem joinRoom = new JMenuItem();
JMenuItem createRoom = new JMenuItem();
JMenuItem help = new JMenuItem();
//JLabel onlineUsers = new JLabel();
JLabel generalRoom = new JLabel();
private final static String newline = "\n";
String text;
public static String user2;
//public static String stringValue;
private ActionListener callback;
private ActionListener errorCallback;
public static JTextArea generalConverstaionBox = new JTextArea(20,45);
public static JTextField generalTextFeildBox = new JTextField(45);
Frame dialog = new Frame();
public static DefaultListModel contactsModel = new DefaultListModel();
public static JList jListOnlineUsers = new JList(contactsModel);
// public static JScrollPane scrollingList = new JScrollPane(jListOnlineUsers);
private JScrollPane scrollPane = new JScrollPane(jListOnlineUsers);
public ContactsFrame(Client x, String username, String password){
finalClient client = x;
try
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception)
{
exception.printStackTrace();
}
//update list
generalTextFeildBox.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() == KeyEvent.VK_ENTER)
{
if(generalTextFeildBox.getText().length() == 0)
{
//conversationBox.append("");
}
text = generalTextFeildBox.getText().trim();//assigns the value of the text area ta to the String text
System.out.println(text); //used for testing
// generalConverstaionBox.append(LogonFrame.getUsername() + " says: " + text + newline);
//String message = text;
try
{
String users[] = client.getList(LogonFrame.getUsername(), LogonFrame.getPassword());
client.sendMessage(LogonFrame.getUsername(),LogonFrame.getPassword(), users, text);
//client.sendMessage(users, message);
}
catch(Exception e)
{
System.out.println ("error at sendChatMessage (ChatWindowJava)");
}
generalTextFeildBox.setText(""); //resets the text area back to blank
}
}
} );
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit(/*Client x*/) throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setOpaque(true);
//contentPane.setLayout( new BorderLayout() );
contentPane.setLayout(new BorderLayout());
this.setJMenuBar(contactsMenuBar);
setSize(new Dimension(400, 300));
setTitle("ToODLe Contacts ");
contactsMenuBar .setBorder(null);
jFileMenu.setMnemonic('F');
jFileMenu.setText("File");
jContactsMenu.setMnemonic('C');
jContactsMenu.setText("Contacts");
jToolsMenu.setMnemonic('o');
jToolsMenu.setText("Conference");
jHelpMenu.setMnemonic('H');
jHelpMenu.setText("Help");
register.setMnemonic('R');
register.setText("Register New User");
changePassword.setMnemonic('P');
changePassword.setText("Change Password");
chat.setMnemonic('C');
chat.setText("Chat");
logOff.setMnemonic('L');
logOff.setText("Log off");
exit.setMnemonic('X');
exit.setText("Exit");
addContact.setText("Add Contact");
searchForContact.setText("Search for Contact");
joinRoom.setMnemonic('J');
joinRoom.setText("Join a room");
createRoom.setMnemonic('C');
createRoom.setText("Create a Room");
help.setMnemonic('A');
help.setText("About");
generalRoom.setText("General Room");
generalConverstaionBox.setEditable(false);
generalConverstaionBox.setLineWrap(true);//sets up word wrap
generalConverstaionBox.setWrapStyleWord(true); //sets up word wrap style
JScrollPane scrollPane = new JScrollPane(generalConverstaionBox,//provide horizontal and vertical scroll bars
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//jListOnlineUsers.setBounds(new Rectangle(116, 30, 154, 80));
//jLabel2.setText("Offline Users");
//jLabel2.setBounds(new Rectangle(121, 128, 116, 30));
//jListOnlineUsers.setBounds(new Rectangle(50, 52, 154, 80));
//onlineUsers.setText("Online Users");
//jLabel1.setBounds(new Rectangle(10, 100, 109, 23));
contactsMenuBar.add(jFileMenu);
contactsMenuBar.add(jContactsMenu);
contactsMenuBar.add(jToolsMenu);
contactsMenuBar.add(jHelpMenu);
jFileMenu.add(register);
jFileMenu.add(changePassword);
jFileMenu.add(chat);
jFileMenu.add(logOff);
jFileMenu.add(exit);
jContactsMenu.add(addContact);
jContactsMenu.add(searchForContact);
jToolsMenu.add(joinRoom);
jToolsMenu.add(createRoom);
jHelpMenu.add(help);
//contentPane.add(onlineUsers, BorderLayout.NORTH);
contentPane.add(generalRoom, BorderLayout.NORTH);
//contentPane.add(jListOnlineUsers, BorderLayout.EAST);
//contentPane.add(jLabel2);
scrollPane.getViewport().add( jListOnlineUsers );
contentPane.add(scrollPane, BorderLayout.EAST);
contentPane.add(generalConverstaionBox,BorderLayout.CENTER);
contentPane.add(generalTextFeildBox,BorderLayout.SOUTH);
//contentPane.add(scrollPane, BorderLayout.EAST);
//contentPane.add(jList2);
jListOnlineUsers.addListSelectionListener(this);
jListOnlineUsers.addMouseListener(mouseListener);
contactsModel.addListDataListener(this);
//create instance of menuHandler
menuHandler mH = new menuHandler(/*client*/);
//...for each JMenuItem add menuHandler instance:
register.addActionListener(mH);
changePassword.addActionListener(mH);
chat.addActionListener(mH);
logOff.addActionListener(mH);
exit.addActionListener(mH);
addContact.addActionListener(mH);
searchForContact.addActionListener(mH);
joinRoom.addActionListener(mH);
createRoom.addActionListener(mH);
help.addActionListener(mH);
}
//Handler for menu (reference: SUN -http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html)
public class menuHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Client client = new Client();
String username = LogonFrame.getUsername();
String itemClicked = e.getActionCommand();
//...Get information from the action event...
//get itemClick and run approiate method.
if(itemClicked.equals("Register New User"))
{
registerUser();
}
else if (itemClicked.equals("Change Password"))
{
changePwd();
}
else if (itemClicked.equals("Chat"))
{
chat();//This one Bob
}
else if(itemClicked.equals("Log off"))
{
//not implemented within contacts frame...
logOff();
}
else if(itemClicked.equals("Exit"))
{
exit();
}
else if(itemClicked.equals("Add Contact"))
{
addContact();
}
else if(itemClicked.equals("Search for Contact"))
{
searchForContacts();
}
else if(itemClicked.equals("Join a room"))
{
joinConfRoom();
}
else if(itemClicked.equals("Create a Room"))
{
createConfRoom();
}
else if(itemClicked.equals("About"))
{
about();
}
}
}
public static void updateOnlineList(){//this method is used to populate to contacts model on display the online users
try{
String onlineList[] = Client.getList(LogonFrame.getUsername(), LogonFrame.getPassword());
int count=onlineList.length;
for (int i = 0; i < count; i++)
{
// while(!onlineList.equals(LogonFrame.getUsername()))
// {
ContactsFrame.contactsModel.add(i, onlineList);
System.out.println(onlineList);
//}
}
}
catch(Exception e)
{
System.out.println ("error at sendChatMessage (ChatWindowJava)");
}
}
public void contentsChanged(ListDataEvent e) {
//log.append("contentsChanged: " + e.getIndex0() +
//", " + e.getIndex1() + newline);
}
public void intervalAdded(ListDataEvent e) {
}
public void intervalRemoved(ListDataEvent e) {
// log.append("intervalRemoved: " + e.getIndex0() +
//", " + e.getIndex1() + newline);
}
public static void displayMessage(String time, String user2, String msg)
{
generalConverstaionBox.append(user2 + " says: "+ msg + newline);
}
//handler for mouse clicks i.e selecting of users by double clicking
MouseListener mouseListener = new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
// int index = jListOnlineUsers.locationToIndex(e.getPoint());
//System.out.println("Double clicked on Item " + index);
//Client client = logonFrame.getclient();
//client.chatWindowGUI(client, stringValue);//This needs to be 'user2'
//... a String drawn from a selection
Client client = new Client(); //LogonFrame.getMyClient(); //?.getClient();
//stringValue = (String) jListOnlineUsers.getSelectedValue();
//System.out.println("You are chatting with :" + stringValue);
user2 = (String) jListOnlineUsers.getSelectedValue();
System.out.println("You are chatting with :" + user2);
try
{//request chat
if (Client.requestChat(user2));//if the user accepts then cause a chatWindow to appear on his machine
//need a dialog box on the receiver machine
{
Client.chatWindowGUI(client, user2); //chatWindow appears on sender machine
}
}
catch(Exception ex)
{}
}
}
};
public void valueChanged( ListSelectionEvent event ){}
public static String getChatPartyName()
{
return user2;
}
public void registerUser()
{
//TODO: Register new user method
//register new user method comes in here
System.out.println("You selected register new user");
}
public void changePwd()
{
//TODO
//change password method comes here
System.out.println("You selected change password");
}
public void chat()
{
//TODO
//one to one chat method comes here
System.out.println("You selected chat");
//client.
}
public void logOff()
{
System.out.println("You selected log off");
try{
String username = LogonFrame.getUsername();
String password = LogonFrame.getPassword();
Client.logoff(username, password);
}
catch(IOException ex)
{
//exception.printStackTrace();
System.out.println("An error has occurred, Contact system support");
}
}
public void exit()
{
//TODO
//method to exit system comes here...one line code.
System.out.println("You selected exit");
logOff();
System.exit(0);
}
public void createConfRoom()
{
//TODO
//method for creating conf. room comes here
System.out.println("You selected create conference room");
}
public void joinConfRoom()
{
//TODO
//method to allow user to join a conf room comes here
System.out.println("You selected join a conf. room");
}
public void closeConf()
{
//TODO
//method to join conf. room comes help
System.out.println("You selected clos conf. room");
}
//we may not need these...in case we have time we can implemnt.
public void addContact()
{
//TODO
//method to add contact
System.out.println("You selected add contact");
}
public void searchForContacts()
{
//TODO
//method to search for contacts
System.out.println("You selected search for contacts");
}
public void about()
{
System.out.println("Selected about - ToODle chat (trial)V1");
}
}

