Chapter 6. GUI Programming: From Appletsto Swing

This chapter is all about creating GUIs with Java code in Eclipse. So far, our code has just displayed text in the console. In this chapter, we’re going to start creating GUIs using applets, the Abstract Windowing Toolkit (AWT), and Swing. We’ll also take a look at using an Eclipse plug-in to create Swing code.

Our first topic is all about building applets. Java applets were Java’s first foray into the Internet, and they were popular for quite a while. Browsers first started stocking Java in order to support applets. In time, applets have become less popular because they’re necessarily limited and have been superceded by glitzy packages like Flash and Java Web Start. Nonetheless, Eclipse has special provisions for developing and testing applets, so we’ll take a brief look here at how that support works.

Creating an applet is much like creating any other Java project in Eclipse. In this case, our applet is just going to display the message “Hello from Eclipse!” Create a new Java project named Ch06_01, and give it a new class, Ch06_01, in the org.eclipsebook.ch06 package. To create an applet, we’ll need to import java.applet.Applet and java.awt.*:

import java.applet.Applet;
import java.awt.*;

Then extend the Ch06_01 class from the Applet class:

public class Ch06_01 extends Applet {
        .
        .
        .

In an applet, the init method lets you initialize the applet, and we’ll set the background color to white in that method. The start, stop, and ...

Get Eclipse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.