-
Let's go ahead and make this a JFrame, separate from ChatImpl, for proper
Object Oriented Design.
-
In ChatImpl, we'll need to construct an instance of this frame.
-
When we receive a message from the client in the sendMessage method, we'll
need to call some method on the JFrame to update the list.
-
As for the JFrame....
-
We'll give it some simple GUI elements. A JLabel for the
title and a JList inside a JScrollPane.
-
We'll need to make a DefaultListModel object, and pass that to
the JList via the setModel method. This will allow us to
dynamically update the JList as we receive messages.
-
We'll also need to make a method that accepts String messages
and updates this JList.
-
For more information on JLists and DefaultListModels,
see my Java II slides on
that subject.
-
Screen Capture:
-
The new source: BulletinBoard
/*
* BulletinBoard.java
*
* Created on May 8, 2003, 12:32 AM
*/
package chat;
import javax.swing.DefaultListModel;
/**
*
* @author default
*/
public class BulletinBoard extends javax.swing.JFrame {
DefaultListModel model;
/** Creates new form BulletinBoard */
public BulletinBoard() {
initComponents();
// Instantiate our model, pass it to the JList.
model = new DefaultListModel();
lstBoard.setModel(model);
}
/** This method is called from within the constructor
to
* initialize the form.
* WARNING: Do NOT modify this code. The
content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
lblTitle = new javax.swing.JLabel();
pnlButtons = new javax.swing.JPanel();
scrBoard = new javax.swing.JScrollPane();
lstBoard = new javax.swing.JList();
addWindowListener(new
java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
lblTitle.setText("jLabel1");
getContentPane().add(lblTitle,
java.awt.BorderLayout.NORTH);
getContentPane().add(pnlButtons,
java.awt.BorderLayout.SOUTH);
scrBoard.setViewportView(lstBoard);
getContentPane().add(scrBoard,
java.awt.BorderLayout.CENTER);
pack();
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent
evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new BulletinBoard().show();
}
/**
* A method to add
incoming messages to the JList.
*
*/
public void addMessage(String
message) {
model.addElement(message);
}
// Variables declaration - do not modify
private javax.swing.JLabel lblTitle;
private javax.swing.JPanel pnlButtons;
private javax.swing.JScrollPane scrBoard;
private javax.swing.JList lstBoard;
// End of variables declaration
}
-
The new source: ChatImpl
package chat;
/*
* ChatImpl.java
*
* Created on April 28, 2003, 11:48 PM
*/
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.MalformedURLException;
/** Unicast remote object implementing Chat interface.
*
* @author default
* @version 1.0
*/
public class ChatImpl extends java.rmi.server.UnicastRemoteObject
implements Chat {
BulletinBoard board;
/** Constructs ChatImpl object and exports it
on default port.
*/
public ChatImpl() throws RemoteException {
super();
// Fire up our JFrame here.
board = new BulletinBoard();
board.show();
}
/** Constructs ChatImpl object and exports it
on specified port.
* @param port The port for exporting
*/
public ChatImpl(int port) throws RemoteException
{
super(port);
}
/** Register ChatImpl object with the RMI registry.
* @param name - name identifying the service
in the RMI registry
* @param create - create local registry
if necessary
* @throw RemoteException if cannot be
exported or bound to RMI registry
* @throw MalformedURLException if name
cannot be used to construct a valid URL
* @throw IllegalArgumentException if null
passed as name
*/
public static void registerToRegistry(String
name, Remote obj, boolean create) throws RemoteException, MalformedURLException{
if (name == null) throw
new IllegalArgumentException("registration name can not be null");
try {
Naming.rebind(name, obj);
} catch (RemoteException
ex){
if (create) {
Registry r = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
r.rebind(name, obj);
} else throw ex;
}
}
/** Main method.
*/
public static void main(String[] args) {
System.setSecurityManager(new
RMISecurityManager());
try {
ChatImpl obj = new ChatImpl();
registerToRegistry("ChatImpl", obj, true);
} catch (RemoteException
ex) {
ex.printStackTrace();
} catch (MalformedURLException
ex) {
ex.printStackTrace();
}
}
public void login(String user, String password)
{
}
public void sendMessage(String message, String
user) {
System.out.println("Message:
" + message + " User: " + user);
// call method on JFrame to add this to our list.
board.addMessage(message);
}
}
-
All other source remains the same.