Java Reference
In-Depth Information
3. Within the actionPerformed() method, if the Send button is clicked,
instantiate a new InstantMessage object (using your InstantMessage
class from Lab 16.3) and serialize it to the Object OutputStream field
of the SendMessage class.
4. Save and compile the SendMessage class.
5. Write a class named Participant that extends Thread. Add a field of
type ObjectInputStream and a field of type String to represent a
user's name.
6. Add a constructor that takes in an InputStream and a String. Initialize
the ObjectInputStream field by chaining the InputStream parameter to
a new ObjectInputStream object. Store the String in your String field.
7. Within the run() method, add an infinite while loop. Within the
while loop, invoke readObject() on the ObjectInputStream field.
This will cause the thread to block until an object becomes available
in the stream.
8. Cast the return value of readObject() to an InstantMessage reference.
The InstantMessage read from the stream represents an incoming
message from a friend.
9. Display a modeless dialog window that displays the name of the
sender and the message. This may require writing additional classes
for laying out the dialog and performing any necessary event han-
dling to close the dialog. A handy feature is a Reply button, which
allows the user to quickly reply to the sender.
10. You will now modify your InstantMessageDialog.java class from
Lab 13.4 because it will no longer compile with all the changes you
made to SendMessage. Within the constructor of InstantMessage
Dialog, instantiate a PipedOutputStream and PipedInputStream and
connect them.
11. Pass in the PipedOutputStream to the SendMessage constructor.
12. Instantiate a new Participant object, passing in the PipedInput-
Stream to the constructor. Start the Participant thread.
13. Save, compile, and run the InstantMessageDialog class.
When you click a friend in the JList of the InstantMessageFrame win-
dow, a dialog window should appear (as before). Typing in a message
and clicking the Send button should close this dialog and display a new
modeless dialog window with the message you just sent to somebody else.
(In the next chapter, “Network Programming,” you will finish this project
by sending the message to an actual recipient on a different computer
instead of sending it to yourself, which is what your program does now.)
Search WWH ::




Custom Search