Java Tutorial

 
 
 
 
import java.io.*;
import java.awt.*;

class  StudentFile extends Frame
{
       //Defining window components
       TextField number, name, marks;
       Button enter,done;
       Label  lblnum, lblname, lblmarks;

       DataOutputStream dos;

       //Initialize the Frame
       public StudentFile(){
             super(\"Create Student File\");
        }

         // Setup the window
       public void setup()
       {
            resize(400,200);
            setLayout(new GridLayout(4,2));
            //Create the componenets of the Frame
            number = new TextField(25);
            name = new TextField(25);
            marks = new TextField(25);
            lblnum = new Label(\"Roll Number\");
            lblname = new Label(\"Student Name\");
            lblmarks = new Label(\"Marks\");
            enter = new Button(\"Enter\");
            done = new Button(\"Done\");

            //Add the components to the Frame
            add(lblnum);
            add(number);
            add(lblname);
            add(name);
            add(lblmarks);
            add(marks);
            add(enter);
            add(done);

            //show the Frame using show method of Frame
            show();

            //open the file
            try{
               dos = new DataOutputStream( 
                                 new FileOutputStream(\"Student.txt\"));
                } 
            catch(Exception e) {
                          System.err.println(e.toString());
                          System.exit(1);}

        }

        //Write to the file
        public void addRecord(){
            int num;
            Double d;
            num = (new Integer(number.getText())).intValue();
            try{
                dos.writeInt(num);
                dos.writeUTF(name.getText());
                 d = new Double(marks.getText());
                 dos.writeDouble(d.doubleValue());
             } catch (Exception e) {}

         //Clear the text fields
         number.setText(\" \");
         name.setText(\" \");
         marks.setText(\" \");
        }

        //Adding the record and clearing the TextFields
        public void cleanup(){
              if (!number.getText().equals(\" \"))
              {
                   addRecord();
              }
              try{
                  dos.flush();
                  dos.close();
                }catch(Exception e) {}
          }

        //Processing the event
        public boolean action(Event event, Object o){
              if (event.target instanceof Button)
              {
                  if (event.arg.equals(\"Enter\"))
                  {
                      addRecord();
                      return true;
                  }
              }
              return super.handleEvent(event);
        }

       public boolean handleEvent(Event event){
            if (event.target instanceof Button)
            {
                if (event.arg.equals(\"Done\"))
                {
                     cleanup();
                     System.exit(0);
                     return true;
                }
            }
            return super.handleEvent(event);
       }

     //Execute the Program
    public static void main(String[] args) 
    {
          StudentFile student = new StudentFile();
          student.setup();
    }
}

    Related Post:
  1. An applet program to display Horizontal Color Bar

  2. Create an interface coversions. Which have the following functions Inchestomillimeters(), hptowatts(),wattstohp(),poundstogram(),gramtopounds()

  3. Program to show the use of Nested Loops

  4. Program to show the use of Labeled Break Statement

  5. Program to show the use of While Loop

  6. Program that enabled applet calculator

  7. Even number ,Prime no. ,Factorial of a number

  8. program which takes a long sentence from the client passes to the server,server separates out each word and sends them back to client in reverse order

  9. Program to show an example of Arrays of String

  10. Program to show an example of Concatination of Strings

  11. Program of an applet to display alternate filled and empty circle

  12. Word list program that reads a text file and makes an alphabetical list of all the words in that file. The list of words is output to another file

  13. Program to show the use of Labeled Continue Statement

  14. Program to show the use of Conditional (Ternary) Operator

  15. RMI based application, which reads a file with list of marks of student from a client, send it to server and find how many students having distinct

  16. Program for calculating area of a circle on server side,when a client sends request along with given radius and then displaying result on client side

  17. Corba program to develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side

  18. Program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7

  19. Application which takes your marks of six subjects from the client side and displays the percentage that you have obtained

  20. Define an array of ten String elements each containing an arbitrary string of the form “month/day/year”

 
 
Didn't find what you were looking for? Find more on Program of creating a file using text fields in windows