Written by
Shraddha Sheth
Neel Shah
o Creating Windows
o Creating a Window
o Components and Containers
o Basics of Components
o Using Containers
o Containers Layout Managers
o Adding a Menu to a Window
o Applets

JFC – JavaTM
Foundation Classes
Encompass a group of features for constructing graphical
user interfaces (GUI).
Implemented without any native code.
“Swing” is the codename of the project that developed the
first JFC components (JFC 1.11
).
The name “Swing” is frequently used to refer to new
components and related API.
JFC includes:
The Swing Components
Dialog, Tabbed pane, Buttons, File Chooser, ...
Pluggable Look and Feel
Accessibility API
Screen readers, Braille displays, ...
Java 2DTM
API (Java 2 Platform only)
Drag and Drop (Java 2 Platform only)
Between Java applications and native applications.
Each picture shows the same program but with
a different look and feel
javax.swing.plaf.metal.MetalLookAndFeel
javax.swing.plaf.motif.MotifLookAndFeel
Javax.swing.plaf.windows.WindowsLookAn
dFeel
import javax.swing.UIManager;
static setLookAndFeel() method that is defined in the
UIManager class.
This method can throw an exception of
ClassNotFoundException if the look-and-feel class cannot be
found,
For example:
try {
 UIManager.setLookAndFeel(“com.sun.java.swing.plaf.motif.MotifL
ookAndFeel”);
} catch(Exception e) {
 System.err.println(“Look and feel not set.”);
}
UIManager.LookAndFeelInfo[]
looks = UIManager.getInstalledLookAndFeels();
for( UIManager.LookAndFeelInfo look : looks)
System.out.println(look.getClassName());
Java Default Look And Feel
UIManager.setLookAndFeel
( UIManager.getCrossPlatformLookAndFeel());
Swing provides many standard GUI components
such as buttons, lists, menus, and text areas,
which you combine to create your program's GUI.
Swing provides containers such as a window.
top level: frames, dialogs
intermediate level: panel, scroll pane, tabbed pane, ...
View Components
Descendents of the java.awt.Container class
Components that can contain other components.
Use a layout manager to position and size the
components contained in them.
Components are added to a container using one
of the various forms of its add method
Depending on which layout manager is used by the
container
panel.add(component);
Every program that presents a Swing GUI
contains at least one top-level container.
A Top level container provides the support that
Swing components need to perform their painting
and event-handling.
Swing provides four top-level containers:
JFrame (Main window)
JDialog (Base For Dialogs)
JApplet (An applet display area within a browser
window)
JWindow (Secondary Display Devices)
To appear on screen, every GUI component must be part
of a containment hierarchy, with a top-level container
as its root.
Each top-level container has a content pane that contains
visible components in that top-level container’s GUI.
A top-level container can not contain another top
level container.
A frame implemented as an instance of the JFrame
class, is a window that has decorations such as a
border, a title and buttons for closing and iconifying
the window.
Applications with a GUI typically use at least one
frame.
import javax.swing.*;
public class HelloWorldSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("HelloWorldSwing");
final JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label); // 1.4
// OR USE THIS frame.add(label); JDK 5
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Position (x,y)
Name .setName() .getName();
Size
Foreground And Background Color
Font
Cursor
State (setEnabled ([True/False])) isEnable()
Visible (setVisible([True/False])) isVisible()
Valid isValid()
Position is defined by x and y coordinates of type int,
or by an object of type java.awt.Point.
Size is defined by width and height, also values of
type int, or by an object of type java.awt.Dimension.
A Rectangle specifies an area in a coordinate space
that is enclosed by the Rectangle object's upper-left
point (x,y) in the coordinate space, its width, and its
height.
The decorations on a frame are platform dependent.
A JApplet object has the same arrangement of panes
as a JFrame object.
Window class and its subclasses, as objects of type
Window (or of a subclass type) can’t be contained in
another container.

Java Swing

  • 1.
  • 2.
    o Creating Windows oCreating a Window o Components and Containers o Basics of Components o Using Containers o Containers Layout Managers o Adding a Menu to a Window o Applets 
  • 3.
    JFC – JavaTM FoundationClasses Encompass a group of features for constructing graphical user interfaces (GUI). Implemented without any native code. “Swing” is the codename of the project that developed the first JFC components (JFC 1.11 ). The name “Swing” is frequently used to refer to new components and related API.
  • 4.
    JFC includes: The SwingComponents Dialog, Tabbed pane, Buttons, File Chooser, ... Pluggable Look and Feel Accessibility API Screen readers, Braille displays, ... Java 2DTM API (Java 2 Platform only) Drag and Drop (Java 2 Platform only) Between Java applications and native applications.
  • 5.
    Each picture showsthe same program but with a different look and feel javax.swing.plaf.metal.MetalLookAndFeel javax.swing.plaf.motif.MotifLookAndFeel Javax.swing.plaf.windows.WindowsLookAn dFeel
  • 6.
    import javax.swing.UIManager; static setLookAndFeel()method that is defined in the UIManager class. This method can throw an exception of ClassNotFoundException if the look-and-feel class cannot be found, For example: try {  UIManager.setLookAndFeel(“com.sun.java.swing.plaf.motif.MotifL ookAndFeel”); } catch(Exception e) {  System.err.println(“Look and feel not set.”); }
  • 7.
    UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels(); for(UIManager.LookAndFeelInfo look : looks) System.out.println(look.getClassName()); Java Default Look And Feel UIManager.setLookAndFeel ( UIManager.getCrossPlatformLookAndFeel());
  • 8.
    Swing provides manystandard GUI components such as buttons, lists, menus, and text areas, which you combine to create your program's GUI. Swing provides containers such as a window. top level: frames, dialogs intermediate level: panel, scroll pane, tabbed pane, ... View Components
  • 9.
    Descendents of thejava.awt.Container class Components that can contain other components. Use a layout manager to position and size the components contained in them. Components are added to a container using one of the various forms of its add method Depending on which layout manager is used by the container panel.add(component);
  • 11.
    Every program thatpresents a Swing GUI contains at least one top-level container. A Top level container provides the support that Swing components need to perform their painting and event-handling. Swing provides four top-level containers: JFrame (Main window) JDialog (Base For Dialogs) JApplet (An applet display area within a browser window) JWindow (Secondary Display Devices)
  • 13.
    To appear onscreen, every GUI component must be part of a containment hierarchy, with a top-level container as its root. Each top-level container has a content pane that contains visible components in that top-level container’s GUI. A top-level container can not contain another top level container.
  • 15.
    A frame implementedas an instance of the JFrame class, is a window that has decorations such as a border, a title and buttons for closing and iconifying the window. Applications with a GUI typically use at least one frame.
  • 17.
    import javax.swing.*; public classHelloWorldSwing { public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); // 1.4 // OR USE THIS frame.add(label); JDK 5 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
  • 21.
    Position (x,y) Name .setName().getName(); Size Foreground And Background Color Font Cursor State (setEnabled ([True/False])) isEnable() Visible (setVisible([True/False])) isVisible() Valid isValid()
  • 22.
    Position is definedby x and y coordinates of type int, or by an object of type java.awt.Point. Size is defined by width and height, also values of type int, or by an object of type java.awt.Dimension. A Rectangle specifies an area in a coordinate space that is enclosed by the Rectangle object's upper-left point (x,y) in the coordinate space, its width, and its height.
  • 32.
    The decorations ona frame are platform dependent. A JApplet object has the same arrangement of panes as a JFrame object. Window class and its subclasses, as objects of type Window (or of a subclass type) can’t be contained in another container.