Advanced Java m3 (2)
Advanced Java m3 (2)
BIS402
Module 3
Java Swing
• Swing is used to create a graphical user interface for a Java-based
application. An interface is a dashboard that allows a user to
interact with the system.
• The Java Swing is called lightweight because its components are
entirely implemented in Java.
• Java Swing was introduced as part of the Java Foundation Classes
(JFC) in the late 1990s, to address the limitations of the Abstract
Window Toolkit (AWT).
• Swing provides a comprehensive set of components for building
GUIs, including buttons, text fields, panels, and more.
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 2
Two Key Swing Features
• Swing Components Are Lightweight
• Swing components are lightweight. This means that they are written entirely in Java,
and they are platform-independent, as the swing components used to construct the
program are not platform-specific. It works on any platform and in any location.
• Examples of Swing components include buttons, labels, text fields, tables, and trees.
• Swing Supports a Pluggable Look and Feel
• Swing supports a pluggable look and feel (PLAF). Because each Swing component is
rendered by Java code and the look and feel of a component is under the control of
Swing.
• The L&F can be changed at runtime, allowing applications to adapt to different
platforms or user preferences.
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 3
Difference Between AWT and Swing
• MVC framework is used to separate the data access layer, business logic
code and the graphical user interface that users or be defined and
designed to let the user interact with the application.
• This application has three parts:
• Model - This part of the framework is to store the data of the application, such as
databases, text data, files and/or other web resources.
• View - This is the graphical user interface of the application. That would contain
different buttons, text boxes and other controls to let the user interact with the
application to complete his projects depending on the sort of the software he is
using.
• Controller - The actual back-end code constitutes the controller of the
framework. A controller controls the data coming from the users or going to the
user from a model.
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 5
MVC Architecture
https://www.naukri.com/code360/library/swing-components-in-java
Method Description
// Create an ImageIcon
ImageIcon imageIcon = new ImageIcon("C:\Users\User\Downloads\1160358.png");
// Create an ImageIcon
ImageIcon imageIcon = new ImageIcon("C:\\Users\\SANTHU\\Downloads\\images1.png");
// Create a label.
JLabel jl = new JLabel("Hourglass", imageIcon, JLabel.CENTER);
frame.add(jl);
frame.setVisible(true);
}
} Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 20
JTextField
}
}
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 28
CheckBoxes
• The JCheckBox class is used to create a checkbox.
• Clicking on a CheckBox changes its state from "on" to "off" or from "off"
to "on”.
Constructors:
• JCheckBox() - Creates an initially unselected check box button with
no text and no icon.
• JCheckBox(String text) - Creates an initially unselected
checkbox with text.
• JCheckBox(String text, boolean selected) - Creates a
checkbox with the text and specifies whether or not it is initially
selected.
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 29
import javax.swing.*;
public class CheckBoxExample { JCheckBoxes
public static void main(String args[])
{
JFrame f= new JFrame("CheckBox Example");
JCheckBox cb1 = new JCheckBox("C++");
cb1.setBounds(100,100, 100,50);
JCheckBox cb2 = new JCheckBox("Java", true);
cb2.setBounds(100,150, 100,50);
f.add(cb1);
f.add(cb2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
} Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 30
JRadioButton