Results 1 to 4 of 4
  1. #1
    zed420 is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default ActionListener NOT working

    Hi All
    Can someone please tell me why my ActionListener NOT working. There are NO errors in the programme. It connects to Database fine. It wont populate the table(members).
    Thanks
    Zed
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import javax.swing.*;
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.SQLException; 
    import java.sql.Driver;
     
     public class PersonalInfo2 extends JFrame implements ActionListener {
    	 
    	 Connection conn=null;
    	 JButton but1, but2;
    	 JTextField tf1, tf2;
    	 JLabel lb1,lb2;
    	 JPanel Np, Sp;
    
    	public PersonalInfo2(){
    
    	 Container cPane = getContentPane();
    	 cPane.setLayout(new FlowLayout());
    	 	
    	 JPanel Np = new JPanel();
    	 cPane.add(Np);
    	 JPanel Sp = new JPanel();
    	 cPane.add(Sp);
    	 
    	 JButton but1 = new JButton("Submit");
    	 Np.add(but1);
    	 but1.addActionListener(this);	
         
         JButton but2 = new JButton("Cancel");
         Np.add(but2);
         but2.addActionListener(this);
    	 
    	 JLabel lb1 = new JLabel("First Name",JLabel.CENTER);
         Np.add(lb1);
         JTextField tf1 = new JTextField(20);
         Np.add(tf1);
         tf1.addActionListener(this);
         JLabel lb2 =new JLabel("Last Name",JLabel.CENTER);
         Np.add(lb2);
         JTextField tf2=new JTextField(20);
         Np.add(tf2);
         tf2.addActionListener(this);
         Np.setLayout(new GridLayout(2,1));
         Np.setBackground(Color.CYAN);
    }
    
     public void addRecord(){
        String firstname = tf1.getText();
        String surname = tf2.getText();
        
        	String url = "jdbc:mysql://localhost:3306/"; 
    		String dbName = "perinfo"; 
    		String userName = "root"; 
    		String password = ""; 
    		String driver = "com.mysql.jdbc.Driver"; 
    
        try 
          {
    		Class.forName(driver).newInstance(); 
    		Connection conn = DriverManager.getConnection(url+dbName,userName,password); 
            conn.setAutoCommit(false);
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO members values(?,?)");
            pstmt.setString(1,firstname);
            pstmt.setString(2,surname);
            pstmt.executeUpdate();
            conn.commit();
            conn.close();
          } catch (Exception ex) 
          {
            ex.printStackTrace();
          } 
      }
     public void actionPerformed(ActionEvent ae){
     	if (ae.getSource() == but1)
     	{
     		addRecord();
     	}
     } 	  	
       	 public static void main (String[] args) {
    		System.out.println("Personal information 2");
    		PersonalInfo PerInfo = new PersonalInfo();
            PerInfo.setTitle("Personal Information 2");
            PerInfo.setSize(400,200);
            PerInfo.setLocation(100,40);
            PerInfo.setVisible(true);
          	PerInfo.addWindowListener(new WindowAdapter() {
    	public void windowClosing(WindowEvent e) {
    		System.exit(0);
    			}
    		});
    	}
     
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What you first need to do is some debugging. I suggest you either run your code under a debugger and carefully follow the state of your variables as your code runs, or else sprinkle a bunch of println statements throughout your code:
    Java Code:
    System.out.println("Debug 23: Variable xyz is: " + xyz);
    Much luck

    edit: also, this:
    Java Code:
    PersonalInfo PerInfo = new PersonalInfo();
    Needs to be changed to this:
    Java Code:
    PersonalInfo2 PerInfo = new PersonalInfo2();
    I think.
    Last edited by Fubarable; 12-20-2009 at 03:49 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Zed: Please see a private message regarding cross-posting.

  4. #4
    zed420 is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks Fubarable I had two school boy mistakes
    1
    Java Code:
    PersonalInfo2 PerInfo = new PersonalInfo2();
    2these should've been initialized
    Java Code:
             JButton but1 = new JButton("Submit");
             JTextField tf1 = new JTextField(20);

Similar Threads

  1. How to access the ActionListener
    By jboy in forum New To Java
    Replies: 3
    Last Post: 10-15-2009, 07:04 PM
  2. NullPointerException by ActionListener
    By YouGina in forum AWT / Swing
    Replies: 8
    Last Post: 07-10-2009, 12:08 PM
  3. ActionListener Error
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 09:36 AM
  4. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  5. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 11:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •