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. Program that demonstrates type casting

  2. Program to read rollno, marks of sub1,sub2 and sub3 and on basis of that display result

  3. Program to display Floyds Triangle of a numbers

  4. An applet program to display moving banner

  5. Program to read the ballots and count the votes cast for each candidate using an array variable count

  6. Program to read the table elements into a two-dimensional array temperature

  7. Program to show the use of If-Else Statement

  8. Program to display triangle of numbers

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

  10. An application that combines several classes and interface

  11. Program to calculate the frequency for different values of C starting from 0.01 to 0.1 in steps of 0.01

  12. An applet program showing Clicked Event on canvas

  13. Program of UDP news server

  14. Program in CORBA which takes a long sentence from the client (at least 7 words), passes to the server, server separates out each word

  15. An applet program to draw different shapes

  16. Develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side, executed on server

  17. Program to copy the elements of an array into another array in reverse order and print them

  18. Program, which creates a three thread. One thread display the numbers from 1 to 5, second thread display the square root of that number etc

  19. Program to show some properties of StringBuffer object

  20. 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

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