Java Swing Tutorial
Java Swing Tutorial
Java Swing Tutorial
Java 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.
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
Difference between AWT and Swing
There are many differences between java awt and swing that are given below.
No.
Java AWT
Java Swing
1)
AWT
components
dependent.
are platform-
2)
3)
4)
AWT
provides less
Swing.
Swing
provides more
powerful
components such as tables, lists,
scrollpanes, colorchooser, tabbedpane
etc.
5)
components than
What is JFC
The Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.
Description
sets
the
layout
component.
manager
for
the
Containers
Containers are integral part of SWING GUI components. A container provides a space
where a component can be located. A Container in AWT is a component itself and it
adds the capability to add component to itself. Following are noticable points to be
considered.
Sub classes of Container are called as Container. For example JPanel, JFrame and
JWindow.
Container can add only Component to itself.
A default layout is present in each container which can be overridden using
setLayout method.
SWING Containers:
Following is the list of commonly used containers while designed GUI using SWING.
Sr.
No.
Panel
JPanel is the simplest container. It provides space in which any
other component can be placed, including other panels.
Frame
A JFrame is a top-level window with a title and a border
Window
A JWindow object is a top-level window with no borders and no
menubar.
warning to a user, but Dialogs can present images, directory trees, or just about
anything compatible with the main Swing Application that manages them.
For convenience, several Swing component classes can directly instantiate and
display dialogs. To create simple, standard dialogs, you use the JOptionPane class.
The ProgressMonitor class can put up a dialog that shows the progress of an operation.
Two other classes, JColorChooser and JFileChooser, also supply standard dialogs. To
bring up a print dialog, you can use the Printing API. To create a custom dialog, use
the JDialog class directly.
The code for simple dialogs can be minimal. For example, here is an informational
dialog:
"Eggs
are
not
supposed
to
be
JPanel:
The class JPanel is a generic lightweight container.
Class constructors
S.N.
JPanel()
Creates a new JPanel with a double buffer and a flow layout.
JPanel(boolean isDoubleBuffered)
Creates a new JPanel with FlowLayout and the specified buffering strategy.
JPanel(LayoutManager layout)
Create a new buffered JPanel with the specified layout manager.
Class methods
S.N.
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JPanel.
PanelUI getUI()
Returns the look and feel (L&F) object that renders this component.
String getUIClassID()
Returns a string that specifies the name of the L&F class that renders this component.
void updateUI()
Resets the UI property with a value from the current look and feel.