Tag: code

StoppableThread.java A template to place a thread in a RUN, WAIT, or STOP state.

/** A template to control the state of a thread through setting  *  an internal flag. public class StoppableThread extends Thread {    public static final int STOP    = 0;    public static final int RUN     = 1;    public static final int WAIT    = 2;    private int state = RUN;   /** Public …

Continue reading

mall example showing the basic use of a JToolBar

mall example showing the basic use of a JToolBar import java.awt.*; import javax.swing.*; import java.awt.event.*; /** Small example showing basic use of JToolBar.  *  *  */ public class JToolBarExample extends JFrame                              implements ItemListener {   private BrowserToolBar toolbar;   private JCheckBox labelBox;   public static void main(String[] args) {     new JToolBarExample();   } …

Continue reading

UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server.

UrlRetriever2.java  Illustrates how the URL class can simplify communication to an HTTP server. import java.net.*; import java.io.*; /** Read a remote file using the standard URL class  *  instead of connecting explicitly to the HTTP server.  *  *  Taken from Core Web Programming from  *  Prentice Hall and Sun Microsystems Press,  *  .  *  © …

Continue reading

RMI Example – Message, illustrates retrieving a message from an object located on a remote server. Requires the following classes

Rem.java  Establishes which methods the client can access in the remote object. import java.rmi.*; /** The RMI client will use this interface directly. The RMI  *  server will make a real remote object that implements this,  *  then register an instance of it with some URL.  *  *  Taken from Core Web Programming from  *  …

Continue reading

Creates three common types of sliders

Creates three common types of sliders: one without tick marks, one with tick marks, and one with both tick marks and labels. import java.awt.*; import javax.swing.*; /** Simple example illustrating the use of JSliders, especially  *  the ability to specify tick marks and labels.  *  *     */ public class JSliders extends JFrame {   …

Continue reading

Basic Swing Details

WindowUtilities.java Utility class that simplifies creating a window and setting the look and feel. ExitListener.java A WindowListener with support to close the window. JAppletExample.java A simple applet (JApplet) created in Swing. Illustrates setting the look and feel, adding components to the content pane, and changing the layout to FlowLayout (default is BorderLayout). See JAppletExample.html (requires …

Continue reading

AddressVerifier.java Connects to an SMTP server and issues a expn request to display details about a mailbox on the server. Uses the following classes

import java.net.*; import java.io.*; /** Given an e-mail address of the form user@host,  *  connect to port 25 of the host and issue an  *  ‘expn’ request for the user. Print the results.  *  *  Taken from Core Web Programming from  *  Prentice Hall and Sun Microsystems Press,  *  .  *  © 2001 Marty Hall …

Continue reading

Buggy Counter Applet.java Demonstrates that data shared by multiple threads is candidate for a potential race condition

import java.applet.Applet; import java.awt.*; /** Emulates the Counter and Counter2 classes, but this time  *  from an applet that invokes multiple versions of its own run  *  method. This version is likely to work correctly  *  except when  an important customer is visiting. public class BuggyCounterApplet extends Applet                                 implements Runnable{   private int totalNum …

Continue reading

SplitTest.java Illustrates parsing a string with a String.split

SplitTest.java Illustrates parsing a string with a String.split /** Prints the tokens resulting from treating the first * command-line argument as the string to be tokenized * and the second as the delimiter set. Uses * String.split instead of StringTokenizer. */ public class SplitTest { public static void main(String[] args) { if (args.length == 2) …

Continue reading

TokTest.java Illustrates parsing a string with a StringTokenizer.

TokTest.java  Illustrates parsing a string with a StringTokenizer. import java.util.StringTokenizer; /** Prints the tokens resulting from treating the first  *  command-line argument as the string to be tokenized  *  and the second as the delimiter set.  *  *  Taken from Core Web Programming from  *  Prentice Hall and Sun Microsystems Press,  *  .  *  © …

Continue reading