swing: building guis in java · advisable to use only swing widgets. however, much of the...

51

Upload: others

Post on 14-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Swing: Building GUIs in Java

ENGI 5895: Software Design

Andrew Vardy

Faculty of Engineering & Applied ScienceMemorial University of Newfoundland

February 10, 2011

Page 2: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Outline

1 Introduction

2 Aside: Inner Classes

3 Aside: Threads

4 Examples

Page 3: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Introduction

Swing is the o�cially supported GUI toolkit for Java. It wasdeveloped to replace the GUI components of the Abstract WindowToolkit (AWT). It has the following features:

Pluggable �look and feel� [DEMO]

Numerous standard and specialized widgets (text boxes,buttons, tabbed panels,...)

Swing components render themselves in an OS-independentmanner

Relative layout of components

Nice design choices: Easily extensible with heavy use ofpatterns

Page 4: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Introduction

Swing is the o�cially supported GUI toolkit for Java. It wasdeveloped to replace the GUI components of the Abstract WindowToolkit (AWT). It has the following features:

Pluggable �look and feel� [DEMO]

Numerous standard and specialized widgets (text boxes,buttons, tabbed panels,...)

Swing components render themselves in an OS-independentmanner

Relative layout of components

Nice design choices: Easily extensible with heavy use ofpatterns

Page 5: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Introduction

Swing is the o�cially supported GUI toolkit for Java. It wasdeveloped to replace the GUI components of the Abstract WindowToolkit (AWT). It has the following features:

Pluggable �look and feel� [DEMO]

Numerous standard and specialized widgets (text boxes,buttons, tabbed panels,...)

Swing components render themselves in an OS-independentmanner

Relative layout of components

Nice design choices: Easily extensible with heavy use ofpatterns

Page 6: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Introduction

Swing is the o�cially supported GUI toolkit for Java. It wasdeveloped to replace the GUI components of the Abstract WindowToolkit (AWT). It has the following features:

Pluggable �look and feel� [DEMO]

Numerous standard and specialized widgets (text boxes,buttons, tabbed panels,...)

Swing components render themselves in an OS-independentmanner

Relative layout of components

Nice design choices: Easily extensible with heavy use ofpatterns

Page 7: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Introduction

Swing is the o�cially supported GUI toolkit for Java. It wasdeveloped to replace the GUI components of the Abstract WindowToolkit (AWT). It has the following features:

Pluggable �look and feel� [DEMO]

Numerous standard and specialized widgets (text boxes,buttons, tabbed panels,...)

Swing components render themselves in an OS-independentmanner

Relative layout of components

Nice design choices: Easily extensible with heavy use ofpatterns

Page 8: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Introduction

Swing is the o�cially supported GUI toolkit for Java. It wasdeveloped to replace the GUI components of the Abstract WindowToolkit (AWT). It has the following features:

Pluggable �look and feel� [DEMO]

Numerous standard and specialized widgets (text boxes,buttons, tabbed panels,...)

Swing components render themselves in an OS-independentmanner

Relative layout of components

Nice design choices: Easily extensible with heavy use ofpatterns

Page 9: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Packages

In the past the AWT provided the widgets, but it is generallyadvisable to use only Swing widgets. However, much of thefunctionality provided by AWT has been retained by Swing:

java.awt: Contains superclasses for GUI components, as wellas other useful classes such as Color and Point

java.awt.event: Classes for event handling

javax.swing: Contains the actual GUI components

The Swing components all start with �J�. e.g. JButton, JPanel,JFrame... Avoid the �non-J� versions of these.

Page 10: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Packages

In the past the AWT provided the widgets, but it is generallyadvisable to use only Swing widgets. However, much of thefunctionality provided by AWT has been retained by Swing:

java.awt: Contains superclasses for GUI components, as wellas other useful classes such as Color and Point

java.awt.event: Classes for event handling

javax.swing: Contains the actual GUI components

The Swing components all start with �J�. e.g. JButton, JPanel,JFrame... Avoid the �non-J� versions of these.

Page 11: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Packages

In the past the AWT provided the widgets, but it is generallyadvisable to use only Swing widgets. However, much of thefunctionality provided by AWT has been retained by Swing:

java.awt: Contains superclasses for GUI components, as wellas other useful classes such as Color and Point

java.awt.event: Classes for event handling

javax.swing: Contains the actual GUI components

The Swing components all start with �J�. e.g. JButton, JPanel,JFrame... Avoid the �non-J� versions of these.

Page 12: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Packages

In the past the AWT provided the widgets, but it is generallyadvisable to use only Swing widgets. However, much of thefunctionality provided by AWT has been retained by Swing:

java.awt: Contains superclasses for GUI components, as wellas other useful classes such as Color and Point

java.awt.event: Classes for event handling

javax.swing: Contains the actual GUI components

The Swing components all start with �J�. e.g. JButton, JPanel,JFrame... Avoid the �non-J� versions of these.

Page 13: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Packages

In the past the AWT provided the widgets, but it is generallyadvisable to use only Swing widgets. However, much of thefunctionality provided by AWT has been retained by Swing:

java.awt: Contains superclasses for GUI components, as wellas other useful classes such as Color and Point

java.awt.event: Classes for event handling

javax.swing: Contains the actual GUI components

The Swing components all start with �J�. e.g. JButton, JPanel,JFrame... Avoid the �non-J� versions of these.

Page 14: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Class Diagram

This is a partial class diagram for AWT and Swing:

In orange are the commonly used containers for other components.In blue are some of the commonly used components. What designpattern do you see here?

Page 15: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Aside: Inner Classes

You can create classes within classes. These are known as InnerClasses. Why would you want anything so strange?

Like �helper methods� you can create helper classes which areembedded within a larger class

Inner classes have priviledged access to private members of theouter class

Similar to the friend feature of C++, only more contained

Having small classes declared close to their point of use helpsreadibility and maintainability

Page 16: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Aside: Inner Classes

You can create classes within classes. These are known as InnerClasses. Why would you want anything so strange?

Like �helper methods� you can create helper classes which areembedded within a larger class

Inner classes have priviledged access to private members of theouter class

Similar to the friend feature of C++, only more contained

Having small classes declared close to their point of use helpsreadibility and maintainability

Page 17: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Aside: Inner Classes

You can create classes within classes. These are known as InnerClasses. Why would you want anything so strange?

Like �helper methods� you can create helper classes which areembedded within a larger class

Inner classes have priviledged access to private members of theouter class

Similar to the friend feature of C++, only more contained

Having small classes declared close to their point of use helpsreadibility and maintainability

Page 18: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Aside: Inner Classes

You can create classes within classes. These are known as InnerClasses. Why would you want anything so strange?

Like �helper methods� you can create helper classes which areembedded within a larger class

Inner classes have priviledged access to private members of theouter class

Similar to the friend feature of C++, only more contained

Having small classes declared close to their point of use helpsreadibility and maintainability

Page 19: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Aside: Inner Classes

You can create classes within classes. These are known as InnerClasses. Why would you want anything so strange?

Like �helper methods� you can create helper classes which areembedded within a larger class

Inner classes have priviledged access to private members of theouter class

Similar to the friend feature of C++, only more contained

Having small classes declared close to their point of use helpsreadibility and maintainability

Page 20: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Life of an Inner Class

Each inner class instance is associated with an outer class instance.Consider this example:

class Outer {private int outValue = 6 ;

public int getValue ( ) {return outValue ;

}//

class Inner1 {private int value ;

//Inner1 ( ) {

value = outValue + 1 ;}

//public int getValue ( ) {

return value ;}

}}

Page 21: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Life of an Inner Class

Each inner class instance is associated with an outer class instance.Consider this example:

class Outer {private int outValue = 6 ;

public int getValue ( ) {return outValue ;

}//

class Inner1 {private int value ;

//

Inner1 ( ) {value = outValue + 1 ;

}//

public int getValue ( ) {return value ;

}}

}

Page 22: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Life of an Inner Class

Each inner class instance is associated with an outer class instance.Consider this example:

class Outer {private int outValue = 6 ;

public int getValue ( ) {return outValue ;

}//

class Inner1 {private int value ;

//Inner1 ( ) {

value = outValue + 1 ;}

//

public int getValue ( ) {return value ;

}}

}

Page 23: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Life of an Inner Class

Each inner class instance is associated with an outer class instance.Consider this example:

class Outer {private int outValue = 6 ;

public int getValue ( ) {return outValue ;

}//

class Inner1 {private int value ;

//Inner1 ( ) {

value = outValue + 1 ;}

//public int getValue ( ) {

return value ;}

}}

Page 24: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

To create an instance of an inner class, we must use the followingsyntax:

OuterClass . InnerClass innerObject = outerObject . new InnerClass ( ) ;

Notice how new is treated like a �eld of the outer class. Here wetest our previous example:

public class TestOuter {public static void main ( String [ ] args ) {

Outer outer = new Outer ( ) ;System . out . println ( outer . getValue ( ) ) ;

//Outer . Inner1 inner = outer . new Inner1 ( ) ;System . out . println ( inner . getValue ( ) ) ;

}}

Page 25: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

To create an instance of an inner class, we must use the followingsyntax:

OuterClass . InnerClass innerObject = outerObject . new InnerClass ( ) ;

Notice how new is treated like a �eld of the outer class. Here wetest our previous example:

public class TestOuter {public static void main ( String [ ] args ) {

Outer outer = new Outer ( ) ;System . out . println ( outer . getValue ( ) ) ;

//Outer . Inner1 inner = outer . new Inner1 ( ) ;System . out . println ( inner . getValue ( ) ) ;

}}

Page 26: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

To create an instance of an inner class, we must use the followingsyntax:

OuterClass . InnerClass innerObject = outerObject . new InnerClass ( ) ;

Notice how new is treated like a �eld of the outer class. Here wetest our previous example:

public class TestOuter {public static void main ( String [ ] args ) {

Outer outer = new Outer ( ) ;System . out . println ( outer . getValue ( ) ) ;

//

Outer . Inner1 inner = outer . new Inner1 ( ) ;System . out . println ( inner . getValue ( ) ) ;

}}

Page 27: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

To create an instance of an inner class, we must use the followingsyntax:

OuterClass . InnerClass innerObject = outerObject . new InnerClass ( ) ;

Notice how new is treated like a �eld of the outer class. Here wetest our previous example:

public class TestOuter {public static void main ( String [ ] args ) {

Outer outer = new Outer ( ) ;System . out . println ( outer . getValue ( ) ) ;

//Outer . Inner1 inner = outer . new Inner1 ( ) ;System . out . println ( inner . getValue ( ) ) ;

}}

Page 28: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Nested Classes

The general category to which inner classes belong is nestedclasses. A static nested class is like an inner class but does nothave access to the members of the enclosing class:

class OuterClass {. . .static class StaticNestedClass {

. . .}class InnerClass {

. . .}

}

Example from The Java Tutorials: http://download.oracle.com/javase/tutorial/java/javaOO/nested.html

Static nested classes are basically top-level classes that arepackaged within another class for convenience.

Page 29: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Nested Classes

The general category to which inner classes belong is nestedclasses. A static nested class is like an inner class but does nothave access to the members of the enclosing class:

class OuterClass {. . .static class StaticNestedClass {

. . .}class InnerClass {

. . .}

}

Example from The Java Tutorials: http://download.oracle.com/javase/tutorial/java/javaOO/nested.html

Static nested classes are basically top-level classes that arepackaged within another class for convenience.

Page 30: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Nested Classes

The general category to which inner classes belong is nestedclasses. A static nested class is like an inner class but does nothave access to the members of the enclosing class:

class OuterClass {. . .static class StaticNestedClass {

. . .}class InnerClass {

. . .}

}

Example from The Java Tutorials: http://download.oracle.com/javase/tutorial/java/javaOO/nested.html

Static nested classes are basically top-level classes that arepackaged within another class for convenience.

Page 31: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Local Classes

A local class is a nested class declared within the body of amethod. Note that it still has access to outer class members:

import java . util . ∗ ;

class Celebrity extends Observable { /∗∗/ }//

class MyOuter1 {int i = 0 ;public void doStuff ( ) {

Celebrity britney = new Celebrity ( ) ;//

// Dec l a r e a l o c a l c l a s s as an Oberverclass MyLocalObserver implements Observer {

public void update ( Observable o , Object arg ) {// . . .i++;

}}

//britney . addObserver ( new MyLocalObserver ( ) ) ;

}}

Page 32: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Local Classes

A local class is a nested class declared within the body of amethod. Note that it still has access to outer class members:

import java . util . ∗ ;

class Celebrity extends Observable { /∗∗/ }//

class MyOuter1 {int i = 0 ;public void doStuff ( ) {

Celebrity britney = new Celebrity ( ) ;//

// Dec l a r e a l o c a l c l a s s as an Oberverclass MyLocalObserver implements Observer {

public void update ( Observable o , Object arg ) {// . . .i++;

}}

//britney . addObserver ( new MyLocalObserver ( ) ) ;

}}

Page 33: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Local Classes

A local class is a nested class declared within the body of amethod. Note that it still has access to outer class members:

import java . util . ∗ ;

class Celebrity extends Observable { /∗∗/ }//

class MyOuter1 {int i = 0 ;public void doStuff ( ) {

Celebrity britney = new Celebrity ( ) ;//

// Dec l a r e a l o c a l c l a s s as an Oberverclass MyLocalObserver implements Observer {

public void update ( Observable o , Object arg ) {// . . .i++;

}}

//

britney . addObserver ( new MyLocalObserver ( ) ) ;}

}

Page 34: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Local Classes

A local class is a nested class declared within the body of amethod. Note that it still has access to outer class members:

import java . util . ∗ ;

class Celebrity extends Observable { /∗∗/ }//

class MyOuter1 {int i = 0 ;public void doStuff ( ) {

Celebrity britney = new Celebrity ( ) ;//

// Dec l a r e a l o c a l c l a s s as an Oberverclass MyLocalObserver implements Observer {

public void update ( Observable o , Object arg ) {// . . .i++;

}}

//britney . addObserver ( new MyLocalObserver ( ) ) ;

}}

Page 35: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Anonymous Inner Classes

A local class is a nested class declared within the body of amethod. Note that it still has access to outer class members:

import java . util . ∗ ;

class Celebrity extends Observable { /∗∗/ }//

class MyOuter2 {int i = 0 ;

//public void doStuff ( ) {

Celebrity britney = new Celebrity ( ) ;//

// Crea te an anonymous i n n e r c l a s s and add as an// o b s e r v e r o f b r i t n e y .britney . addObserver ( new Observer ( ) {

public void update ( Observable o , Object arg ) {// . . .i++;

}} ) ;

}}

Page 36: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Anonymous Inner Classes

A local class is a nested class declared within the body of amethod. Note that it still has access to outer class members:

import java . util . ∗ ;

class Celebrity extends Observable { /∗∗/ }//

class MyOuter2 {int i = 0 ;

//

public void doStuff ( ) {Celebrity britney = new Celebrity ( ) ;

//// Crea te an anonymous i n n e r c l a s s and add as an// o b s e r v e r o f b r i t n e y .britney . addObserver ( new Observer ( ) {

public void update ( Observable o , Object arg ) {// . . .i++;

}} ) ;

}}

Page 37: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Anonymous Inner Classes

A local class is a nested class declared within the body of amethod. Note that it still has access to outer class members:

import java . util . ∗ ;

class Celebrity extends Observable { /∗∗/ }//

class MyOuter2 {int i = 0 ;

//public void doStuff ( ) {

Celebrity britney = new Celebrity ( ) ;//

// Crea te an anonymous i n n e r c l a s s and add as an// o b s e r v e r o f b r i t n e y .britney . addObserver ( new Observer ( ) {

public void update ( Observable o , Object arg ) {// . . .i++;

}} ) ;

}}

Page 38: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Anonymous Inner Classes

A local class is a nested class declared within the body of amethod. Note that it still has access to outer class members:

import java . util . ∗ ;

class Celebrity extends Observable { /∗∗/ }//

class MyOuter2 {int i = 0 ;

//public void doStuff ( ) {

Celebrity britney = new Celebrity ( ) ;//

// Crea te an anonymous i n n e r c l a s s and add as an// o b s e r v e r o f b r i t n e y .britney . addObserver ( new Observer ( ) {

public void update ( Observable o , Object arg ) {// . . .i++;

}} ) ;

}}

Page 39: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Aside: Threads

A process is a self contained program with its own resources. Athread is a lightweight process. Threads share the memory spaceof the process that spawned them.

A process with multiple threads is useful in situations where variousclients (e.g. the user, network connections) must be continuallyaddressed. However, they introduce a number of potentialproblems. Concurrency is a big topic (the subject of a wholecourse�7894). In this course you are advised to follow theguidelines for using threads in Swing. If you are sure your projectneeds multiple threads, read the following tutorial:

http://download.oracle.com/javase/tutorial/essential/

concurrency/index.html

Page 40: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Aside: Threads

A process is a self contained program with its own resources. Athread is a lightweight process. Threads share the memory spaceof the process that spawned them.

A process with multiple threads is useful in situations where variousclients (e.g. the user, network connections) must be continuallyaddressed. However, they introduce a number of potentialproblems. Concurrency is a big topic (the subject of a wholecourse�7894). In this course you are advised to follow theguidelines for using threads in Swing. If you are sure your projectneeds multiple threads, read the following tutorial:

http://download.oracle.com/javase/tutorial/essential/

concurrency/index.html

Page 41: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Aside: Threads

A process is a self contained program with its own resources. Athread is a lightweight process. Threads share the memory spaceof the process that spawned them.

A process with multiple threads is useful in situations where variousclients (e.g. the user, network connections) must be continuallyaddressed. However, they introduce a number of potentialproblems. Concurrency is a big topic (the subject of a wholecourse�7894). In this course you are advised to follow theguidelines for using threads in Swing. If you are sure your projectneeds multiple threads, read the following tutorial:

http://download.oracle.com/javase/tutorial/essential/

concurrency/index.html

Page 42: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Guideline for Threads in Swing

Swing is not thread-safe. Willy-nilly usage of threads will createproblems! Possible problems range from unresponsive userinterfaces to bizarre crashes and exceptions.

There are three types of threads to be concerned with:

Initial thread(s): Executes the initial code of your application

The event dispatch thread: Handles all events. Almost all ofyour Swing code will run in this thread. Computationallyexpensive tasks should not be executed here.

Worker or background threads: Handle expensive tasksrunning in the background.

Page 43: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Guideline for Threads in Swing

Swing is not thread-safe. Willy-nilly usage of threads will createproblems! Possible problems range from unresponsive userinterfaces to bizarre crashes and exceptions.

There are three types of threads to be concerned with:

Initial thread(s): Executes the initial code of your application

The event dispatch thread: Handles all events. Almost all ofyour Swing code will run in this thread. Computationallyexpensive tasks should not be executed here.

Worker or background threads: Handle expensive tasksrunning in the background.

Page 44: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Guideline for Threads in Swing

Swing is not thread-safe. Willy-nilly usage of threads will createproblems! Possible problems range from unresponsive userinterfaces to bizarre crashes and exceptions.

There are three types of threads to be concerned with:

Initial thread(s): Executes the initial code of your application

The event dispatch thread: Handles all events. Almost all ofyour Swing code will run in this thread. Computationallyexpensive tasks should not be executed here.

Worker or background threads: Handle expensive tasksrunning in the background.

Page 45: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Guideline for Threads in Swing

Swing is not thread-safe. Willy-nilly usage of threads will createproblems! Possible problems range from unresponsive userinterfaces to bizarre crashes and exceptions.

There are three types of threads to be concerned with:

Initial thread(s): Executes the initial code of your application

The event dispatch thread: Handles all events. Almost all ofyour Swing code will run in this thread. Computationallyexpensive tasks should not be executed here.

Worker or background threads: Handle expensive tasksrunning in the background.

Page 46: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Guideline for Threads in Swing

Swing is not thread-safe. Willy-nilly usage of threads will createproblems! Possible problems range from unresponsive userinterfaces to bizarre crashes and exceptions.

There are three types of threads to be concerned with:

Initial thread(s): Executes the initial code of your application

The event dispatch thread: Handles all events. Almost all ofyour Swing code will run in this thread. Computationallyexpensive tasks should not be executed here.

Worker or background threads: Handle expensive tasksrunning in the background.

Page 47: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

The most important rule is that Swing code should almost alwaysshould be executed in the event-dispatch thread. The code in maincan be considered the initial thread. In Swing applications the mainjob of the initial thread is to create a Runnable object and scheduleit for execution on the event dispatch thread.

Runnable is an interface that consists of nothing but public void

run(). The Runnable created should be passed toSwingUtilities.invokeLater or SwingUtilities.invokeAndWait. In thefollowing, we create an anonymous inner Runnable and callinvokeLater on it:

SwingUtilities . invokeLater ( new Runnable ( ) {public void run ( ) {

createAndShowGUI ( ) ;}

} ) ;

For more details see the Java tutorial on �Concurrency in Swing�:http://download.oracle.com/javase/tutorial/uiswing/

concurrency/index.html

Page 48: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

The most important rule is that Swing code should almost alwaysshould be executed in the event-dispatch thread. The code in maincan be considered the initial thread. In Swing applications the mainjob of the initial thread is to create a Runnable object and scheduleit for execution on the event dispatch thread.

Runnable is an interface that consists of nothing but public void

run(). The Runnable created should be passed toSwingUtilities.invokeLater or SwingUtilities.invokeAndWait. In thefollowing, we create an anonymous inner Runnable and callinvokeLater on it:

SwingUtilities . invokeLater ( new Runnable ( ) {public void run ( ) {

createAndShowGUI ( ) ;}

} ) ;

For more details see the Java tutorial on �Concurrency in Swing�:http://download.oracle.com/javase/tutorial/uiswing/

concurrency/index.html

Page 49: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

The most important rule is that Swing code should almost alwaysshould be executed in the event-dispatch thread. The code in maincan be considered the initial thread. In Swing applications the mainjob of the initial thread is to create a Runnable object and scheduleit for execution on the event dispatch thread.

Runnable is an interface that consists of nothing but public void

run(). The Runnable created should be passed toSwingUtilities.invokeLater or SwingUtilities.invokeAndWait. In thefollowing, we create an anonymous inner Runnable and callinvokeLater on it:

SwingUtilities . invokeLater ( new Runnable ( ) {public void run ( ) {

createAndShowGUI ( ) ;}

} ) ;

For more details see the Java tutorial on �Concurrency in Swing�:http://download.oracle.com/javase/tutorial/uiswing/

concurrency/index.html

Page 50: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

The most important rule is that Swing code should almost alwaysshould be executed in the event-dispatch thread. The code in maincan be considered the initial thread. In Swing applications the mainjob of the initial thread is to create a Runnable object and scheduleit for execution on the event dispatch thread.

Runnable is an interface that consists of nothing but public void

run(). The Runnable created should be passed toSwingUtilities.invokeLater or SwingUtilities.invokeAndWait. In thefollowing, we create an anonymous inner Runnable and callinvokeLater on it:

SwingUtilities . invokeLater ( new Runnable ( ) {public void run ( ) {

createAndShowGUI ( ) ;}

} ) ;

For more details see the Java tutorial on �Concurrency in Swing�:http://download.oracle.com/javase/tutorial/uiswing/

concurrency/index.html

Page 51: Swing: Building GUIs in Java · advisable to use only Swing widgets. However, much of the functionality provided by WTA has been retained by Swing: java.awt: Contains superclasses

Introduction Aside: Inner Classes Aside: Threads Examples

Examples

Code for the following examples will be posted on the coursewebsite.

FirstSwingFrame

SecondSwingFrame

LayoutPlay

Paint1

Paint2

GameOfLife