Advanced java unit1_notes
Advanced java unit1_notes
A component is the fundamental user interface object in Java. Everything you see on the display in a
Java application is a component. This includes things like windows, panels, buttons, checkboxes,
scrollbars, lists, menus, and text fields. To be used, a component usually must be placed in a container.
1. Container
The Container is a component that will be used to extend other components such as window, panel,
Frame, Dialog, and Applet as shown in the above diagram.
Window: The Window is a Container that doesn’t include borders and a menu bar.
Panel: The Panel is also a Container that doesn’t include a title bar, menu, or border.
Frame: The Frame is a container used while creating an AWT application. It can have
components like title bar, menu bars, borders and also buttons, scroll bar, etc.
Dialog: The Dialog box is a container that will display the message that we want to display on
the screen.
2. Button
A button is a labeled component when clicked performs an event. It is created by the Button class.
When clicked it performs some action by sending an instance of ActionEvent by AWT.
public class Button extends Component implements Accessible
It contains 2 constructors:
Java
// Driver Class
class SubmitButton extends Frame {
// main function
public static void main(String[] args)
{
// create instance of frame with label
Frame frame = new Frame("Submit form");
Output:
3. Label
It is used to show text in the Container. It will displays text in the form of READ-ONLY, which
cannot be changed by the user directly. We need to create an instance of Label Class to create a
Label. The Declaration of Label Class will be
public class Label extends Component implements Accessible
4. Checkbox
It is used to create a Checkbox in the Container. It can get a value either true or false by checking
and unchecking the checkbox.
checked – returns true
unchecked – returns false
It can be created by creating an instance of Checkbox. The Declaration of Label Class will be
Example 1:
Java
Output:
5. Choice
It is used to show the popup menu to select any item from the menu items. The selected choice will
be shown at the top of the menu bar. We need to create an instance of Choice Class to create a
Choice. The Declaration of Choice Class will be
public class Choice extends Component implements ItemSelectable, Accessible
It has 1 constructor:
6. List
The List Object creates a list of items in which we can choose one or multiple items at a time. We
need to create an instance of List Class to create a List. The Declaration of Label Class will be
public class List extends Component implements ItemSelectable, Accessible
It has 3 constructors:
An event can be defined as changing the state of an object or behavior by performing actions. Actions
can be a button click, cursor movement, keypress through keyboard or page scrolling, etc.
Classification of Events
Foreground Events
Background Events
Types of Events
1. Foreground Events
Foreground events are the events that require user interaction to generate, i.e., foreground events are
generated due to interaction by the user on components in Graphic User Interface (GUI). Interactions
are nothing but clicking on a button, scrolling the scroll bar, cursor moments, etc.
2. Background Events
Events that don’t require interactions of users to generate are known as background events. Examples
of these events are operating system failures/interrupts, operation completion, etc.
Event Handling
It is a mechanism to control the events and to decide what should happen after an event occur. To
handle the events, Java follows the Delegation Event model.
Listeners: Listeners are used for handling the events generated from the source. Each of these
listeners represents interfaces that are responsible for handling events.
To perform Event Handling, we need to register the source with the listener.
Syntax:
addTypeListener()
MouseListener & The events that occur due to the user interaction
MouseEvent
MouseMotionListener with the mouse (Pointing Device).
Code-Approaches
The three approaches for performing event handling are by placing the event handling code in one of
the below-specified places.
1. Within Class
2. Other Class
3. Anonymous Class
Note: Use any IDE or install JDK to run the code, Online compiler may throw errors
due to the unavailability of some packages.
Java
import java.awt.*;
import java.awt.event.*;
TextField textField;
GFGTop()
{
// Component Creation
textField = new TextField();
// add Components
add(textField);
add(button);
// set visibility
setVisible(true);
}
Output
A Thread is a very light-weighted process, or we can say the smallest part of the process that allows a
program to operate more efficiently by running multiple tasks simultaneously.
In order to perform complicated tasks in the background, we used the Thread concept in Java. All the
tasks are executed without affecting the main program. In a program or process, all the threads have
their own separate path for execution, so each thread of a process is independent.
When multiple threads are executed in parallel at the same time, this process is known
as Multithreading.
In a simple way, a Thread is a:
o Feature through which we can perform multiple activities within a single process.
o Lightweight process.
Thread Model
Just like a process, a thread exists in several states. These states are as follows:
Creating Thread
Thread Class
A Thread class has several methods and constructors which allow us to perform various operations on
a thread. The Thread class extends the Object class. The Object class implements
the Runnable interface. The thread class has the following constructors that are used to perform
various operations.
o Thread()
o Thread(Runnable target)
The Runnable interface is required to be implemented by that class whose instances are intended to be
executed by a thread. The runnable interface gives us the run() method to perform an action for the
thread.
start() method
The method is used for starting a thread that we have newly created. It starts a new thread with a new
callstack. After executing the start() method, the thread changes the state from New to Runnable. It
executes the run() method when the thread gets the correct time to execute it.
Let's take an example to understand how we can create a Java thread by extending the Thread class:
ThreadExample1.java
6. int a= 10;
7. int b=12;
11. }
13. {
16. //calling start method to execute the run() method of the Thread class
17. t1.start();
18. }
19. }
Networking features
Java Networking is a concept of connecting two or more computing devices together so that we can
share resources.
Java socket programming provides facility to share data between different computing devices.
1. Sharing resources
2. Centralize software management
The java.net package supports two protocols,
1. TCP: Transmission Control Protocol provides reliable communication between the sender
and receiver. TCP is used along with the Internet Protocol referred as TCP/IP.
2. UDP: User Datagram Protocol provides a connection-less protocol service by allowing packet
of data to be transferred along two or more nodes
1. IP Address
2. Protocol
1) IP Address
2) Protocol
A protocol is a set of rules basically that is followed for communication. For example:
o TCP
o FTP
o Telnet
o SMTP
o POP etc.
3) Port Number
The port number is used to uniquely identify different applications. It acts as a communication
endpoint between applications.
The port number is associated with the IP address for communication between two applications.
4) MAC Address
MAC (Media Access Control) address is a unique identifier of NIC (Network Interface Controller). A
network node can have multiple NIC but each with unique MAC address.
For example, an ethernet card may have a MAC address of 00:0d:83::b1:c0:8e.
6) Socket
java.net package
2. A High Level API: It deals with the abstraction of URIs i.e. Universal Resource Identifier,
URLs i.e. Universal Resource Locator, and Connections i.e. connections to the resource
pointed by URLs.
The java.net package provides many classes to deal with networking applications in Java. A list of
these classes is given below:
o Authenticator
o CacheRequest
o CacheResponse
o ContentHandler
o CookieHandler
o CookieManager
o DatagramPacket
o DatagramSocket
o DatagramSocketImpl
o ContentHandlerFactory
o CookiePolicy
o CookieStore
o DatagramSocketImplFactory
Media techniques:
Modern world's rich internet applications must be capable to play and edit the media files when
required. JavaFX provides the media-rich API that can play audio and video on the user's demand.
JavaFX Media API enables the users to incorporate audio and video into the rich internet applications
(RIAs). JavaFX media API can distribute the media content across the different range of devices like
TV, Mobile, Tablets and many more.
the capability of JavaFX to deal with the media files in an interactive way. For this purpose, JavaFX
provides the package javafx.scene.media that contains all the necessary
classes. javafx.scene.media contains the following classes.
1. javafx.scene.media.Media
2. javafx.scene.media.MediaPlayer
3. javafx.scene.media.MediaStatus
4. javafx.scene.media.MediaView
Media Events
Possible media and media-player events are discussed in the following table.
Media setOnError() This method is invoked when an error occurs. It is the part of the class
Media.
MediaPlayer setOnEndOfMedia() The method is invoked when end of the media play is reached.
MediaPlayer setOnHalted() This method is invoked when the status of media changes to halted.
MediaPlayer setOnMarker() This method is invoked when the Marker event is triggered.
MediaPlayer setOnPlaying() This method is invoked when the play event occurs.
MediaPlayer setOnRepeat() This method is invoked when the repeat property is set.
MediaPlayer setOnStalled() This method is invoked when the media player is stalled.
MediaPlayer setOnStopped() This method is invoked when the media player has stopped.
MediaView setOnError() This method is invoked when an error occurs in the media view.
We must notice that MediaPlayer class contains the most number of events triggered while MediaView
and Media classes contains one event each.
javafx.scene.media.Media class
The properties of the class are described in the following table. All the properties are the read only
except onError.
Property Description
duration The duration of the source media in seconds. This property is of object type of the class
Duration.
error This is a property set to media exception value when an error occurs. This property is of the
type object of the class MediaException.
height The height of the source media in pixels. This is an integer type property.
onError The event handler which is called when the error occurs. The method setOnError() is used to
set this property.
width The width of the source media in pixels. This is an integer type property
JavaFX.scene.media.MediaPlayer class
The properties of the class along with the setter methods are described in the following table.
autoPlay This is the boolean type property. The true setAutoPlay(Boolean value)
value indicates the playing will be started as
soon as possible.