The GUI toolbox Swing
Examples
Note
: the two examples
SwingApplication and CoordinatesDemo come from the tutorial about the
SDK5.0. These examples are no longer included
in the current tutorial, which is mainly based on the use of the IDE
NetBeans.
Hello World
The two following examples give the same result, but using two
different implementations for handling the "action" event on the button
:
- In this
version , the main class implements ActionListener
and then, is used as instance of ActionListener
to listen the button.
- In this
version, the instance of ActionListener
is written using an anonymous inner class.
SwingApplication
This example of
a simple application comes from the Java tutorial.
Coordinates Demo
This example,
which explains how to draw in a component, comes from the Java
tutorial.
Layout Demos
These examples
come from the Java tutorial. The most useful layout managers are (in
my opinion) :
- Border Layout
- Box Layout
- Flow Layout
- Grid Layout
- GridBag Layout
Browser
This example shows how to implement a simple web browser. More
specifically, it contains a generic history.
- First version
- The history is implemented in a generic way in the class History. Then it
may be reused in any context where a history is needed. History
extends Observable
and it is this class which will activate the updates of the two others
components Browser
and HistoryBar,
which are instances of Observer.
- HistoryBar
uses two images for its buttons : HorizontalPrevious.png
et HorizontalNext.png,
placed in a directory named images.
- When a web page cannot be loaded, a default page, Failure.html,
is used to prevent the user.
- Version with multiple
windows and a menu
- In this version, classes History and HistoryBar are
unchanged.
- The class Browser
has been renamed BrowserFrame.
The frame is completed by adding a menu "File" allowing to create or
close a frame, or to quit the application. Setting the size and showing
the frame is left to the responsibility of the client of the class BrowserFrame.
- Frames are managed by a FrameController,
implemented here by the class Browser. Please
note the use of a dialog to confirm the intention to quit the program.