I am doing my home work in which i have to pass values from java text field to text text file. My code of program is given below. The problem is that the program is not passing values from text field to text file. Please help me.
/**
* @(#)GUITest.java
*
*
* @author
* @version 1.00 2010/5/15
*/
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
public class GUITest {
JFrame myFrame;
JTextField tf,tf1,tf2,tf3;
JLabel label,label1,label2,label3,label4 ;
JButton b1;
//method used by
public void initGUI() {
myFrame = new JFrame();
Container c = myFrame.getContentPane();
c.setLayout(new GridLayout(12,1));
JTextField tf = new JTextField(10);
JTextField tf1 = new JTextField(10);
JTextField tf2 = new JTextField(10);
JTextField tf3 = new JTextField(10);
JLabel label4 = new JLabel(" Car Registration Form ");
JLabel label = new JLabel("Engine Number :");
JLabel label1 = new JLabel("Model :");
JLabel label2 = new JLabel("Maker :");
JLabel label3 = new JLabel("Owner Name :");
JButton b1 = new JButton("Save");
b1.setPreferredSize(new Dimension(70,25));
c.add(label4);
c.add(label);
c.add(tf);
c.add(label1);
c.add(tf1);
c.add(label2);
c.add(tf2);
c.add(label3);
c.add(tf3);
c.add(b1);
//CALL button Handler
ButtonHandler b1Handler = new ButtonHandler();
b1.addActionListener(b1Handler);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(500,500);
myFrame.setVisible(true);
}
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event){
FileWriter fw = null;
PrintWriter pw = null;
try {
// attaching node stream with data source
// if file does not exist, it automatically creates it
fw = new FileWriter ("car_reg.txt");
// attatching filter stream over node stream
pw = new PrintWriter(fw);
String s1 = tf.getText();
String s2 = tf1.getText();
String s3 = tf2.getText();
String s4 = tf3.getText();
// writing First string to file
pw.println(s1);
// writing Second string to file
pw.println(s2);
// writing Third string to file
pw.println(s3);
// writing Fourth string to file
pw.println(s4);
// flushing stream
pw.flush();
// closing streams
pw.close();
fw.close();
}catch(IOException ioex){
System.out.println(ioex);
}
}
}
public GUITest()
{
initGUI();
}
public static void main (String args[]){
GUITest gui = new GUITest();
}
}