Java swing toolkit graphics
Sponsored Links
This presentation is the property of its rightful owner.
1 / 22

Java Swing Toolkit Graphics PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

Java Swing Toolkit Graphics. The key to effectively using graphics in Java is understanding: the basic components of the graphics library the patterns that are used to combine components. Some patterns used. O BSERVER S TRATEGY C OMPOSITE D ECORATOR. Some patterns used.

Download Presentation

Java Swing Toolkit Graphics

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Java Swing Toolkit Graphics

  • The key to effectively using graphics in Java is understanding:

    • the basic components of the graphics library

    • the patterns that are used to combine components

Some patterns used





Some patterns used

  • OBSERVER – event handling

  • STRATEGY – layout management

  • COMPOSITE – containers are also components

  • DECORATOR – scrollbars, streams


  • All displayable objects in the graphics hierarchy extend this base class. You will never create an instance of this class directly (since it is abstract), but instead use the pre-defined GUI elements that Java provides.


  • A containers is the basic enclosing element of a graphical application. You can’t have a graphical application without container to hold the rest of your graphical components.

  • The top level container for an application is usually a javax.swing.JFrame


  • A JFrame is a top-level container (meaning it does not need to be contained within any other container).

  • A JFrame is a window with a title and a border.

  • JFrames also support menu bars.

An example using a JFrame

public class JFrameExample {

public JFrameExample() {

JFrame f = new JFrame();




public static void main(String[] args) {

new JFrameExample();



Composite Pattern

Composite Pattern

  • The whole graphical framework is a hierarchy of components. This is an almost textbook example of the composite pattern. A frame contains many children, which all comply to the same interface (Component), and a call to, for example, repaint() on the frame will also call repaint() on all its children.

Input Elements

  • The whole point of a GUI is to accept user input and do something with it. To this end, there are a large number of input components in Swing.

  • Let’s first examine the JButton.

Button Example

JButton button = new JButton("Push Me");


button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {





Idea: decouple event from event handling

Abstract Observable

Abstract Observable

Abstract Observable

Abstract Observer






Concrete Observable

Concrete Observer


  • The observer pattern is used for event notification.

  • Observables (classes like JButton) generate events.

  • An observable can have many observers.


Terminology differs slightly in JFC classes:

Abstract Observable

Abstract Observable






Multiple Observers

We can add more than one observer to our button. All observers are notified when a button event occurs. Lets add an observer that will also change the color of the label.

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {





  • A decorator adds functionality while maintaining an interface.

  • One example:

    • InputStreamReader wraps InputStream

    • BufferedReader wraps InputStreamReader

  • Another example:

    • JScrollPane wraps Jlist


  • A layout manager has responsibility for laying out components within a container.

  • Unlike in NGP, JFC containers do not have fixed layout managers.

  • Layout managers are treated as strategies.

  • Strategies can be swapped.

Layout Managers

  • Swing has several different layout managers. Some of the most common are FlowLayout, BorderLayout, GridLayout, and GridBag Layout. Each has advantages and disadvantages.


  • The BorderLayout manager creates an object that resembles a picture with a four sided frame, or border.


  • FlowLayout is arguably the simplest layout manager. It just stacks up components in a row, right to left. If it runs out of space, it wraps to a new line. This is the default layout manager for JPanel. You can see the behavior in how the button, text area, and combo box are laid out.


  • GridLayout arranges its components in an equally spaced grid of cells. Will take as much space as is available to it.

GridBag Layout

  • GridBag is both the most flexible of the layout managers, and the most difficult to use. It allows you customize the size and growth of all the components separately, but setting it up is a pain.

  • Login