Page 1 of 2 12 LastLast
Results 1 to 20 of 24
Like Tree3Likes

Thread: ATM Machine - Inner Listener Class

  1. #1
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default ATM Machine - Inner Listener Class

    Couple of problems here...
    1. I am trying to use the getSource method in my inner class, in order to set the JPasswordField within an Inner Listener Class. This is a 4 integer password, setup by my for statement in the loop. I know what I want the program to do, but I don't know the correct language to use. I want the user to press one of the keys (0-9), that value be stored in JPassword (at least I think that is how it works) and for it to display the "*" in the Field---I want this to loop 4x. How do I use the getSource() to do this.
    Also, I believe I need to use the set and get methods for the password entered into the JPassField, is that correct?

    Note: The inner class is not recognizing my the object password that represents the JPasswordField.

    2. In my second Inner Listener Class, clearButton, I set the event.getSource method to reset the JPasswordField to " ", when the clear button is pressed. Again, the problem is that the inner class does not recognize password. Why is this the case, since it is still a part of the parent class Atm?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Atm extends JFrame {
    	Atm(){
    		super("ATM");
    		int i = 0;
    		
    	//Create Panels For ATM
    		JPanel buttonPanel1 = new JPanel();     //To Contain Digits 1-9
    		buttonPanel1.setLayout(new GridLayout(4, 3));
    		
    		JPanel passwordPanel = new JPanel();	//To Contain JPasswordField
    		passwordPanel.setLayout(new GridLayout(1, 1));
    		
    		JPanel titlePanel = new JPanel();	    //Contains PIN Authentication Title
    
    	//Create 9 Digit Buttons and Add to Panel1
    		final JButton [] buttonArray = new JButton [10];
    
    		for(i = 0 ; i < buttonArray.length; i++){
    			buttonArray[i] = new JButton();}
    		
    		for(i = 1 ; i <= 9 ; i++){
    			buttonPanel1.add(new JButton(" " + i));
    		}
    		
    	//Additional Panel1 Buttons 
    		final JButton enter = new JButton("ENTER");
    		final JButton zero = new JButton("0");
    		final JButton clear = new JButton("CLEAR");
    		buttonPanel1.add(enter);
    		buttonPanel1.add(zero);
    		buttonPanel1.add(clear);
    		
    	//Panel 2 Message and Password Field
    		final JPasswordField password = new JPasswordField(4);
    		password.setEchoChar('*');
    		passwordPanel.add(new JLabel(" "));
    		passwordPanel.add(new JLabel("Welcome, Please Enter PIN: "));
    		passwordPanel.add(password);
    		
    	//Panel 3 PIN Authentication Title
    		titlePanel.add(new JLabel("ATM PIN AUTHENTICATION"), BorderLayout.CENTER);
    	
    	add(passwordPanel, BorderLayout.NORTH);	
    	add(buttonPanel1, BorderLayout.CENTER);
    	add(titlePanel, BorderLayout.SOUTH);
    	
    	//Implement Constructors For Each Class Action
    	buttonAction perform = new buttonAction();
    		for(i = 0; i < buttonArray.length; i++){
    			buttonArray[i].addActionListener(perform);
    			buttonArray[i].setActionCommand(buttonArray[i].getText());
    		}
    		//password.addActionListener(perform);
    	clearAction delete = new clearAction();
    		clear.addActionListener(delete);
    	enterAction execute = new enterAction();
    		enter.addActionListener(execute);
    	
    	}
    	
    	//Inner Listener Classes
    	private class buttonAction implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			for(int i = 0; i < 4; i++){			//Allow The Numeric Buttons to be pressed four times
    				event.getSource(); 
    				}
    			}
    		}
    	private class clearAction implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			password.setText("");
    		}
    	}
    	private class enterAction implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			
    		}
    	}
    }
    
    
    
    
    	
    	
    	//Display according to password input
    /*			if (isPasswordCorrect(password)) {
    				JOptionPane.showMessageDialog(null, "Password is correct!");			
    			}
    			else if(isPasswordIncorrect(password)){
    				for(int i = 0 ; i < 3 ; i++)
    				JOptionPane.showMessageDialog(null,"Sorry. Try again.\n" + "You have" + i + "times remaining", 
    						"Error Message",JOptionPane.ERROR_MESSAGE);
    			}
    			else{
    				JOptionPane.showMessageDialog(null, "Please contact administrator.");
    			}*/

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,396
    Rep Power
    25

    Default Re: ATM Machine - Inner Listener Class

    How do I use the getSource()
    The EventObject class's getSource() method returns a reference to the object that caused the event. Cast that reference to the type of the object and you will be able to call that object's methods as needed.

    You need to consider how control flows when using GUI and listeners. Every user caused event will cause the jvm to call the listener. The listener should handle that event and exit to allow the user to cause the next event. If there are 4 events from the user, then that loop will go around 4 times: user to listener back to jvm, user to listener back to jvm etc
    The execution can NOT do a loop in a listener to get multiple user inputs.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: ATM Machine - Inner Listener Class

    First, don't put getSource in a loop. There is only one source and it won't changed for the duration of the event.
    Second, you need to cast the event source to the type you expect. You can check the type with instanceof. if needed, you can
    also use action commands to discern between different event sources.


    Edit: geez! I am always one step behind Norm. Why do I bother? :)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,396
    Rep Power
    25

    Default Re: ATM Machine - Inner Listener Class

    But you answer the questions more thoroughly than I do. Mine are sort of like a heading for yours.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,993
    Rep Power
    5

    Default Re: ATM Machine - Inner Listener Class

    Quote Originally Posted by jim829 View Post
    Edit: geez! I am always one step behind Norm. Why do I bother? :)
    Hey duplicate answers are not a bad thing; its a good way to compare notes.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Inner Listener Class

    So you are saying that I should do something like the following:

    Java Code:
    private class buttonAction implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			for(int i = 0; i < 4; i++){			//Allow The Numeric Buttons to be pressed four times
    				Object source = event.getSource(); 
    					if(source == buttonArray[i]){
    						
    					}
    				}
    Why is the buttonArray not recognized in the inner class? That doesn't make any sense to me. It is still a part of the main class, Atm.


    I think I understand what you are saying Norm, about using loops with getSource() and if this is the case, I don't need any for loop within the inner class...? Does that mean that the JPasswordField will not accept anymore inputs since it was predefined with only 4 columns?
    Last edited by javaStooge; 03-31-2014 at 04:38 PM.

  7. #7
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Inner Listener Class

    Quote Originally Posted by gimbal2 View Post
    Hey duplicate answers are not a bad thing; its a good way to compare notes.
    An added plus is it gives me more than one perspective on the issue.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: ATM Machine - Inner Listener Class

    Columns have nothing to do with the events. Every time you "press" a button, the event will be fired. The source of the event tells you which button your pressed.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,396
    Rep Power
    25

    Default Re: ATM Machine - Inner Listener Class

    I don't need any for loop within the inner class
    It depends what the loop is supposed to do. Can you explain why you want a loop in the listener method?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Inner Listener Class

    Quote Originally Posted by Norm View Post
    It depends what the loop is supposed to do. Can you explain why you want a loop in the listener method?
    The reason I was using the for loop in the buttonAction listener class was so that the user would be limited to entering the number of integers to 4...since the passcode is only 4 digits.

    And, I'm going to go ahead and assume that the reason the buttonArray isn't working must be fairly obvious....and I'm going to guess that I don't need it there.
    Last edited by javaStooge; 03-31-2014 at 04:57 PM.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,396
    Rep Power
    25

    Default Re: ATM Machine - Inner Listener Class

    the for loop in the buttonAction listener
    The listener is called when the button is pressed. There is only ONE press per call to the listener.

    From post#2:
    You need to consider how control flows when using GUI and listeners. Every user caused event will cause the jvm to call the listener. The listener should handle that event and exit to allow the user to cause the next event. If there are 4 events from the user, then that loop will go around 4 times: user to listener back to jvm, user to listener back to jvm etc
    The execution can NOT do a loop in a listener to get multiple user inputs.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: ATM Machine - Inner Listener Class

    I suggest you write some small programs to simply play around with buttons and textfields. Do buttons first. simply create ten buttons labeled 0 thru 9. Set up an actonlistener and each time the actionPerformed method is called, display the buttons name. You don't need to save the button instances at all. Just retrieve them from the event object. Once you do this and similar tests with the text fields and other JComponents your problem will be much easier to solve. And I really do follow my own advice. When confronted with something I don't use often (like JTrees, JTables, editors) I play around with the features before I integrate the component into a larger program.

    Regards,
    Jim
    Norm likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Inner Listener Class

    Thank you guys and I will heed your advice Jim. Am I on the right path with this program, or should I scrap it and start over?

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: ATM Machine - Inner Listener Class

    I would not scrap it. But for now, focus on getting the buttons to work properly. As I said, you don't need to save the references. The proper reference for the button that generated the event will be provided in the event source. So you can get the button number from that.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Inner Listener Class

    I wrote a smaller, more condensed version of the previous code:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class run extends JFrame{
    	run(){
    		
    	super("Action Listener");
    	
    	//Step One: Foundation, build panels?
    	JPanel panel1 = new JPanel();    //Construct Panel
    	panel1.setLayout(new GridLayout(1,2));		//What style or format will panel be in.
    	
    	JPanel panel2 = new JPanel();
    	panel2.setLayout(new BorderLayout());
    	
    	//Step Two: What is the purpose of the panels?
    	final JButton butt1 = new JButton("Butt 1"); //Construct Object to exist
    	final JButton butt2 = new JButton("Butt 2");
    	
    	final JPasswordField password = new JPasswordField(10);
    	
    	//Step Three: Where will objects exits?
    	panel1.add(butt1);
    	panel1.add(butt2);
    	panel2.add(password);
    	
    	//Where will the newly constructed panels exits?
    	add(panel1, BorderLayout.SOUTH);
    	add(panel2, BorderLayout.NORTH);
    	
    	//What is the purpose of the buttons?
    	final classAction button1 = new classAction();		//Construct ActionListener for buttons
    	butt1.addActionListener(button1);				//Initialize ActionListener for buttons
    	classAct button2 = new classAct();
    	butt2.addActionListener(button2);
    	}
    	private class classAction implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			int butt = butt1.getSource();
    						
    		}
    	}
    	private class classAct implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			
    		}
    	}
    }
    Now, I've been watching some very nice tutorial videos by Cave Of Programming on YouTube, and in a particular video of his regarding Event Handling he uses the getSource() method with his object variable. He gets an error at first but then he says that he can do one of two things, either make his object variable an instance variable, or just make it final -- which he chooses to make final. When I use final, however, it does not work for me.
    I understand the purpose of getSource, I'm not sure how to use it.

    Also, the only thing I can find in other forums regarding JPassField, is how to use the keyboard to enter text and not JButtons.
    Last edited by javaStooge; 03-31-2014 at 06:14 PM.

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,993
    Rep Power
    5

    Default Re: ATM Machine - Inner Listener Class

    I'm going to pitch you a tiny bit of code and hopefully you'll pick up on what it implies yourself:

    Java Code:
    public class ActionListenerTest extends JFrame {
    
      private JButton button1;
      private JButton button2;
    
      public ActionListenerTest(){
    
        button1 = new JButton("1");
        button2 = new JButton("2");
    
        button1.addActionListener(new MyActionListener());
        button2.addActionListener(new MyActionListener());
    
        // etc. etc.
      }
    
    
      private class MyActionListener implements ActionListener {
    
        public void actionPerformed(ActionEvent event){
           // option 1: caring which button it is
           if(event.getSource() == button1){
    
           } else if(event.getSource() == button2){
    
           }
    
          // option 2: not caring
          JButton theButtonPressed = (JButton) event.getSource();
        }
      }
    }
    Bit of a dumb example that does not represent any real-life use case, but I'm short on time.
    javaStooge likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: ATM Machine - Inner Listener Class

    Regarding the password field, I suggest you not use it. I believe (and others may correct me) that the password field is a way to hide the typed in password. But you are not typing anything in. So when the buttons are pressed, you can put the "*" in the field yourself as an indicator to the user but save the actual number in the real password field. So all you really need is a JTextField. You can place the "*" or whatever character you choose in the field when your button event fires.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Inner Listener Class

    That is what I started thinking when I couldn't find anything on the web, except our professor specifically asked for the password field.
    Attached Thumbnails Attached Thumbnails ATM Machine - Inner Listener Class-screen-shot-2014-03-31-2.07.27-pm.jpg  

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: ATM Machine - Inner Listener Class

    Well, then I guess you better use it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: ATM Machine - Inner Listener Class

    Quote Originally Posted by jim829 View Post
    Well, then I guess you better use it.

    Regards,
    Jim
    HOW!?!?!?!?

    kindly

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 10
    Last Post: 02-28-2012, 08:44 PM
  2. Action Listener listen to another class/ program
    By kevinn205 in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 10:59 PM
  3. Replies: 4
    Last Post: 08-22-2011, 03:42 PM
  4. Replies: 0
    Last Post: 02-07-2011, 01:27 PM
  5. Replies: 2
    Last Post: 03-01-2010, 03:19 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
  •