Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » File HandlingRSS Feeds

Program of creating a file using text fields in windows

Posted By: Easy Tutor     Category: Java     Views: 9516

Write a program of creating a file using text fields in windows.

Code for Program of creating a file using text fields in windows in Java

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 Framepublic StudentFile(){
             super("Create Student File");
        }
        
         // Setup the windowpublicvoid 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 filetry{
               dos = new DataOutputStream( 
                                 new FileOutputStream("Student.txt"));
                } 
            catch(Exception e) {
                          System.err.println(e.toString());
                          System.exit(1);}

        }

        //Write to the filepublicvoid 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 TextFieldspublicvoid cleanup(){
              if (!number.getText().equals(" "))
              {
                   addRecord();
              }
              try{
                  dos.flush();
                  dos.close();
                }catch(Exception e) {}
          }

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

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

     //Execute the Programpublicstaticvoid main(String[] args) 
    {
          StudentFile student = new StudentFile();
          student.setup();
    }
}
  
Share: 


Didn't find what you were looking for? Find more on Program of creating a file using text fields in windows Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of creating a file using text fields in windows is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!