Tuesday, April 23, 2019

print string using textbox and commandbutton (method 2)

import java.awt.*; 
import java.awt.event.*; 
class p1 extends Frame implements ActionListener{ 
TextField tf; 
p1(){ 
 
//create components 
tf=new TextField(); 
tf.setBounds(60,50,170,20); 
Button b=new Button("click me"); 
b.setBounds(100,120,80,30); 
 
//register listener 
b.addActionListener(this);//passing current instance 
 
//add components and set size, layout and visibility 
add(b);add(tf); 
setSize(300,300); 
setLayout(null); 
setVisible(true); 

public void actionPerformed(ActionEvent e){ 
tf.setText("Welcome"); 

public static void main(String args[]){ 
new p1(); 



output::





reference -- javapoint.com
  • Button
    • public void addActionListener(ActionListener a){}
  • MenuItem
    • public void addActionListener(ActionListener a){}
  • TextField
    • public void addActionListener(ActionListener a){}
    • public void addTextListener(TextListener a){}
  • TextArea
    • public void addTextListener(TextListener a){}
  • Checkbox
    • public void addItemListener(ItemListener a){}
  • Choice
    • public void addItemListener(ItemListener a){}
  • List
    • public void addActionListener(ActionListener a){}
    • public void addItemListener(ItemListener a){}





reference -- javapoint.com

print string using text box and command buttton

import java.awt.*; 
import java.awt.event.*; 
public class p1 { 
public static void main(String[] args)
 { 
    Frame f=new Frame("Button Example"); 
    final TextField tf=new TextField(); 
    tf.setBounds(50,50, 150,20); 
    Button b=new Button("Click Here"); 
    b.setBounds(50,100,60,30); 
 
 b.addActionListener(new ActionListener()
                                       { 
                                       public void actionPerformed(ActionEvent e)
                                        { 
                                           tf.setText("this is a text box");
                                        } 
                                      }
                                   ); 
    f.add(b);f.add(tf); 
    f.setSize(400,400); 
    f.setLayout(null); 
    f.setVisible(true); 




output:::



set bound() method in java

public void setBounds(int x, int y, int width, int height)

Friday, April 12, 2019

exception handling

1. divide by zero without exception  -link
2. divide by zero with exception - link
3. null pointer - link
4. number format exception - link
5. array index out of bound- link
6. own exception - link
7. nested try -link


threads

1. thread using thread class -link
2. Java Thread Example by implementing Runnable inter...  link
3. multithreading with run() -- link
4. multithreading with start() -- link
5. multithreading with join method -- link
6. thread setname(),getname(),getid() -- link
7. current thread name - link
8. thread priority - link
9. set priority of thread - link
10. daemon thread -  link
11. thread suspend()/resume() -- link
12. without synchronization -- link
13. synchronization - i) synchronized method -- link
                                  ii) synchronized block -- link










dynamic 2d array in java

class cll
{
public static void main(String args[])
{

int a[][];
a=new int[2][];
for (int i=0;i<2;i++)
{
   a[i]=new int [i+2];
   for(int j=0;j<i+2;j++)
     {
            a[i][j]=i*j;
     }
}

System.out.println("matrix  ");
for (int i=0;i<2;i++)
{
 
   for(int j=0;j<i+2;j++)
     {
            System.out.print(a[i][j]+" ");
     }
     System.out.println(" ");
}

System.out.println("total row "+ a.length);
System.out.println("total column in row 0="+a[0].length);
System.out.println("total column in row 1="+a[1].length);

}
}




D:\>javac cll.java

D:\>java cll
matrix
0 0
0 1 2
total row 2
total column in row 0=2
total column in row 1=3

D:\>



exception handling



public class cl {
 public static void main(String args[]) {
  int d = 0;
  int n = 20;
  try {
   int fraction = n / d;
   System.out.println("This line will not be Executed");
  } catch (ArithmeticException e) {
   System.out.println("In the catch Block due to Exception = " + e);
  }
  System.out.println("End Of Main");
 }
}




$javac cl.java
$java -Xmx128M -Xms16M cl
In the catch Block due to Exception = java.lang.ArithmeticException: / by zero
End Of Main

exception handling

public class cl {
 public static void main(String args[]) {
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
 at cl.main(cl.java:4)

exception handling

{
 public static void main(String args[]) {
  String s="abc";
int i=Integer.parseInt(s);//NumberFormatException

  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:580)
 at java.lang.Integer.parseInt(Integer.java:615)
 at cl.main(cl.java:4)