Sunday, April 5, 2020

Checkbox class in java

CheckBox In Java
A check box is a graphical component that can be in either an "on" (true) or "off" (false) state. Clicking on a check box changes its state from "on" to "off," or from "off" to "on."
Constructor :-
1.public Checkbox() throws HeadlessException
Creates a check box with no label.
The state of this check box is set to "off," and it is not part of any check box group.
2. public Checkbox(String label)
Creates a check box with the specified label.
The state of this check box is set to "off,"
 it is not part of any check box group.
 Checkbox(String label,boolean state) 
Creates a check box with the specified label and sets the specified state. This check box is not part of any check box group.
4. Checkbox(String label, CheckboxGroup group, boolean state)
Creates a check box with the specified label, in the specified check box group, and set to the specified state. 
  Checkbox constructors:
  • Checkbox( )
  • Checkbox(String str)
  • Checkbox(String str, boolean on)
  • Checkbox(String str, boolean on, CheckboxGroup cbGroup)
  • Checkbox(String str, CheckboxGroup cbGroup, boolean on)
  • These methods 
  • boolean getState( )
  • void setState(boolean on)
  • String getLabel( )
  • void setLabel(String str)
  • Handling Check Boxes
  • Each time a check box is selected or deselected, an item event is generated
  • Each listener implements the ItemListener interface
  • Interface defines the itemStateChanged( ) method.
 Methods in CheckBox


  • getLabel
  • public String getLabel() Gets the label of this check box. Returns the label of this check box, or null if this check box has no label.
  • setLabel
  • public void setLabel(String label)
  • Sets this check box's label to be the string argument.
  •  
  • getState
  • public boolean getState()
  • Determines whether this check box is in the "on" or "off" state.
  • checkbox1.getState());
  • g.drawString("Java: " + java.getState(),10,60);
  • setState
  • public void setState(boolean state Sets the state of this check box to the specified state.
  • Enabling Checkbox checkbox1.setEnabled(false);
  • Checking enable or not
  • (checkbox1.isEnabled()
 Checkbox groups
  • Checkbox groups are collections of checkboxes with the special property that no more than one checkbox in the same group can be selected at a time. The checkboxes in a CheckboxGroup are often called radio buttons. 
  • CheckboxGroup cbg = new CheckboxGroup();
  • To make check boxes act like radio buttons, use this constructor for each Checkbox in the group.
  • public Checkbox(String label, CheckboxGroup cbg, boolean checked)The label is the label for this Checkbox. The CheckboxGroup is the group you want this Checkbox to belong to and must already exist.

Monday, March 30, 2020

Applet in java

  • Applets:-
  • Applets are small Java applications It runs inside the web browser ..
  • Any applet in Java is a class that extends the java.applet.Applet class.
  • An Applet class does not have any main() method. It is viewed using JVM. The JVM can use either a plug-in of the Web browser or applet viewer to run applet .
  • JVM creates an instance of the applet class and invokes init() method to initialize an Applet.
  • The java.applet.Applet class include some common methods
  1. Get applet parameter<parms>
  2. Print a status message in the browser using "showstatus" method
  3. Fetch an image
  4. Fetch an audio clip
  5. Play an audio clip
  6. Resize the applet

  Java Applet Life cycle

Following are the stages in Applet

  1. Applet is initialized using init() method
  2. Applet is started using start()
  3. Applet is painted using paint()
  4. Applet is stopped using stop()
  5. Applet is destroyed using destroy()
  • init() : init() is the first method to be called. where variable are initialized. This method is called only once during the runtime of applet.
  • start() : start() method is called after init(). 
  • stop() : stop() method is called to suspend thread that does not need to run when applet is not visible.
  • destroy() : destroy() method is called when your applet needs to be removed completely from memory or when user close web browser 
Example:-

import java.applet.*;
import java.awt.*;
public class App1 extends Applet
{
public void start()
{
System.out.println("Applet started");
}
public void stop()
{
System.out.println("Applet stopped");
}
public void init()
{
System.out.println("Welcome");
}
public void paint(Graphics g)
{
setBackground(Color.red);
g.setColor(Color.green);
g.drawString("Hello",20,20);
}}
// <applet code= "App1.class" height=200 width=200></applet>


Example 2:-
import java.awt.*;
import java.applet.*;
public class Tes2 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawArc(50,50,100,60,50,80);
g.fillArc(200,200,100,60,80,50);
}}
// <applet code= "Tes2.class"height=200 width=200></applet>



Example 3:-

import java.awt.*;
import java.applet.*;
public class app extends Applet

{

public void paint(Graphics g)
{

Color c1=new Color(255,200,100);
Font f1=new Font("Serif",Font.BOLD,42);
g.setFont(f1);
g.setColor(c1);
g.drawString("hello",30,30);
g.setColor(c1);
g.drawLine(10,30,44,32);
g.drawRect(55,55,55,55);

g.drawRoundRect(50,50,50,50,50,50);
g.drawArc(80,100,200,400,85,96);
}

}
// <applet code= "app.class"height=200 width=200></applet>

.

 Images in Applet


java.awt.Image class is used for representing an image.
java.applet, java.awt and java.awt.image are the packages which are used for event handling.

Loading an image

In Applet, images are loaded using getImage() method. It is always  call the constructor in init() method.
 examples:
Image image2 = getImage(getDocumentBase(), "pic1.jpeg"); 

Displaying an image

In Applet, images are displayed using drawImage() method. This method is supplied by the Graphics object, which is passed to paint() method.

Param Tag

 The <param> tag contains two attributes: name and value which are used to specify the name of the parameter and the value of the parameter respectively. 
For example
<param name=”class” value=”msc” />
<param name=”seats” value=”40″ />
Now, these two parameters can be accessed in the applet program using the getParameter() method of the Applet class.
 getParameter() Method
 The getParameter() method of the Applet class can be used to retrieve the parameters passed from the HTML page. The syntax of getParameter() method is as follows:
Example:
import java.awt.*;
import java.applet.*;
public class App1 extends Applet 
{
String x;
String a;
public void init()
{
x = getParameter("class");
a = getParameter("seats");
}
public void paint(Graphics g)
{
g.drawString("class is: " + x, 20, 20);
g.drawString("seats is: " + a, 20, 40);
}
}
/*
<applet code="App1" height="300" width="500">
<param name="class" value="msc" />
<param name="seats" value="40" />
</applet>
*/

Tuesday, March 24, 2020

Steps to servlets in java

For Running Java Servlet

1. jdk1.5.0 Kit
2. Install Tomcat6.0
3. Now set environment Variables

Right Click on My COmputer>Properties/Advanced (tab) CLick Environment Variables

Click on New Button

Variable name :   JAVA_HOME
Variable value:   C:\jdk1.5.0\bin; 
clcik ok

now Set CLASSPATH Again Click on NEW bUTTON

Variable name :    CLASSPATH
Variable value:    C:\Tomcat6\lib\servlet-api.jar;

Click Ok>>Ok>> Ok

4. Make a program in NOtepad and Save in jdk\bin directory

Compile the file and This CLass file is PASTE in the following path

C:\Tomcat6\webapps\ROOT\WEB-INF

Make a new folder name (classes), in WEB-INF folder and paste in classes FOLDER

5. Now Edit the xml file in   C:\Tomcat6\webapps\ROOT\WEB-INF

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>

6. Start Monitor Tomcat from Start Menu 
Server is Running in Task Bar in Green Play Button

NOW run the in Browser as

http://localhost:8080/Hello         then ENTER

ways to read input from console in Java

Different ways to read input from console in Java

using IO Streams 


import java.io.*;
class aa
{
public static void main(String sd[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter first no");
String s1=br.readLine();

int a=Integer.parseInt(s1);
System.out.print("enter second no");
String s2=br.readLine();       //s2 is a string that is read from console
int b=Integer.parseInt(s2);  //s2 is converted to data type int b

System.out.print("sum="a+b);    
}}

Description :

import java.io.*:    It is a import package statement in which IO package is used wit all classes by 
using (*)

InputStreamReader :is a class of IO package to accept the input from user
BufferedReader is class  is Reads text from a character-input stream, buffering characters  for the efficient reading of characters.
readLine: method is a static method of Java Console class. It is used to reads a single line of text from the console.example br.readLine() "br  is an object of BufferedReader" 
Integer.parseInt:The method generally used to convert String to Integer in Java is parseInt().

Java User Input (Scanner)

import java.util.*;  //util is a package that contain Scanner class
class scn
{
public static void main(String sd[])  
{
Scanner sn = new Scanner(System.in);
System.out.print("enter first no");


int a=sn.nextInt(); //nextInt is a method to read  int value from user
System.out.print("enter second no");
int b=sn.nextInt();

System.out.print( a+b);
}}
 
Note:-If you enter wrong input you will get exception message like "InputMismatchException"

 Example:- 2

import java.util.*; 
public class scn2 { 
public static void main(String args[]){ 
Scanner in = new Scanner(System.in); 
System.out.print("Enter your name: "); 
String name = in.nextLine();//using nextLine method to print string
System.out.println("Your Name is: " + name);           
                     
          } 




Sunday, March 22, 2020

first program in java

Steps to create a simple java program in Notepad

  • Open Notepad and create
class Hellojava
{
public static void main(String arg[])
{
System.out.print("HELLO");
}}


  • Create myprograms folder on  any drive (C:,D:)
  • Save the file with the class name like Hellojava.java inside the created folder 
  • Open cmd and type command  javac Hellojava.java
  • After compiling run the program using java Hellojava command   

1 Simple program to print A to Z


class AZ

{
public static void main(String arg[])
{
for(char i='A';i<='Z';i++)
System.out.println(" "+i);
}}

output:-


2. simple example to print A to Z 
class ab

{
public static void main(String arg[])
{
for(char i= 65;i< 91;i++)
System.out.print(" "+i);
}}

Setting Path in Java

How to set Temporary and Permanent Paths in Java



First of all we have to know that if java source file is jdk /bin  directory then no need to set java path  the bin folder if java file is saved outside the jdk/bin folder then we need to set path

There are tow common way to set java paths
  1. Temporary Path
  1. Permanent Path

1. steps to set temporary path

  • Open my computer and double click on C drive go to program file and open java folder double click on jdk 
  • Copy the path of jdk/bin directory
  • Write in command prompt:SET PATH=copied_path
for example :- SET PATH=  "C:\Program Files\Java\jdk1.5.0\bin"
  1. create java source file in notepad or notepad++ and save the file as class name.java
  2.  example 
class A
{
public static void main(String args[])
{

System.out.println("Hello");
}}

save this file as A.java in C drive or any other drive

  • open cmd and set the above given path
  • type javac A.java for compilation after successful compilation A.class file is generated
  • to run java program type java A
Setting Permanent Java Path:

  • Go to My PC properties  and click on properties
  • Click on Advanced system settings
  • Click on Environment variables
  • Edit Path variable in system variables


  • place semicolon at the end of existing path and past copied path till bin
C:\Program Files\Java\jdk1.5.0\bin and press ok button