Dear All
I am a newbie Java Learner.I try to learn over Java API Docs.But still I need some help.
I have a gui which consist of four textfields(tf1,tf2,tf3,tf4) and a button(b1).
If I press the button(b1)"Register", I want my action write whole textfield sources into a text.txt file by using buffer writer method.and also tyring to write line by line.
Here is the codes and I just want to know where I am doing wrong?
Thanks in advance
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Register extends JFrame implements ActionListener
{
public static void main(String arg[])throws IOException
{
Register rd=new Register();
}
String[] Reg = {"","","",""};
JButton b1;
JTextField tf1;
JTextField tf2;
JTextField tf3;
JTextField tf4;
public Register()
{
JPanel p=new JPanel();
JPanel p3=new JPanel();
p3.setSize(10,100);
p3.setLayout(new GridLayout(3,1,20,20));
JPanel p2=new JPanel();
p2.setSize(10,50);
JPanel p5=new JPanel();
p5.setSize(10,50);
p5.setLayout(new GridLayout(2,1,20,20));
Label l3=new Label("New Record", Label.CENTER);
JLabel jl=new JLabel(" ");
JLabel j2=new JLabel(" ");
p.setSize(20,200);
p.add(l3);
JPanel pl=new JPanel();
pl.setSize(30, 50);
pl.setLayout(new GridLayout(0,2,0,15));
setLayout(new BorderLayout());
JLabel l1=new JLabel(" UserName:",Label.RIGHT);
JLabel l2=new JLabel(" Password:",Label.RIGHT);
JLabel l4=new JLabel(" FullName:",Label.RIGHT);
JLabel l5=new JLabel(" ID Nr:",Label.RIGHT);
JTextField tf1=new JTextField(30);
JTextField tf2=new JTextField(8);
JTextField tf3=new JTextField(30);
JTextField tf4=new JTextField(30);
JButton b1=new JButton("Register"); //register button
pl.add(l1);
pl.add(tf1);
pl.add(l2);
pl.add(tf2);
pl.add(l4);
pl.add(tf3);
pl.add(l5);
pl.add(tf4);
p2.add(b1);
p3.add(j2);
p3.add(p2);
p5.add(l3);
p5.add(jl);
add(p5, BorderLayout.NORTH);
add(pl,BorderLayout.WEST);
add(p3,BorderLayout.SOUTH);
setSize(700,400);
setVisible(true);
}//end Register
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)//button clicked
{
Reg[0]=tf1.getText(); //textfield 1
Reg[1]=tf2.getText(); //textfield 2
Reg[2]=tf3.getText(); //textfield 3
Reg[3]=tf4.getText(); //textfield 4
PrintWriter print;
try
{
print = new PrintWriter(new BufferedWriter(new FileWriter("test.txt",true)));
for(int x=0;x<=4;x++)
{
print.print(Reg[x] + " ");
print.println();
}
print.println("---");
print.close();
}
catch (IOException ex)
{
Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
}
}//end if
}// end of actionperformed
}//ende of Main JFrame