0% found this document useful (0 votes)
21 views

Swing in Java

Java Swing is a GUI toolkit that provides lightweight components built on top of AWT. Swing uses a containment hierarchy with containers like JFrame and JPanel that hold atomic components like JButton and JLabel. Containers have content panes where components are added.

Uploaded by

Riya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Swing in Java

Java Swing is a GUI toolkit that provides lightweight components built on top of AWT. Swing uses a containment hierarchy with containers like JFrame and JPanel that hold atomic components like JButton and JLabel. Containers have content panes where components are added.

Uploaded by

Riya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Swing in Java

• Java Swing is a part of Java Foundation Classes (JFC) that is used to


create window-based applications. It is built on the top of AWT
(Abstract Windowing Toolkit) API and entirely written in java.

• Unlike AWT, Java Swing provides platform-independent and


lightweight components. Swing classes are defined in javax.swing
package
Components and Containers
• The AWT set is structured in a class hierarchy; every UI component is
a descendant of the Component class.
• All AWT components are derived from the package
java.awt.Container;
• A Container is a special type of component to which you can add
other components. Eg:JFrame, JPanel, JApplet etc.
The Swing Containment Hierarchy
A Swing containment hierarchy typically has at least:
 a top-level container: the root container that holds everything together. e.g. JApplet,
JFrame etc.
 an intermediate container: a container to simplify the positioning of atomic
components. e.g. JPanel, JScrollPane
 an atomic component: a self-sufficient component not holding other components.
eg. JButton, JLabel
 A component cannot be added directly to the top-level container, but must be
added to an intermediate container contained in the top-level container, called the
content pane. This is done by calling the method getContentPane() that returns a
Container instance.
• A container has several layers in it. You can think of a layer as a
transparent film that overlays the container. In Java Swing, the layer
that is used to hold objects is called the content pane. Objects are
added to the content pane layer of the container. The
getContentPane() method retrieves the content pane layer so that
you can add an object to it.
Panel

Applet

JWindow

JFrame
• JFrame – A frame is an instance of JFrame. Frame is a window that
can have title, border, menu, buttons, text fields and several other
components. A Swing application must have a frame to have the
components added to it.

• JPanel – A panel is an instance of JPanel. A frame can have more than


one panels and each panel can have several components. You can also
call them parts of Frame. Panels are useful for grouping components
and placing them to appropriate locations in a frame.
Difference between AWT and Swing

No. Java AWT Java Swing


1) AWT components are platform- Java swing components are
dependent. platform-independent.
AWT components are
2) Swing components are lightweight.
heavyweight.
3) AWT doesn't support pluggable Swing supports pluggable look and
look and feel. feel.
Swing provides more powerful
4) AWT provides less components components such as tables, lists,
than Swing. scrollpanes, colorchooser,
tabbedpane etc.

You might also like