Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Passing data from one action event to another

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Passing data from one action event to another

    First of all i hope this is the right section for this if not please move

    this is the first code

    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
     
    public class ChickenListener implements ActionListener {
        int loopctrl;
     
        static Connection cn;
        static Statement st;
        static ResultSet rs;
        static PreparedStatement ps;
     
        String sql = "";
        String url = "jdbc:mysql://localhost/";
        String dbName = "restaurant";
        String driver = "com.mysql.jdbc.Driver";
        String username = "root";
        String password = "";
        String errorCode;
     
        FriedChicken parent;
     
        public ChickenListener (FriedChicken parent) {
            this.parent = parent;
        }
        public void actionPerformed (ActionEvent ae) {
            String tempString;
            Object event = ae.getSource ();
     
            for (loopctrl = 0; loopctrl <8; loopctrl++) {
    	    if (event == parent.choose[loopctrl]) {
                    tempString = parent.choose[loopctrl].getText();
                    new QuantityCounter ();
     
                    parent.abc.setText (hld.holdString);
     
                    try {
                        Class.forName (driver).newInstance ();
                        cn = DriverManager.getConnection (url + dbName, username, password);
     
                	    try {
                            st = cn.createStatement ();
                            ps = cn.prepareStatement ("INSERT INTO corder VALUES (?,?,?)");
                            ps.setString (1,tempString);
                            ps.setInt (2,0); //quantity
                            ps.setString (3,"1999-01-01");//date yyyy-mm-dd
                            ps.executeUpdate ();
                        }
                        catch (SQLException ec) {
                            errorCode = ("Error code: " + ec);
                        }
                    }
                    catch (Exception ec) {
                         errorCode = ("Error code: " + ec);
                    }
                }
            }
        }
    }

    the second code

    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
     
    public class QuantityCounterListener implements ActionListener {
        String tempStrng;
        int tempInt;
        int x = 0;
        static Connection cn;
        static Statement st;
        static ResultSet rs;
        static PreparedStatement ps;
     
        String sql = "";
        String url = "jdbc:mysql://localhost/";
        String dbName = "restaurant";
        String driver = "com.mysql.jdbc.Driver";
        String username = "root";
        String password = "";
        String errorCode;
     
        QuantityCounter parent;
     
        public QuantityCounterListener (QuantityCounter parent) {
            this.parent = parent;
        }
     
        public void actionPerformed (ActionEvent ae) {
            Object event = ae.getSource ();
            Holder hld = new Holder ();
            if (event == parent.okBtn) {
                tempStrng = parent.quanFld.getText ();
    	    tempInt = Integer.parseInt (tempStrng);
     
                try {
                    Class.forName (driver).newInstance ();
                    cn = DriverManager.getConnection (url + dbName, username, password);
     
            	try {
                        st = cn.createStatement ();
                        ps = cn.prepareStatement ("UPDATE corder SET quant = ? WHERE oid = ?");
                        ps.setInt (1,tempInt);
                        ps.setString (2,//x); // need to transfer here the tempString in code 1
                        ps.executeUpdate ();
                    }
                    catch (SQLException ec) {
                        errorCode = ("Error code: " + ec);
                    }
                }
                catch (Exception ec) {
                    errorCode = ("Error code: " + ec);
                } 
     
                parent.frame.setVisible (false);
            }
        }
    }

    what my problem is i need to transfer what does "tempString" from code 1 holds to code 2 in the //x part in the prepared statement.

    can somebody help me? need help asap XD

    thanks and more power!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Passing data from one action event to another

    User getter (accessor) or setter (mutator) methods to pass values between class instances.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Passing data from one action event to another

    Quote Originally Posted by GregBrannon View Post
    User getter (accessor) or setter (mutator) methods to pass values between class instances.
    mind modifying my code abit? or giving an example? im kind of confuse?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Passing data from one action event to another

    I suggest that another class be created to hold all of the relevant data for the entire program. The data kept in an instance of that class should accurately reflect the application's state at any given instant in time, and each supporting object should have a reference to that instance. (If it's possible that the data could be changed or accessed by clients from multiple threads, then you should also read up on synchronized methods.) THEN, any of the other application's objects could update or retrieve the data needed from that instance using getter and setter methods.

    If you are not familiar with the use of accessor (getter) and mutator (setter) methods then I suggest you study up on them, because they are a basic element of encapsulation that you should know.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Passing data from one action event to another

    Quote Originally Posted by GregBrannon View Post
    I suggest that another class be created to hold all of the relevant data for the entire program. The data kept in an instance of that class should accurately reflect the application's state at any given instant in time, and each supporting object should have a reference to that instance. (If it's possible that the data could be changed or accessed by clients from multiple threads, then you should also read up on synchronized methods.) THEN, any of the other application's objects could update or retrieve the data needed from that instance using getter and setter methods.

    If you are not familiar with the use of accessor (getter) and mutator (setter) methods then I suggest you study up on them, because they are a basic element of encapsulation that you should know.
    ok so i studied abit of the accessor and mutator and here's what i got:

     
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
     
    public class ChickenListener implements ActionListener {
        int loopctrl;
     
        static Connection cn;
        static Statement st;
        static ResultSet rs;
        static PreparedStatement ps;
     
        String sql = "";
        String url = "jdbc:mysql://localhost/";
        String dbName = "restaurant";
        String driver = "com.mysql.jdbc.Driver";
        String username = "root";
        String password = "";
        String errorCode;
     
        FriedChicken parent;
     
        public ChickenListener (FriedChicken parent) {
            this.parent = parent;
        }
        public void actionPerformed (ActionEvent ae) {
            String tempString;
            Object event = ae.getSource ();
     
     
     
            for (loopctrl = 0; loopctrl <8; loopctrl++) {
    	    if (event == parent.choose[loopctrl]) {
                    tempString = parent.choose[loopctrl].getText();
                    new QuantityCounter ();
     
    		Holder hld = new Holder (tempString);
     
                    try {
                        Class.forName (driver).newInstance ();
                        cn = DriverManager.getConnection (url + dbName, username, password);
     
                	    try {
                            st = cn.createStatement ();
                            ps = cn.prepareStatement ("INSERT INTO corder VALUES (?,?,?)");
                            ps.setString (1,tempString);
                            ps.setInt (2,0); //quantity
                            ps.setString (3,"1999-01-01");//date yyyy-mm-dd
                            ps.executeUpdate ();
                        }
                        catch (SQLException ec) {
                            errorCode = ("Error code: " + ec);
                        }
                    }
                    catch (Exception ec) {
                         errorCode = ("Error code: " + ec);
                    }
                }
            }
        }
    }

     
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
     
    public class QuantityCounterListener implements ActionListener {
        String tempStrng;
        int tempInt;
        int x = 0;
        static Connection cn;
        static Statement st;
        static ResultSet rs;
        static PreparedStatement ps;
     
        String sql = "";
        String url = "jdbc:mysql://localhost/";
        String dbName = "restaurant";
        String driver = "com.mysql.jdbc.Driver";
        String username = "root";
        String password = "";
        String errorCode;
     
        QuantityCounter parent;
     
        public QuantityCounterListener (QuantityCounter parent) {
            this.parent = parent;
        }
     
        public void actionPerformed (ActionEvent ae) {
            Object event = ae.getSource ();
            Holder hld = new Holder ();
            if (event == parent.okBtn) {
                tempStrng = parent.quanFld.getText ();
    	    tempInt = Integer.parseInt (tempStrng);
     
                try {
                    Class.forName (driver).newInstance ();
                    cn = DriverManager.getConnection (url + dbName, username, password);
     
            	try {
                        st = cn.createStatement ();
                        ps = cn.prepareStatement ("UPDATE corder SET quant = ? WHERE oid = ?");
                        ps.setInt (1,tempInt);
                        ps.setString (2,(new Holder.getHold())); //quantity
                        ps.executeUpdate ();
                    }
                    catch (SQLException ec) {
                        errorCode = ("Error code: " + ec);
                    }
                }
                catch (Exception ec) {
                    errorCode = ("Error code: " + ec);
                } 
     
                parent.frame.setVisible (false);
            }
        }
    }

    here is the "holder" class

    public class Holder {
        public String tempString;
     
        public Holder (String tempString) {
        	this.tempString = tempString;
        }
     
        public String getHold () {
            return tempString;
        }
     
    }

    but i dont know what am i doing wrong it doesnt work

Similar Threads

  1. aCTION EVENT WITHOUT ACTIONLISTENER
    By ranzer29 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 1st, 2013, 01:22 AM
  2. Problem with Action Event
    By jayzee98 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: August 29th, 2012, 10:04 AM
  3. [SOLVED] difference between action event from 2 buttons with same name?
    By nggdowt in forum AWT / Java Swing
    Replies: 5
    Last Post: November 7th, 2011, 10:51 AM
  4. Replies: 9
    Last Post: June 29th, 2011, 12:27 PM
  5. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 06:24 AM

Tags for this Thread