Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
This simple example of a client/server system is intended to demonstrate how to build applications using just the streams available in the standard API. The chat uses TCP/IP sockets to communicate, and can be embedded easily in a Web page. For reference, we provide a sidebar explaining Java network programming components that are relevant to this application. If you're still getting up to speed, take a look at the sidebar first. If you're already well-versed in Java, though, you can jump right in and simply refer to the sidebar for reference.
We start with a simple graphical chat client. It takes two command-line parameters -- the server name and the port number to connect to. It makes a socket connection and then opens a window with a large output region and a small input region.

After the user types text into the input region and hits Return, the text is transmitted to the server. The server echoes back everything that is sent by the client. The client displays everything received from the server in the output region. When multiple clients connect to one server, we have a simple chat system.
This class implements the chat client, as described. This involves setting up a basic user interface, handling user interaction, and receiving messages from the server.
import java.net.*;
import java.io.*;
import java.awt.*;
public class ChatClient extends Frame implements Runnable {
// public ChatClient (String title, InputStream i, OutputStream o) ...
// public void run () ...
// public boolean handleEvent (Event e) ...
// public static void main (String args[]) throws IOException ...
}
The ChatClient class extends Frame; this is typical for a graphical application. We implement the Runnable interface so that we can start a Thread that receives messages from the server. The constructor performs the basic setup of the GUI, the run() method receives messages from the server, the handleEvent() method handles user interaction, and the main() method performs the initial network connection.
protected DataInputStream i;
protected DataOutputStream o;
protected TextArea output;
protected TextField input;
protected Thread listener;
public ChatClient (String title, InputStream i, OutputStream o) {
super (title);
this.i = new DataInputStream (new BufferedInputStream (i));
this.o = new DataOutputStream (new BufferedOutputStream (o));
setLayout (new BorderLayout ());
add ("Center", output = new TextArea ());
output.setEditable (false);
add ("South", input = new TextField ());
pack ();
show ();
input.requestFocus ();
listener = new Thread (this);
listener.start ();
}
The constructor takes three parameters: a title for the window, an input stream, and an output stream. The ChatClient communicates over the specified streams; we create buffered data streams i and o to provide efficient higher-level communication facilities over these streams. We then set up our simple user interface,
consisting of the TextArea output and the TextField input. We layout and show the window, and start a Thread listener that accepts messages from the server.
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq