Results 1 to 4 of 4
Thread: ActionListener NOT working
- 12-20-2009, 02:34 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
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); } }); } }
-
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:
Much luckJava Code:System.out.println("Debug 23: Variable xyz is: " + xyz);
edit: also, this:
Needs to be changed to this:Java Code:PersonalInfo PerInfo = new PersonalInfo();
I think.Java Code:PersonalInfo2 PerInfo = new PersonalInfo2();
Last edited by Fubarable; 12-20-2009 at 02:49 PM.
-
Zed: Please see a private message regarding cross-posting.
- 12-20-2009, 03:40 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
How to access the ActionListener
By jboy in forum New To JavaReplies: 3Last Post: 10-15-2009, 06:04 PM -
NullPointerException by ActionListener
By YouGina in forum AWT / SwingReplies: 8Last Post: 07-10-2009, 11:08 AM -
ActionListener Error
By blackstormattack in forum New To JavaReplies: 1Last Post: 03-05-2009, 08:36 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
ActionListener interface
By tsantana in forum New To JavaReplies: 2Last Post: 03-30-2008, 10:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks