Graphical User Interface Information

Home Up Search Java 2 API C++ Resources

Below is a listing of some of the most common GUI classes and interfaces used.   It is not complete by any means, and is only intended as a quick overview for novices.   For more complete discussions of the classes involved, please see the main Java API documentation.

"AWT" stands for "Abstract Windowing Toolkit" which is the primary set of classes used by Java to produce GUI's.   "Swing" stands for the latest edition of GUI components that are designed for better performance and flexibility over the original AWT components.   Given a choice, always choose the Swing component over the AWT component. 

  • java.awt.Component

    • javax.swing.JLabel

    These components allow you to display textual or iconic information on the screen. 

      • void setText(String s)

This method set the text of the label to the supplied String value.

      • String getText()

This method retrieves the text of the label.

    • javax.swing.JButton

These components allow your program to respond to mouse clicks (note: responding to mouse clicks is not restricted to buttons).

      • void setText(String s)

This method set the text of the button to the supplied String value.

      • String getText()

This method retrieves the text of the button.

    • javax.swing.JTextField

These components allow you to enter a single line of text information.

      • void setText(String s)

This method set the text of the text field to the supplied String value.

      • String getText()

This method retrieves the text of the text field.

    • javax.swing.JTextArea

These components allow you to input and display multiple lines of text information.

      • void setText(String s)

This method set the text of the text area to the supplied String value.

      • String getText()

This method retrieves the text of the text area.

      • void append(String s)

This method adds the supplied text to the existing text in the text area.

    • Containers

Containers are components that can contain other components (Composite design pattern).

      • javax.swing.JFrame

A window with a title bar, resizing controls and optionally, menu and task bars.  The components that a JFrame holds are organize into "panes" which are individual layers of components.  Panes can be tranparent or opaque, visible or invisible and can be used to rapidly switch from one collection of components, to another.

        • Container getContentPane()

This method returns the "content pane" which is the default pane for a JFrame.  This is normally the only pane that is used.   Components need to be added to this pane.

        •  
      • javax.swing.JPanel

This container is used inside frames or other panels to isolate areas of the screen.   For instance, a set of buttons can be grouped together by placing them all in the same panel and thus be separated from other collections of components. A total layout of a GUI usually consists of many layers of panels holding other panels.

      • Methods common to all Containers

        • void add(Component c)

This method adds the supplied Component to the container and subjects it to the Container's installed layout.

        • setLayout(Layout l)

Sets the container's layout to the supplied layout.

      • Layouts

Layouts control the automatic positioning of Components in a Container.   This enables the Java program to run on a wide variety of operating systems and window managers which all have slightly different ideas of what graphical elements should look like.    It also enables a window and the components in it to properly resize.

        • BorderLayout

This places components in one of 5 locations:  around the edge of the container ("east", "west", "north" or "south") or in the center ("center"); This is the default layout for frames.

        • FlowLayout

This places components in a single centered line in the container.  It is the default layout for panels.

        • GridLayout

This places components in a mxn grid, where m and n can be specified.  Using -1 for the number of rows or columns allows you to be unspecific--the number of rows or columns that will be shown will correspond to the number of components added.

    • Methods Common to All Components

      • void paint(Graphics g)

This method is called by the Java graphics subsystem ("the "AWT" subsystem) whenever a component needs to be drawn ("painted").  The Graphics object handed to the method by the AWT system represents the screen onto which the Component can draw itself.    The (0, 0) coordinate of the Graphics object is the upper left hand corner of the Component on the screen.   Since painting a Component often involves a number of processes that programmer may not care about, the first line of an overriden paint() method is usually

super.paint(g);

to let the Component take care of what is usually done, such as clearing the screen.

The paint() method is NEVER called directly from a program!   Only the AWT system can call it, as the AWT system is the only thing that can correctly provide the proper Graphics object.

      •  void repaint()

This method sends a request to the AWT system to repaint the Component.   There is no guarantee as to when the Component will be repainted!   A common mistake is to assume that the screen gets repainted as soon as a a repaint() method is called.  

A program can only call repaint() to request that the screen be repainted, never paint() directly.

      • void validate()

This method causes the the Component to recalculate the layout of the Component.   Usually only used on Containers to re-layout the contained Components.   Useful when Components are being added, removed or moved dynamically.

      • void addActionListener(ActionListener al)

This method adds the supplied listener to the set of "observers" that the Component (the "Observable") will call when an event occurs.

      • void setEnabled(Boolean isEnabled)

This method enables or disables the Component's ability to process events.  Useful for things like disabling buttons when certain processes are taking place. 

      • void setVisible(Boolean isVisible)

This method shows or hides the component on the screen.

      • void setSize(int width, int height)

This method sets the width and height of the component in pixels.

      • void setPreferredSize(Dimension d)

This is a method of javax.swing.JComponent, not Component.  It sets the size the JComponent should be, if the Java graphics system can do it.   Dimension is a class that holds a width and a height, e.g   myJComponent.setPreferredSize(new Dimension( myWidth, myHeight)).

  • Other Classes and Interfaces

    • java.awt.Color

This class represents a color to Java.   It has a number of public static fields that can be used to represent common colors:  Color.red, Color.blue, Color.black, Color.white, etc.

    • java.awt.Graphics

These objects represent the graphics surface of the screen.  That is, any direct painting of the screen, such as for drawing lines or shapes, is done on a Graphics object.

        • void setColor(Color c)

Sets the color to be used for the next drawing task.

        • void drawLine(int x1, int y1, int x2, int y2)

Draws a line from (x1, y1) to (x2, y2).   The origin used is the upper left corner of the Component that is being painted upon.   Positive y values are downward and positive x values are to the right.

        • void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

Draws an arc using the following parameters:

x- the x coordinate of the upper-left corner of the arc to be drawn.
y - the y coordinate of the upper-left corner of the arc to be drawn.
width - the width of the arc to be drawn.
height - the height of the arc to be drawn.
startAngle - the beginning angle.
arcAngle - the angular extent of the arc, relative to the start angle.

There is also a fillArc() method that will create a filled arc.

    • java.awt.event.ActionListener

This interface provides the actionPerformed(ActionEvent e) method that Components such as buttons, labels and text fields call when mouse clicked.    This class defines a listener for those components.  There is no associated adapter class since ther is only one method.  

    • java.awt.event.ActionEvent

This class encapsulates all the relevant information about an event.   The Java AWT system will construct an instance of this class and pass it as a parameter when the ActionListener's actionPerformed() is called.   Good OO code rarely needs the information in the ActionEvent because the information is already known via polymorphism.

    • java.awt.event.MouseListener

This interface is used for events that concern mouse presses, releases, entering a component, leaving a component, etc.   It has an associated MouseEvent class that is used to encapsulate the specifics of the event.  The MouseInputAdapter class implements this interface and gives default no-operation behavior to all the methods.

    • java.awt.event.MouseMotionListener

This interface is very similar to MouseListener, but is for events such as the mouse is moving or the mouse is dragged.   The event information is encapsulated in MouseEvent classes.   The MouseInputAdapter also implements this interface.   MouseMotionAdapter only implements this interface.

    • java.awt.event.KeyListener

This interface is used for key pressed, key released and character typed events.   The event specifics are encapsulated in KeyEvent objects.   The associated adapter is KeyAdapter.