Swing Components and the Containment Hierarchy
Swing
application creates four commonly used Swing components:
i) a frame, or main window (
JFrame
)
ii) an applet,
called JApplet
iii) a panel,
sometimes called a pane (
JPanel
)
iv) lots of
other types of containers such as JScrollPane, JTabbedPane, JInternalFrame etc
The hierarchy of
classes used is as follows –
Every top-level
container indirectly contains an intermediate container known as a content
pane. The content pane contains,
directly or indirectly, all of the visible components in the window's GUI.
1. Component
A component
is an object having a graphical representation that can be displayed on the
screen and that can interact with the user. Examples of components are the
buttons, checkboxes, and scrollbars of a typical graphical user interface. It contains basic methods such as setting the
size, background/foreground colors that are associated with every GUI element.
2. Container
A container is a
component that has the ability to hold other components. It also takes of alignment and sizing of
components that are present in it. This
is done using Layout manager classes.
3. Window
A
Window
object is a top-level window with no borders and no menubar. The default layout
for a window is BorderLayout
.
4. Frame
A
Frame
is a
top-level window with a title and a border.
5. Panel
Panel
is the simplest
container class. A panel provides space in which an application can attach any
other component, including other panels.
The default layout manager for a panel is the FlowLayout
layout manager.
6. Applet
An applet is a
small program that is intended not to be run on its own, but rather to be
embedded inside another application. The
Applet
class must be
the superclass of any applet that is to be embedded in a Web page or viewed by
the Java Applet Viewer.
7. JFrame
JFrame is an
extended version of
java.awt.Frame
that adds support for the JFC/Swing component
architecture. Like all other JFC/Swing top-level
containers, a
JFrame
contains a JRootPane
as its only child. The content pane provided by the root pane
should, as a rule, contain all the non-menu components displayed by the JFrame
.
8. JApplet
JApplet is an
extended version of java.applet.Applet that adds support for the JFC/Swing
component architecture.
9. Canvas
A
Canvas
component represents a blank rectangular area of the screen onto which the
application can draw or from which the application can trap input events from
the user. It cannot be used to
add/remove components.
The different
types of swing components such as – JList, JButton etc. are derived from
JComponent.
Drawing in AWT/Swing
1. paint ( Graphics g )
This method is
called when the contents of the component should be painted; such as when the
component is first being shown. The
Graphics
class is the abstract base class for all graphics contexts that allow an
application to draw onto components that are realized on various devices, as
well as onto off-screen images. It
contains methods such as setColor, drawRect, fillOval etc.
2. paintComponent ( Graphics g)
Swing programs should override
paintComponent()
instead of overriding paint()
. Each time a window needs to be redrawn, no matter what
the reason, the event handler notifies the component. This causes the paintComponent
methods of
all components to be executed. When a
component calls its own paintComponent method, it draws in its own window. But this may affect other components in the main
component. That’s why you need to call
super.paintComponent method.
0 comments: