2. Containers
• A container holds a group of components.
• Thus, a container is a special type of component that is designed to hold other
components.
• Furthermore, in order for a component to be displayed, it must be held within
a container.
• Thus, all Swing GUIs will have at least one container.
• Because containers are components, a container can also hold other
containers.
• This enables Swing to define what is called a containment hierarchy, at the top
of which must be a top-level container.
• Swing defines two types of containers.
• The first are top-level containers: JFrame, JApplet, JWindow, and JDialog.
These containers do not inherit JComponent. They do, however, inherit the
AWT classes Component and Container. Unlike Swing’s other components,
which are lightweight, the top-level containers are heavyweight. This makes
the top-level containers a special case in the Swing component library.
3. Java JFrame
• The javax.swing.JFrame class is a type of container which inherits the
java.awt.Frame class. JFrame works like the main window where
components like labels, buttons, textfields are added to create a GUI.
• Unlike Frame, JFrame has the option to hide or close the window with the
help of setDefaultCloseOperation(int) method.
Constructor Description
JFrame() It constructs a new frame that is initially
invisible.
JFrame(GraphicsConfiguration gc) It creates a Frame in the specified
GraphicsConfiguration of a screen device and
a blank title.
JFrame(String title) It creates a new, initially invisible Frame
with the specified title.
JFrame(String title,
GraphicsConfiguration gc)
It creates a JFrame with the specified title
and the specified GraphicsConfiguration of a
screen device.
4. JLabel
• The class JLabel can display either text, an image, or both. Label's
contents are aligned by setting the vertical and horizontal alignment in its
display area. By default, labels are vertically centered in their display area.
Text-only labels are leading edge aligned, by default; image-only labels are
horizontally centered, by default.
• Following is the declaration for javax.swing.JLabel class −
• public class JLabel extends JComponent implements SwingConstants,
Accessible
Constructor Description
JLabel() Creates a JLabel instance with no image and
with an empty string for the title.
JLabel(String s) Creates a JLabel instance with the specified
text.
JLabel(Icon i) Creates a JLabel instance with the specified
image.
JLabel(String s, Icon i, int
horizontalAlignment)
Creates a JLabel instance with the specified
text, image, and horizontal alignment.
5. Constructor & Description
1
JLabel()
Creates a JLabel instance with no image and with an empty
string for the title.
2
JLabel(Icon image)
Creates a JLabel instance with the specified image.
3
JLabel(Icon image, int horizontalAlignment)
Creates a JLabel instance with the specified image and
horizontal alignment.
4
JLabel(String text)
Creates a JLabel instance with the specified text.
5
JLabel(String text, Icon icon, int horizontalAlignment)
Creates a JLabel instance with the specified text, image, and
horizontal alignment.
6
JLabel(String text, int horizontalAlignment)
Creates a JLabel instance with the specified text and horizontal
alignment.
6. Java JPanel
• The JPanel is a simplest container class. It provides space in which an
application can attach any other component. It inherits the JComponents
class.
• It doesn't have title bar.
• JPanel class declaration
• public class JPanel extends JComponent implements Accessible
Constructor Description
JPanel() It is used to create a new JPanel
with a double buffer and a flow
layout.
JPanel(boolean
isDoubleBuffered)
It is used to create a new JPanel
with FlowLayout and the specified
buffering strategy.
JPanel(LayoutManager
layout)
It is used to create a new JPanel
with the specified layout manager.
7. JRadioButton
• The JRadioButton class is used to create a radio button. It is used to choose one
option from multiple options. It is widely used in exam systems or quiz.
• It should be added in ButtonGroup to select one radio button only.
• JRadioButton class declaration
• Let's see the declaration for javax.swing.JRadioButton class.
• public class JRadioButton extends JToggleButton implements Accessible
Constructor Description
JRadioButton() Creates an unselected radio
button with no text.
JRadioButton(String s) Creates an unselected radio
button with specified text.
JRadioButton(String s, boolean
selected)
Creates a radio button with the
specified text and selected
status.
8. Elements are then added to the button group via the following
method:
void add(AbstractButton ab)
Here, ab is a reference to the button to be added to the group.
A JRadioButton generates action events, item events, and change
events each time the button selection changes.
Most often, it is the action event that is handled, which means that
you will normally implement the ActionListener interface.
9. Java JButton
• The JButton class is used to create a labeled button that has platform
independent implementation.
• The application result in some action when the button is pushed. It
inherits AbstractButton class.
• JButton class declaration
• void setIcon(Icon b)It is used to set the specified Icon on the button.
• public class JButton extends AbstractButton implements Accessible
Constructor Description
JButton() It creates a button with no text and icon.
JButton(String s) It creates a button with the specified text.
JButton(Icon i) It creates a button with the specified icon
object.
10. Java JTabbedPane
• The JTabbedPane class is used to switch between a group of components
by clicking on a tab with a given title or icon. It inherits JComponent class.
• JTabbedPane class declaration
• Let's see the declaration for javax.swing.JTabbedPane class.
• public class JTabbedPane extends JComponent implements Serializable, A
ccessible, SwingConstants
Constructor Description
JTabbedPane() Creates an empty TabbedPane
with a default tab placement of
JTabbedPane.Top.
JTabbedPane(int tabPlacement) Creates an empty TabbedPane
with a specified tab placement.
JTabbedPane(int tabPlacement,
int tabLayoutPolicy)
Creates an empty TabbedPane
with a specified tab placement
and tab layout policy.
11. Java JDialog
• The JDialog control represents a top level window with a border and a title
used to take some form of input from the user. It inherits the Dialog class.
• Unlike JFrame, it doesn't have maximize and minimize buttons.
• JDialog class declaration
• Let's see the declaration for javax.swing.JDialog class.
• public class JDialog extends Dialog implements WindowConstants, Accessible,
RootPaneContainer
Constructor Description
JDialog() It is used to create a modeless
dialog without a title and without
a specified Frame owner.
JDialog(Frame owner) It is used to create a modeless
dialog with specified Frame as its
owner and an empty title.
JDialog(Frame owner, String title,
boolean modal)
It is used to create a dialog with
the specified title, owner Frame
and modality.