0% found this document useful (0 votes)
3 views

Advanced Java m3 (2)

The document provides an overview of Java Swing, a GUI toolkit for Java applications, highlighting its lightweight components, pluggable look and feel, and the MVC architecture it supports. It contrasts Swing with AWT, detailing the advantages of Swing's platform independence and richer component set. Additionally, it covers various Swing components, containers, event handling, and includes example code for creating GUI elements such as buttons, text fields, and checkboxes.

Uploaded by

renusagarlrlr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Advanced Java m3 (2)

The document provides an overview of Java Swing, a GUI toolkit for Java applications, highlighting its lightweight components, pluggable look and feel, and the MVC architecture it supports. It contrasts Swing with AWT, detailing the advantages of Swing's platform independence and richer component set. Additionally, it covers various Swing components, containers, event handling, and includes example code for creating GUI elements such as buttons, text fields, and checkboxes.

Uploaded by

renusagarlrlr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

ADVANCED JAVA

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

Java AWT Java Swing


1) AWT components are platform-dependent. Java swing components are platform-independent.
2) AWT components are heavyweight. Swing components are lightweight.
3) AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel.
4) AWT provides less components than Swing. Swing provides more powerful components such
as tables, lists, scrollpanes, colorchooser,
tabbedpane etc.
5) AWT doesn't follows MVC(Model View Swing follows MVC
Controller) where model represents data, view
represents presentation and controller acts as
an interface between model and view.

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 4


The MVC Connection(MVC Architecture)

• 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

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 6


Hierarchy of
Java Swing
classes

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 7


Components and Containers
• Components
Some of the important and common components of the Java Swing class are:
• JButton: JButton is a component that represents a clickable button. It is commonly used to trigger actions in a
GUI application.
• JLabel: JLabel is a component that displays text or an image. It is commonly used to provide information or to
label other components.
• JTextField: JTextField is a component that allows the user to input text. It is commonly used to get input from
the user, such as a name or an address.
• JCheckBox: JCheckBox is a component that represents a checkbox. It is commonly used to get a binary input
from the user, such as whether or not to enable a feature.
• JList: JList is a component that represents a list of elements. It is typically used to display a list of options from
which the user can select one or more items.
• JTable: JTable is a component that represents a data table. It is typically used to present data in a tabular
fashion, such as a list of products or a list of orders.
• JScrollPane: JScrollPane is a component that provides scrolling functionality to other components. It is
commonly used to add scrolling to a panel or a table.

https://www.naukri.com/code360/library/swing-components-in-java

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 8


Components

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 9


Containers

• Containers are an integral part of SWING GUI components. A


container provides a space where a component can be located.
• Example: JFrame, Jpanel, JDialog, JApplet
• Following is the list of commonly used containers while designing
GUI using SWING.
• 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.
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 10
Window v/s Frame

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 11


JPanel vs JFrame

• You can add a JPanel inside a JFrame,


but not vice versa. A Swing GUI
cannot exist without a top-level
container like (JWindow, JFrame or
Applet), while it can exist without a
JPanel.

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 12


Containers

• JFrame: JFrame is a top-level container that represents the main


window of a GUI application. It provides a title bar, and minimizes,
maximizes, and closes buttons.
• JPanel: JPanel is a container that can hold other components. It is
commonly used to group related components together.

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 13


Java Foundation Classes (JFC)

• The Java Foundation Classes (JFC) are a set of GUI components


which simplify the development of desktop applications.

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 14


Methods of Component Class

Method Description

public void add(Component c) It add a component on another


component.

public void setSize(int width,int height) It sets size of the component.

public void setLayout(LayoutManager m) It sets the layout manager for the


component.

public void setVisible(boolean b) It sets the visibility of the component. It is


by default false.
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 15
Creating a frame

There are two ways to create a frame:


1) By creating the object of Frame class (association)
2) By extending Frame class (inheritance)

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 16


Java Swing Example
import javax.swing.*;
public class SwingsExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello, World (Swing)");
JLabel label = new JLabel("Hello, World!");
frame.add(label);
frame.setSize(300, 100);
frame.setVisible(true);
}
}

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 17


import javax.swing.*;
public class FirstSwingExample extends javax.swing.JFrame
{
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
b.setBounds(130,100,100, 40);//x axis, y axis, width, height
f.add(b);//adding button in JFrame
f.setSize(400,500);//400 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 18
ImageIcon in Java Swing
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageIconExample {
public static void main(String[] args) {
JFrame frame = new JFrame("ImageIcon Example");
frame.setSize(800, 600);

// Create an ImageIcon
ImageIcon imageIcon = new ImageIcon("C:\Users\User\Downloads\1160358.png");

// Create a JLabel to display the image


JLabel label = new JLabel(imageIcon);

// Add the label to the frame


frame.add(label);
frame.setVisible(true);
}
}
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 19
JLabel and ImageIcon in Java Swing
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageIconExample {


public static void main(String[] args) {
JFrame frame = new JFrame("ImageIcon Example");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);

// 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

• The class JTextField is a component which allows the editing


of a single line of text.
• JTextField is the simplest Swing text component.
• It is derived from JTextComponent.
• JTextField() Constructor, Constructs a new TextField.

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 21


import javax.swing.*;
class TextFieldExample
{
public static void main(String args[])
{
// Creating a JFrame object with title "TextField Example."
JFrame f= new JFrame("TextField Example");
// Creating two JTextField objects
JTextField t1, t2;
t1 = new JTextField("Welcome to Davanagere.");
// Setting the position and size of the first JTextField
t1.setBounds(50,100, 200,30);
t2 = new JTextField("BIET");
// Setting the position and size of the second JTextField
t2.setBounds(50,150, 200,30);
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 22
// Adding JTextFields to the JFrame
f.add(t1);
f.add(t2);
// Setting the size of the JFrame
f.setSize(400,400);
// Setting layout to null to use absolute positioning
f.setLayout(null);
// Making the JFrame visible
f.setVisible(true);
}
}

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 23


Event Handling
• Changing the state of an object is known as an event.
• For example, click on button, dragging mouse etc.
• The java.awt.event package provides many event classes and
Listener interfaces for event handling
• Event Classes in Java
• ActionEvent - Represents an event that occurs when an action is performed,
such as a button click or menu item selection.
• MouseEvent- Represents an event that occurs when a mouse button is
pressed, released, clicked, or moved.
• KeyEvent - Represents an event that occurs when a key on the keyboard is
pressed, released, or typed.
• ItemEvent - Represents an event that occurs when an item in a list, choice, or
checkbox is selected or deselected.
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 24
Event Handling
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class EventExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Event Example");
frame.setSize(400,300);
JButton button = new JButton("Click Me!");
button.setBounds(100, 200, 50, 25);
frame.add(button);
frame.setVisible(true);

// Create an event listener


ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
};
// Register the event listener
button.addActionListener(listener);
}
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 25
}
JToggleButton
• A JToggleButton is an extension of AbstractButton and it can be
used to represent buttons that can be toggled ON and OFF.
• When JToggleButton is pressed for the first time, it remains
pressed and it can be released only when it is pressed for the
second time.

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 26


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JTBExample{


public static void main(String[] args) {
JFrame frame = new JFrame("Swing Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(560, 500);
frame.setVisible(true);
JToggleButton toggleButton = new JToggleButton("Click Me!");
toggleButton.setBounds(100, 200, 100, 25);
frame.add(toggleButton);

Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 27


toggleButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
if (toggleButton.isSelected()) {
System.out.println("Button is selected");
}
else {
System.out.println("Button is de-selected");
}
}
});

}
}
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

• The JRadioButton class is used to create a radio button. It is used to


choose one option from multiple options.
• It should be added in ButtonGroup to select one radio button only.
Constructors:

• 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.
Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 31
import javax.swing.*;
public class RadioButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame();
f.setSize(300,300);
JRadioButton rb1=new JRadioButton(" Male");
JRadioButton rb2=new JRadioButton(" Female");
rb1.setBounds(75,50,100,30);
rb2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
f.add(rb1);
f.add(rb2);
f.setLayout(null);
f.setVisible(true);
}
} Prof. Santhosh T, Dept. of ISE, BIET, Davanagere 32

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy