AJP Unit 2 Swing
AJP Unit 2 Swing
Swing
Marks :10 Marks
Created By: Prof.P.M.Takate
Introduction to Swing
Swing is a GUI widget toolkit for java.
It is part of Oracle’s Java Foundation Classes –an API for
providing a Graphical User Interface(GUI) for Java programs.
Swing was developed to provide more sophisticated set of GUI
components than the earlier Abstract Windowing ToolKit(AWT).
Swing has more powerful and flexible components than AWT.
In addition to familiar components such as buttons,checkboxes
and labels.Swing provides several advanced components such
as tabbed panel,scroll panes,trees,tables and so on.
Swing is an extension to the AWT components which provides
feature of pluggable look and feel for the components.
Fig.AWT and Swing Classes
Swing Features
1. Platform Independent: It is platform independent, the swing
components that are used to build the program are not platform
specific. It can be used at any platform and anywhere.
2. Lightweight: Swing components are lightweight which helps in
creating the UI lighter. Swings component allows it to plug into the
operating system user interface framework that includes the
mappings for screens or device and other user interactions like key
press and mouse movements.
3. Plugging: It has a powerful component that can be extended to
provide the support for the user interface that helps in good look and
feel to the application. Its components are imported through a
package called java. swing.
4. Manageable: It is easy to manage and configure. Its mechanism
and composition pattern allows changing the settings at run time as
well. The uniform changes can be provided to the user interface
without doing any changes to application code.
5. MVC: They mainly follows the concept of MVC that is Model
View Controller. With the help of this, we can do the changes
in one component without impacting or touching other
components. It is known as loosely coupled architecture as
well.
6.Highly Customizable − Swing controls can be customized
in a very easy way as visual appearance is independent of
internal representation.
MVC Architecture
Fig.MVC Architecture
Model represents component's data.
View represents visual representation of the
component's data.
Controller takes the input from the user on the view and
reflects the changes in Component's data.
Swing component has Model as a separate element,
while the View and Controller part are clubbed in the
User Interface elements. Because of which, Swing has a
pluggable look-and-feel architecture.
Difference between AWT and Swing
No. Java AWT Java Swing
1) AWT components are platform- Java swing components
dependent. are platform-independent.
SR Constructor Description
No
1 JFrame() It constructs a new frame that is initially
invisible.
f.setLayout(new FlowLayout());
JButton()
1
Creates a button with no set text or icon.
JButton(Icon icon)
2
Creates a button with an icon.
JButton(String text)
3
Creates a button with the text.
JButton(String text, Icon icon)
4
Creates a button with an initial text and an icon.
Methods :
7 JCheckBox(String text, Icon Creates a checkbox with text and icon, and
Program : Output:
import javax.swing.*;
public class CheckBoxExample
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckBoxExample();
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.
Constructors :
SR.NO Constructor Description
{ root.add(b);
{ int
v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_
DefaultMutableTreeNode root=new NEEDED;
DefaultMutableTreeNode("Java");
int
DefaultMutableTreeNode a=new h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_A
DefaultMutableTreeNode ("AWT"); S_NEEDED;
root.add(a); JScrollPane jsp=new JScrollPane(tree,v,h);
DefaultMutableTreeNode a1=new add(jsp);
DefaultMutableTreeNode("Label");
}
a.add(a1);
}
DefaultMutableTreeNode a2=new
DefaultMutableTreeNode("Button"); /*
a.add(a3); */
JTable
The JTable class is used to display data in tabular form. It is composed
of rows and columns.
Constructors :