×
If you can dream it, you can do it.
--Your friends at LectureNotes
Close

Note for Java Programming - JAVA By shivam suri

  • Java Programming - JAVA
  • Note
  • Jammu Univercity - MIET
  • Information Technology Engineering
  • 2 Topics
  • 1936 Views
  • 65 Offline Downloads
  • Uploaded 10 months ago
Shivam Suri
Shivam Suri
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

JAVA PROGRAMMING COURSE NO. IT-705(A) SECTION - A Q1. Define the following terms: i. Event An event in Java is an object that is created when something changes within a graphical user interface. If a user clicks on a button, clicks on a combo box, or types characters into a text field, etc., then an event triggers, creating the relevant event object. This behavior is part of Java's Event Handling mechanism and is included in the Swing GUI library. For example, let's say we have a JButton. If a user clicks on the JButton, a button click event is triggered, the event will be created, and it will be sent to the relevant event listener (in this case, the ActionListener). The relevant listener will have implemented code that determines the action to take when the event occurs. Note that an event source must be paired with an event listener, or its triggering will result in no action. How Events Work Event handling in Java is comprised of two key elements: • • The event source, which is an object that is created when an event occurs. Java provides several types of these event sources, discussed in the section Types of Events below. The event listener, the object that "listens" for events and processes them when they occur. There are several types of events and listeners in Java: each type of event is tied to a corresponding listener. For this discussion, let's consider a common type of event, an action event represented by the Java class ActionEvent, which is triggered when a user clicks a button or the item of a list. At the user's action, an ActionEvent object corresponding to the relevant action is created. This object contains both the event source information and the specific action taken by the user. This event object is then passed to the corresponding ActionListener object's method: void actionPerformed(ActionEvent e) This method is executed and returns the appropriate GUI response, which might be to open or close a dialog, download a file, provide a digital signature, or any other of the myriad actions available to users in an interface. MADE BY: SHIVAM SURI Page 1

Text from page-2

Types of Events Here are some of the most common types of events in Java: • • • • • ActionEvent: Represents a graphical element is clicked, such as a button or item in a list. Related listener: ActionListener. ContainerEvent: Represents an event that occurs to the GUI's container itself, for example, if a user adds or removes an object from the interface. Related listener: ContainerListener. KeyEvent: Represents an event in which the user presses, types or releases a key. Related listener: KeyListener. WindowEvent: Represents an event relating to a window, for example, when a window is closed, activated or deactivated. Related listener: WindowListener. MouseEvent: Represents any event related to a mouse, such as when a mouse is clicked or pressed. Related listener: MouseListener. Note that multiple listeners and event sources can interact with one another. For example, multiple events can be registered by a single listener, if they are of the same type. This means that, for a similar set of components that perform the same type of action, one event listener can handle all the events. Similarly, a single event can be bound to multiple listeners, if that suits the program's design (although that is less common). ii. Thread A thread, in the context of Java, is the path followed when executing a program. All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM) at the program’s start, when the main() method is invoked with the main thread. In Java, creating a thread is accomplished by implementing an interface and extending a class. Every Java thread is created and controlled by the java.lang.Thread class. Java is a multi-threaded application that allows multiple thread execution at any particular time. In a single-threaded application, only one thread is executed at a time because the application or program can handle only one task at a time. For example, a single-threaded application may allow for the typing of words. However, this single thread requires an additional single thread allowing for the recording of keystrokes in order to type the words. Thus, a single-threaded application records the keystrokes, allowing the next single-threaded application (the typing of words) to follow. However, a multi-threaded application allows for the handling of both tasks (recording and typing the keystrokes) within one application. When a thread is created, it is assigned a priority. The thread with higher priority is executed first, followed by lower-priority threads. The JVM stops executing threads under either of the following conditions: MADE BY: SHIVAM SURI Page 2

Text from page-3

• • iii. If the exit method has been invoked and authorized by the security manager All the daemon threads of the program have died Applet An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. There are some important differences between an applet and a standalone Java application, including the following − • An applet is a Java class that extends the java.applet.Applet class. • A main() method is not invoked on an applet, and an applet class will not define main(). • Applets are designed to be embedded within an HTML page. • When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. • A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment. • The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime. • Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed. • Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file. Life Cycle of an Applet Four methods in the Applet class gives you the framework on which you build any serious applet − • init − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed. • start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages. • stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet. MADE BY: SHIVAM SURI Page 3

Text from page-4

• destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet. • paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt. iv. AWT Abstract Window Toolkit(AWT) AWT contains large number of classes and methods that allows you to create and manage graphical user interface ( GUI ) applications, such as windows, buttons, scroll bars,etc. The AWT was designed to provide a common set of tools for GUI design that could work on a variety of platforms. The tools provided by the AWT are implemented using each platform's native GUI toolkit, hence preserving the look and feel of each platform. This is an advantage of using AWT.But the disadvantage of such an approach is that GUI designed on one platform may look different when displayed on another platform. AWT is the foundation upon which Swing is made i.e Swing is a set of GUI interfaces that extends the AWT. But now a days AWT is merely used because most GUI Java programs are implemented using Swing because of its rich implementation of GUI controls and light-weighted nature. AWT Hierarchy MADE BY: SHIVAM SURI Page 4

Lecture Notes