Title: Gui Component High Level
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

AWT Basics and Swing

AWT stands for Abstract Windowing Toolkit. Most of the notes for a while will contain an import of the package:
import java.awt.*;

The AWT library has been around for a long time and the Java Virtual Machine (JVM) in virtually every browser should understand any Applet written with the AWT library. The AWT library was written to make use of native GUI elements present on each platform where it ran. Consequently, each platform will tend to look a little different due to the fact that the underlying platform is different. Later the Java Sun team decided to create a new GUI library called SWING which was a rewrite this software to be more "bottom up". In otherwords, much less native GUI software was used and most of the software comes from the Java library. You still need to use many components in the AWT library, but SWING provides a more uniform look and feel no matter what platform you are executing on.

1. When running an applet, you are using a Java run time environment that exists in the browser. Some problems can occur if the java environment that you are developing in is newer than the the Java run time environment in your browser.
2. If you write your Applets for SWING you will use a set of classes that start with "J" and come from:
import javax.swing.*;
The old classes in the AWT are still available, but are somewhat obsoleted by the "J" classes. Here is a table of examples:

SWING class | AWT class
JApplet | Applet
JFrame | Frame
JButton | Button
JComponent | Component
JTextArea | TextArea
JTextField | TextField
JLabel | Label

3. Swing kept a lot of the original AWT concepts in the areas of dealing with actual drawing, Events (keystrokes, mouse activity), and layout managers. The major area that is impacted is the Component area where SWING provides a different set of classes for buttons, textareas, etc. The following figure gives you a class heirarchy of how AWT and SWING are related:

Concepts

We will mostly focus on JFrames and JApplets. Later sections of the notes will deal with the other windowing types.

JApplets are a special window that can live inside of a Browser web page. A JFrame on the otherhand normally gets started from an application (i.e. java xyz). Our JFrame based applications will always need a main routine where the actual JFrame is created. The normal steps for starting up a JFrame based application include:

1. For GUI Components, you will do many of the same things in the JFrame constructor as we did previously in the basic drawing examples.
2. The one new thing is the layoutManager. The job of the layout manager is to determine where to place your components on the screen. You pick the layout manager based on the strategy you want to use. It is possible(but not recommended) to go with a "null" layout manager and then you can specify the pixel location for all of your components. If you don't want the default layout manager (BorderLayout), you pick your desired Layout Manager. This is a topic that we will discuss more later.

GUI Runnable from the Command line

BasicFrame.java


import java.awt.*;
import javax.swing.*;

public class BasicFrame extends JFrame{
    BasicFrame(String name)
    {
       super(name); // invoke Frame constructor that has a name

       setLayout(new FlowLayout());
       setSize(600,150);
       add(new JButton("HI There"));
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setVisible(true);
     }

  public static void main(String[] args) {
    BasicFrame aFrame = new BasicFrame("BasicFrame");
  } // end of main
}


GUI Runnable Within a Browser

If you are running in an Applet that lives in a web page, then some of the above steps mentioned for Frame based applications are already done by the Browser. Here is the sequence of events that occur in the Applet case:

1. Your html file needs a tag something like:
<applet code="BasicApplet.class" width="600" height="150">
The browser recognizes the applet tags and determines from the Html how many pixels wide and high the applet will take from the phrase width=600 height=150--- see the BasicApplet.html file below.
2. The browser then loads in the java class file based on the code = BasicApplet.class phrase In the BasicApplet.html file below, the browser will download BasicApplet.class from the same directory where BasicApplet.html was found.
3. The browser has a built in Java Runtime Environment (JRE), and will attempt to construct a class named BasicApplet. If the class isn't derived from Applet or JApplet, it won't work.
4. Once the Applet is constructed, the browser calls the Applet init routine to allow the Applet to initialize itself. Often you want to override the Applet init routine to do what you want to do here.
5. When the Browser wants the Applet to start executing, it calls the start routine. You may or may not want to override this routine.
6. Whenever it is necessary to paint the screen, the paint routine is called. If you are using higher level components (like the Button class), the paint occurs inside of the higher level component.
7. When the user moves onto a different web page, the browser will call the stop routine. You can override the stop routine if you have any special stop code you want to include in your Applet.
8. Unless you override either paint, init or start, you won't have anything worthwhile in your applet.
9. If you want to publish the following Applet, compile it and place BasicApplet.html and BasicApplet.code in some web directory that you own.
10. Eclipse will execute Applets for you. If you are running from the lower level java, javac, command line environment you could use the java appletviewer program.
11. One common problem with debugging Applets in a web page is the existence of a very Sticky Cache for applets. This means that you thought you changed the applet, but you still see the old code. You will need to flush the Browser cache --- reload sometimes doesn't work very well for applets. Sometimes you may even need to kill all of your browser windows and reload the applet in a new browser instance. You won't have these problems with eclipse or the appletviewer.

BasicApplet.java


import java.awt.*;
import javax.swing.*;

public class BasicApplet extends JApplet {

    public void init() {
        setLayout(new FlowLayout());
        add(new JButton("HI There"));
    }
}


BasicApplet.html


<html>
<body>
<h2>BasicApplet</h2>
<applet code=BasicApplet.class width=600 height=150>
</applet>
</body></html>


BasicApplet

BasicBoth.java

I can actually create a java class that can act both as an Applet and can be run from the command line. Try the following code at the command line (i.e. java BasicBoth). Also try out this code as an Applet. The steps are as follows:

1. Create a class derived from JApplet and override the routines desired (i.e. paint, init, start, or stop)
2. Add a main routine that effectively executes the normal steps taken by a browser which are:
3. Create a JFrame
4. Give the JFrame a Size in pixels (width, height)
5. Construct the JApplet derived class and add it to the JFrame.
6. If your code uses an init routine call the init routine (this is the only routine I used)
7. If your code uses a start routine call the start routine
8. If your code uses a stop routine, I would recommend that you override the finalize method and call stop from there.


import java.awt.*;
import javax.swing.*;

public class BasicBoth extends JApplet {

    public void init() {
        setLayout(new FlowLayout());
        add(new JButton("HI There"));
    }

  public static void main(String[] args) {
    BasicBoth applet = new BasicBoth();
    JFrame aFrame = new JFrame("Basic");

    aFrame.add(applet);
    aFrame.setSize(600,150);
    // Normally the following call comes from the Browser
    applet.init();

    aFrame.setVisible(true);
  } // end of main
}



BasicBoth

If you take the "Both" approach and you decide that you just want a pure applet and you want to remove the capability to run your code from the command line, just eliminate the main routine.

Last Updated: July 28, 2014 2:34 PM