Owl Sams Teach Yourself Java 2 in 21 Days

Day 21: Handling User Events with Swing

NOTES AND CORRECTIONS

  • Sun renamed the Swing package of classes for JDK 1.2 Beta 4. To compile the programs in this chapter, Beta 4 users should replace the package name java.awt.swing with com.sun.java.swing.
  • Line 118 of Listing 21.4 refers to TextField when it should refer to JTextField. The correct source code for that line is the following:
    • if (evt.getSource() instanceof JTextField)

    The TextField/JTextField error also appears in the explanation of this program on page 546.

SOURCE FILES

READER QUESTIONS

  • Q: I know in Applet we can use getImage() and drawImage() to use images. How can I add images to a standalone AWT application? 
    A:
    If you use Swing instead of the AWT, which I recommend, one way to include an image in your application is to create an ImageIcon object that contains the image. The book doesn't cover this class, though I was able to get it into the expanded Professional Reference Edition. Check out Sun's documentation for javax.swing.ImageIcon. To start you off, you can create an ImageIcon with a constructor like this:
    • ImageIcon ike = new ImageIcon("eisenhower.gif");

LINKS

  • None