Posts

Showing posts from October, 2013

Checkbox and List Example

import java.awt.*; import java.awt.event.*; class MyChoiceCheckBoxExample extends MasterAWTFrame implements ItemListener {     private Checkbox cbbold, cbitalic, cbred, cbblue, cbcyan, cbgray;     private Choice ch1;     private CheckboxGroup chg;     private TextArea ta;     private Font f;     private List mylist, myfsize;     private int style = Font.PLAIN;  private String fname =Font.SANS_SERIF;     private int fontsize = 12;     private String sstr="";     private Canvas drawCan;     MyChoiceCheckBoxExample() {                 cbbold = new Checkbox("Bold");         cbitalic = new Checkbox("Italic");         chg = new CheckboxGroup();                 cbred = new Checkbox("Blue", chg, true);         cbgray = n...

Exception Handling Example

import java.io.IOException; class MyException extends Exception { String mess;     public MyException(String message) {         super(message);         mess = message;     }           } class MyTest {     public static char read() throws IOException     {                 char mychar = (char)System.in.read();                 return mychar;     } } public class MyExceptionTest {     public static void main(String[] args) {         try {         throw new MyException("My First exception ");         }         catch(MyException me) {           System.out.println(me.getMessage());           //  me.printStackTrace();  ...

AWT Frame with Dialog Class Example

import java.awt.Button; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /**  *  * @author bhavesh  */ public class MySecondFrame  extends MasterAWTFrame implements ActionListener {     private Button btok;     public MySecondFrame() {                 btok = new Button("OK");         //setLayout(new FlowLayout(FlowLayout.LEFT));                 p2.add(btok);                   btok.addActionListener(this);             }         public void actionPerformed(ActionEvent ae)     {        new MyDialog(this).setVisible(true);     }               public stati...

WindowAdapter Example with windowClosing method

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Frame; import java.awt.HeadlessException; import java.awt.Panel; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /**  *  * @author bhavesh  */ public class MasterAWTFrame  extends  Frame { protected Panel p1, p2;     public MasterAWTFrame() throws HeadlessException {                   p1 = new Panel();       p2 = new Panel();             add(p1,BorderLayout.CENTER);       add(p2,BorderLayout.SOUTH);             p1.setBackground(Color.gray);       p2.setBackground(Color.CYAN);                             WindowAdapter wlistener = new WindowAdapter()       {           public...

enum Type example

/**  *  * @author bhavesh  */ public enum Season {       WINTER(2),MONSOON(3), SUMMER(4);       private int count;       private Season(int count)     {         this.count=count;     }       public int getMaxDays()     {         return count;     }   } public class MainEnumTest {       public static void main(String[] args) {                     Season s1 = Season.MONSOON;                 s1= Season.SUMMER;         s1 = Season.valueOf("WINTER");         System.out.println("days :"+s1.getMaxDays());                 System.out.println("origin "+ s1.ordinal());               ...

Annotation Example

import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /**  *  * @author bhavesh  */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.TYPE) public @interface NewAnnotation   {       public String name();     public int myno() default 10;             } // @Override  and @NewAnnotaion usage example class A {     public void test()     {         // block of code     } } class B extends A {     @Override     public void test()     {         //block of code     } } @NewAnnotation(name="ISTAR") public class ClassAnnotationTest {     // block of code     //   }

Frame With Anonymous class Example

import java.awt.*; import java.awt.event.*; /**  *  * @author bhavesh  */ public class MyFrameAnonymous extends Frame { private Button btexit;     public MyFrameAnonymous() {                 setLayout(new FlowLayout());         btexit = new Button("Exit");         add(btexit);                 btexit.addActionListener(                                 new ActionListener()                 {                     public void actionPerformed(ActionEvent ae)                     {                                       ...

Anonymous Class Example

/**  *  * @author bhavesh  */ interface MyShape {     public double area();     public void setValue(double v); } public class MyAnonymous  {         public static void main(String args[]) {                 System.out.println("main ");                 MyShape ms = new MyShape() {             double myn;             public double area() {                                 return (myn * myn);             }                         public void setValue(double v)             {                 myn =v ;         ...

Vector and ArrayList Demo Example

import java.util.ArrayList; import java.util.Vector; /**  *  * @author bhavesh  */ class Student {     private int rno;     private String name;     public Student(int rno, String name) {         this.rno = rno;         this.name = name;     }     public int getRno() {         return rno;     }     public String getName() {         return name;     }             } public class VectorTest {     public static void main(String[] args) {                       Vector v = new Vector();                 v.add(123);         v.add(23.45);                 v.add("ISTAR");       ...

KeyEvent Example

package gui; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class GUIKeyEventExample extends JFrame implements KeyListener{     private JTextArea ta1, ta2;     private JScrollPane sp1, sp2;     public GUIKeyEventExample() {               ta1 = new JTextArea(10,30);         ta2 = new JTextArea(10,40);         sp1 = new JScrollPane(ta1);         sp2 = new JScrollPane(ta2);         setLayout(new FlowLayout(FlowLayout.LEFT));         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);         setSize(400,400);         ta1.addKe...

FileDemoDeleteRecord Example

import java.io.*; import java.util.Scanner; public class FileDeleteExample {     public static void main(String[] args) {         Scanner sin = new Scanner(System.in);         System.out.println("Enter Student name whose record to be deleted");         String sname = sin.next();         File infile = new File("d:/mydatafile/stud.txt");         File tempfile = new File("d:/mydatafile/temp.txt");                 String instr=null;         try {                         FileReader fr = new FileReader(infile);         BufferedReader br = new BufferedReader(fr);                 BufferedWriter bw = new BufferedWriter(new FileWriter(tempfile));         while ( (instr=b...