Posts

Program-1 Adv jJava

Image
1.Develop a Programe that has only one button in the frame,clicking on the button cycle through the color RED>GREEN>BLUE ans so on.One color change per Click.(Use getBackGround() method to get the current color)  import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class SimpleAwt extends Frame {     public static void main(String args[]) {         JFrame frame = new JFrame();         JButton button = new JButton("click");         Container c = frame.getContentPane();         c.setBackground(Color.WHITE);         FlowLayout ff = new FlowLayout();         c.setLayout(ff);     ...

Applet life cycle

  Applet Life Cycle in Java In Java, an  applet  is a special type of program embedded in the web page to generate dynamic content. Applet is a class in Java. The applet life cycle can be defined as the process of how the object is created, started, stopped, and destroyed during the entire execution of its application. It basically has five core methods namely init(), start(), stop(), paint() and destroy().These methods are invoked by the browser to execute. Along with the browser, the applet also works on the client side, thus having less processing time. Methods of Applet Life Cycle init():  The init() method is the first method to run that initializes the applet. It can be invoked only once at the time of initialization. The web browser creates the initialized objects, i.e., the web browser (after checking the security settings) runs the init() method within the applet. start():  The start() method contains the actual code of the applet and starts the applet....

Java Applet

  Java Applet Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side. Advantage of Applet There are many advantages of applet. They are as follows: It works at client side so less response time. Secured It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc. Drawback of Applet Plugin is required at client browser to execute applet. Lifecycle of Java Applet Applet is initialized. Applet is started. Applet is painted. Applet is stopped. Applet is destroyed Lifecycle methods for Applet: The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet. java.applet.Applet class For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet. public void init():  is used to initialized the Applet. It is invoked only once. public void start(...

Exceptions in Java

  Exceptions in Java The   Exception Handling in Java   is one of the powerful   mechanism to handle the runtime errors   so that the normal flow of the application can be maintained. In this tutorial, we will learn about Java exceptions, it's types, and the difference between checked and unchecked exceptions. Types of Java Exceptions There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked exception. However, according to Oracle, there are three types of exceptions namely: Checked Exception Unchecked Exception Error Difference between Checked and Unchecked Exceptions 1) Checked Exception The classes that directly inherit the Throwable class except RuntimeException and Error are known as checked exceptions. For example, IOException, SQLException, etc. Checked exceptions are checked at compile-time. 2) Unchecked Exception The classes that inherit the RuntimeException are known as unchecked exceptions. For example, A...

Stopping and blocking threads

  Starting a thread: When we create a thread by taking instance of a thread class, the thread will be in its new-born state. To move it to runnable state, we use a method named start( ). When we invoke this method with a thread, java run-time schedules it to run by invoking it run() method. Now the thread will be in its running state. To start a thread we use the following syntax: MyThread t1 = new MyThread(); t1.start(); Stopping a thread: Whenever we want to stop a thread from running further, we may do so by calling its stop() method. This causes a thread to stop immediately and move it to its dead state. It forces the thread to stop abruptly before its completion i.e. it causes premature death. To stop a thread we use the following syntax: t1.stop(); Blocking a Thread: A thread can also be temporarily suspended or blocked from entering into the runnable and subsequently running state by using either of the following thre...

Priority of a Thread (Thread Priority)

  Priority of a Thread /Thread Priority   Each thread have a priority. Priorities are represented by a number between 1 and 10. In most cases, thread scheduler schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses. 3 constants defined in Thread class: public static int MIN_PRIORITY public static int NORM_PRIORITY public static int MAX_PRIORITY Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10. Example of priority of a Thread:    class  TestMultiPriority1  extends  Thread    {         public   void  run()            {                  System.out.println( "running thread name is:" +Thread.current...

Life cycle of a Thread and creating thread class

Image
  Life cycle of a Thread /Thread States:         A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state.The life cycle of the thread in java is controlled by JVM. The java thread states are as follows: New Runnable Running Non-Runnable (Blocked) Terminated 1) New The thread is in new state if you create an instance of Thread class but before the invocation of start() method. 2) Runnable The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread. 3) Running The thread is in running state if the thread scheduler has selected it. 4) Non-Runnable (Blocked) This is the state when the thread is still alive, but is currently not eligible to run. 5) Terminated A thread is in terminated or dead state when its run() method exits. ...