RMIChatClientpublic class RMIChatClient extends RMICollaboratorImpl implements ActionListenerSource code from "Java Distributed Computing", by Jim Farley.
Class: RMIChatClient
Example: 10-1
Description: A collaborator in an RMI-based chat session. Includes an AWT
interface for the chat display. |
Fields Summary |
---|
TextArea | chatArea | TextField | chatInput |
Constructors Summary |
---|
public RMIChatClient(String name, String host, String mname)
super(name);
Properties p = new Properties();
p.put("host", host);
p.put("mediatorName", mname);
connect(p);
initGraphics();
|
Methods Summary |
---|
public void | actionPerformed(java.awt.event.ActionEvent e)
// See if there's something to say...
String msg = chatInput.getText();
if (msg.length() > 0) {
try {
// Broadcast our message to the rest of the chat clients
boolean success = broadcast("chat", msg);
if (success) {
System.out.println("Sent message OK.");
}
else {
System.out.println("Failed to send message.");
}
// Clear the chat input field
chatInput.setText("");
}
catch (Exception exc) {
}
}
| protected void | initGraphics()
Frame f = new Frame();
f.setLayout(null);
f.addNotify();
f.setSize(f.getInsets().left + 405, f.getInsets().top + 324);
chatArea = new java.awt.TextArea();
chatArea.setBounds(f.getInsets().left, f.getInsets().top,405, 300);
f.add(chatArea);
chatInput = new java.awt.TextField();
chatInput.setBounds(f.getInsets().left + 84,f.getInsets().top + 300,264,24);
f.add(chatInput);
Button button = new java.awt.Button("Send");
button.setBounds(f.getInsets().left + 348,f.getInsets().top + 300,60,24);
f.add(button);
button.addActionListener(this);
Label label = new java.awt.Label("Chat here:");
label.setBounds(f.getInsets().left,f.getInsets().top + 300,84,24);
label.setAlignment(label.RIGHT);
f.add(label);
f.setTitle("RMI Chat Client");
f.show();
| public boolean | notify(java.lang.String tag, java.lang.String msg, Identity src)
// Print the message in the chat area.
chatArea.append("\n" + src.getName() + ": " + msg);
return true;
|
|