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

  2. Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception

  3. Corba program of date operations

  4. Program to show an example of modifying String objects

  5. Program that will count the number of lines in each file that is specified on the command line

  6. An applet program of passing parameters

  7. Program to show an example of using a Static Nested Class

  8. Program to show the use of For Loop

  9. An applet program that concatenates two string entered in TextField

  10. Program that enabled applet calculator

  11. Program to show the use of Comparison operator (==) for comparing Strings

  12. An applet program that creates string dynamically and displays it

  13. Program to print triangle of numbers

  14. Program to ask question and accept answer up to 3 attempt and display right answer if it is wrong

  15. Program to show an example of creating String objects from Character Arrays

  16. Program of insertion sort

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

  18. GUI application in java, which enter the details of a student and on the submit display the details of the student

  19. Program using session tracking capability of Servlets, which displays information of current user session by HttpServletRequest object

  20. An applet program to find maximum of three numbers using textField

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