27 Lecture
27 Lecture
Event Handling
●
Event
●
Event Source
●
Event Classes
●
Event Listeners
●
Delegation Event Model
●
Mouse and Keyboard Events
●
17/10/2023 2
Japplet
●
Icons and Labels
●
Text Fields
●
Buttons,
●
Combo Boxes ,
●
Checkboxes,
●
Tabbed Panes,
●
Scroll Panes,
●
Trees: Tables
17/10/2023 3
Japplet
●
Class Japplet
Hierarchy of Japplet
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
javax.swing.JApplet
17/10/2023 4
1. Implement the ActionListener
interface
17/10/2023 5
● Register the component with the
listener
component.addActionListener(insta
nce of Listener)
17/10/2023 6
override the actionPerformed
method
public void
actionPerformed(ActionEvent e)
{ Code }
17/10/2023 7
●
Java ActionListener Interface
The Java ActionListener is notified whenever you click on the
button or menu item.
It is notified against ActionEvent.
The ActionListener interface is found in java.awt.event package.
It has only one method: actionPerformed().
actionPerformed() method
The actionPerformed() method is invoked automatically whenever
you click on the registered component.
public abstract void actionPerformed(ActionEvent e);
17/10/2023 8
Swing Components
●
JButton
●
JLabel
●
Text Components
●
JtextField
●
JtextArea
●
JPasswordField
●
JCheckBox
●
JRadioButton
17/10/2023 9
●
JComboBox
●
JList
●
JTable
●
JTabbedPane
●
JDialog
●
JPanel
●
JFrame
●
17/10/2023 10
●
JFileChooser
●
JToggleButton
●
JToolBar
●
JViewport
17/10/2023 11
●
Java Swing Apps
●
Notepad
●
Calculator
●
Word Counter
●
URL Source Generator
●
Folder Explorer
●
Puzzle Game
●
Tic Tac Toe Game
17/10/2023 12
Java Utilities (java.util Package)
●
Contains the collections framework, some internationalization
support classes, a service loader, properties, random number
generation, string parsing and scanning classes, base64 encoding
and decoding, a bit array, and several miscellaneous utility
classes. This package also contains legacy collection classes and
legacy date and time classes
17/10/2023 13
Packages of java.util
java.util.concurrent - Utility classes commonly useful in
concurrent programming.
●
●
java.util.function - Functional interfaces provide target types for
lambda expressions and method references.
●
●
java.util.jar - Provides classes for reading and writing the JAR
(Java ARchive) file format, which is based on the standard ZIP file
format with an optional manifest file.
●
17/10/2023 14
●
java.util.logging - Provides the classes and interfaces of the
Java 2 platform's core logging facilities.
●
●
java.util.prefs - This package allows applications to store and
retrieve user and system preference and configuration data.
●
●
java.util.random - This package contains classes and interfaces
that support a generic API for random number generation.
17/10/2023 15
●
java.util.regex - Classes for matching character sequences
against patterns specified by regular expressions.
●
java.util.stream - Classes to support functional-style operations
on streams of elements, such as map-reduce transformations on
collections.
●
java.util.zip - Provides classes for reading and writing the
standard ZIP and GZIP file formats.
17/10/2023 16
Class & Interfaces
●
Arrays - This class contains various methods for manipulating
arrays (such as sorting and searching).
●
List<E> - An ordered collection (also known as a sequence).
●
ArrayList<E> - Resizable-array implementation of the List
interface.
●
17/10/2023 17
●
Calendar - The Calendar class is an abstract class that provides
methods for converting between a specific instant in time and a
set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH,
HOUR, and so on, and for manipulating the calendar fields, such
as getting the date of the next week.
●
Calendar.Builder - Calendar.Builder is used for creating a
Calendar from various date-time parameters.
●
Date - The class Date represents a specific instant in time, with
millisecond precision.
●
17/10/2023 18
●
Why the Collections Framework?
The Java collections framework provides various data structures
and algorithms that can be used directly. This has two main
advantages:
●
We do not have to write code to implement these data
structures and algorithms manually.
●
Our code will be much more efficient as the collections
framework is highly optimized.
●
Moreover, the collections framework allows us to use a specific
data structure for a particular type of data. Here are a few
examples,
●
If we want our data to be unique, then we can use the Set
17/10/2023 19
interface provided by the collections framework.
17/10/2023 20
17/10/2023 21
List Interface
●
The list is a child interface of Collections in java.
●
Insertion order preserved i.e., They appear in the same order in
which we inserted.
●
Duplicate elements are allowed.
●
List Interface is implemented by using ArrayList, LinkedList, and
Vector class.
17/10/2023 22
A List is an ordered Collection (sometimes called a sequence). Lists
may contain duplicate elements. In addition to the operations
inherited from Collection,
17/10/2023 23
List interface includes operations for the
●
following:
Positional access — manipulates elements based on their
numerical position in the list. This includes methods such as get,
set, add, addAll, and remove.
● Search — searches for a specified object in the list and returns its
numerical position. Search methods include indexOf and
lastIndexOf.
● Iteration — extends Iterator semantics to take advantage
of the list's sequential nature. The listIterator methods
provide this behavior.
● Range-view — The sublist method performs arbitrary range
17/10/2023 24
operations on the list.
ArrayList
●
●
ArrayList is a class present in java. util package.
●
It provides a dynamic array for storing the element.
●
It is an array but there is no size limit.
●
We can add or remove elements easily.
●
It is more flexible than a traditional array.
17/10/2023 25
Methods in ArrayList
●
get(object o) It prints the value at a specific index.
●
set(index, object o) It updates the value. In that, we need to
provide an index.
●
add(index, object o) It adds an element at a specific index.
●
remove(Object o) It removes elements at specific indexes.
●
Sort() It sorts an array depending upon the data type.
●
addAll(Collection c) It is used to add another collection.
●
removeAll(Collection c) It is used to remove another collection.
17/10/2023 26
LinkedList
●
LinkedList class uses a doubly LinkedList to store element. i.e.,
the user can add data at the first position as well as the last
position.
●
The dequeue interface is implemented using the LinkedList class.
●
Null insertion is possible.
●
If we need to perform insertion /Deletion operation the LinkedList
is preferred.
●
LinkedList is used to implement Stacks and Queues.
17/10/2023 27
Methods of LinkedList
●
addFirst()
●
addLast()
●
removeFirst()
●
removeLast()
●
getFirst()
●
getLast()
17/10/2023 28
Set Interface
●
Set is a child interface of Collection.
●
Insertion order not preserved i.e., They appear in the different
order in which we inserted.
●
Duplicate elements are not allowed.
●
Heterogeneous objects are allowed.
●
Set Interface is implemented by using LinkedHashSet and
HashSet class.
17/10/2023 29
●
Hashset
●
HashSet stores the elements by using Hashing mechanism.
●
It contains unique elements only.
●
This hashSet allows null values.
●
It does not maintain insertion order. It inserted elements based on
their hashcode.
●
HashSet is the best approach for the search operation.
17/10/2023 30
three different ways to create HashSet:
HashSet hs = new Hashset();
●
Here, HashSet default capacity to store elements is 16 with a
default load factor/fill ratio of 0.75.
●
Load factor is if HashSet stores 75% element then it creates a
new HashSet with increased capacity.
Hashset hs = new Hashset ( 100);
Here 100 is an initial capacity and the default load factor is 0.75.
Hashset hs = new Hashset ( 100,(foot) 0.90);
●
Here capacity is 100 with a load factor of 0.90. The load factor
may be decided by the user but it should be >=0.75.
●
17/10/2023 31
LinkedHashSet
●
The LinkedHashSet class extends the HashSet class.
●
The basic data structure is a combination of LinkedList and
Hashtable.
●
Insertion order is preserved.
●
Duplicates are not allowed.
●
LinkedHashSet is non synchronized.
●
LinkedHashSet is the same as HashSet except the above two
differences are present.
17/10/2023 32
Map Interface
●
A map is a part of the collection framework but it does not
implement a collection interface.
●
A map stores values based on Key and value Pair.
●
Duplicate value of the key is not allowed. In short,
●
Key must be unique while duplicates values are allowed.
●
HashMap
●
LinkedHashMap
●
Hashtable
17/10/2023 33
●
HashMap
●
Map Interface is implemented by HashMap.
●
HashMap stores the elements by using a mechanism called
Hashing.
●
It contains values based on the key-value pair.
●
It contains a unique key.
●
It can store one Null key and Multiple null values.
●
Insertion order is not maintained and it is based on the hash code
of the keys.
●
HashMap is Non-Synchronized.
17/10/2023 34