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

Edited 6 Years Ago by juniper2009: n/a

You have created an action listener, but you have not associated it with the button.
Check out the addActionListener method that JButton inherits from its supercalss AbstractButton

Hi, I'd like to see your entire code to see what's going on but did you try to do a System.out.println to display if the right items are being stored in an array?

System.out.println(Reg[0]);

You have created an action listener, but you have not associated it with the button.
Check out the addActionListener method that JButton inherits from its supercalss AbstractButton

DEAR JAMES
I have added these right after Register Button;

JButton b1=new JButton("Register");
    	b1.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent ae)
    		{
    			actionPerformed(ae);  //error line
    		}

    	});

when I press the button it gives me error on the error line above.
I have checked my actionPerformed (SEE BELOW) class but i can not see anything;

public void actionPerformed(ActionEvent ae) 
    {
    	if(ae.getSource()==b1)
    	{
    	  Reg[0]=tf1.getText();
    	  Reg[1]=tf2.getText();
    	  Reg[2]=tf3.getText();
    	  Reg[3]=tf4.getText();

Hi, I'd like to see your entire code to see what's going on but did you try to do a System.out.println to display if the right items are being stored in an array?

System.out.println(Reg[0]);

DEAR HARANA BOY
Please do check my reply to JAMES!

You have already defined your class Register as an ActionListener, and supplied the appropriate method (line 76) so it's a mistake to add another new listener. All you need to do is to add the current instance of Register -= as in
b1.addActionListener(this);

ps I see what you were trying to do with your new listener - call the existing listener method, but you new line 5 actionPerformed will call the version defined on line 3 (ie itself) in an infinite loop. Anyway, you don't need the new listener, just add (this)

Edited 6 Years Ago by JamesCherrill: ps

This question has already been answered. Start a new discussion instead.