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

    Default ActionListener - JButtons

    I've scanned the book and a few other forums trying to find an answer to my particular question (or I simply didn't understand) with no luck. I want to create an ActionListener for my ATM machine, that will both receive the input numbers to check them against the passcode for a predefined user, but also add the typical " * " that we see when performing such an action.

    1. I have created individual objects for each button, which I then used in the constructor "perform" from class action. I'm not sure where to start in the method ActionPerformed.

    2. How do I get the asterisks to appear in the JPasswordField each time a numeric button is pressed on the keypad?


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Atm extends JFrame {
    	Atm(){
    		super("ATM");
    	//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 one = new JButton("1");
    		final JButton two = new JButton("2");
    		final JButton three = new JButton("3");
    		final JButton four = new JButton("4");
    		final JButton five = new JButton("5");
    		final JButton six = new JButton("6");
    		final JButton seven = new JButton("7");
    		final JButton eight = new JButton("8");
    		final JButton nine = new JButton("9");
    		
    		for(int 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);
    		
    	
    	action perform = new action();
    		zero.addActionListener(perform);
    		one.addActionListener(perform);
    		two.addActionListener(perform);
    		three.addActionListener(perform);
    		four.addActionListener(perform);
    		five.addActionListener(perform);
    		six.addActionListener(perform);
    		seven.addActionListener(perform);
    		eight.addActionListener(perform);
    		nine.addActionListener(perform);
    		password.addActionListener(perform);
    	}
    	private class action implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			
    			
    			
    			//Display according to password input
    			if (isPasswordCorrect(password)) {
    				JOptionPane.showMessageDialog(null, "Password is correct!");			
    			}
    			else {
    				JOptionPane.showMessageDialog(null,"Sorry. Try again.", "Error Message",
    						JOptionPane.ERROR_MESSAGE);
    			}
    			
    		}
    	}
    }

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

    Default ActionListener - JButtons

    I've scanned the book and a few other forums trying to find an answer to my particular question with no luck. I want to create an ActionListener for my ATM machine that will both receive the input numbers to check them against the passcode for a predefined user, but also add the typical " * " that we see when performing such an action.

    1. I have created individual objects for each button, which I then used in the object "perform" from class action. I'm not sure where to start in the method ActionPerformed.

    2. Is it possible to put the numeric buttons into an array, so that I can then use the array in the ActionPerformed method? I can write a few lines of code instead of doing it for each button, is this possible?

    For example:
    Java Code:
    JButton [] buttonArray = new JButton [10];
    
    for(int i = 0 ; i <= buttonArray.length; i++);
    	buttonArray[i] = new JButton(" " + i);
    3. How do I get the asterisks to appear in the JPasswordField each time a numeric button is pressed on the keypad?


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Atm extends JFrame {
    	Atm(){
    		super("ATM");
    	//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 one = new JButton("1");
    		final JButton two = new JButton("2");
    		final JButton three = new JButton("3");
    		final JButton four = new JButton("4");
    		final JButton five = new JButton("5");
    		final JButton six = new JButton("6");
    		final JButton seven = new JButton("7");
    		final JButton eight = new JButton("8");
    		final JButton nine = new JButton("9");
    		
    		for(int 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);
    		
    	
    	action perform = new action();
    		zero.addActionListener(perform);
    		one.addActionListener(perform);
    		two.addActionListener(perform);
    		three.addActionListener(perform);
    		four.addActionListener(perform);
    		five.addActionListener(perform);
    		six.addActionListener(perform);
    		seven.addActionListener(perform);
    		eight.addActionListener(perform);
    		nine.addActionListener(perform);
    		password.addActionListener(perform);
    	}
    	private class action implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			
    			
    			
    			//Display according to password input
    			if (isPasswordCorrect(password)) {
    				JOptionPane.showMessageDialog(null, "Password is correct!");			
    			}
    			else {
    				JOptionPane.showMessageDialog(null,"Sorry. Try again.", "Error Message",
    						JOptionPane.ERROR_MESSAGE);
    			}
    			
    		}
    	}
    }
    Last edited by javaStooge; 03-28-2014 at 01:07 AM.

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

    Default Re: ActionListener - JButtons

    Why on earth is this out of bounds?

    Java Code:
    JButton [] buttonArray = new JButton [10];
    
    		for(i = 0 ; i < buttonArray.length; i++){
    			buttonArray[i] = new JButton();}
    To replace:
    Java Code:
    final JButton one = new JButton("1");
    	  final JButton two = new JButton("2");
    	  final JButton three = new JButton("3");
    	  final JButton four = new JButton("4");
    	  final JButton five = new JButton("5");
    	  final JButton six = new JButton("6");
    	  final JButton seven = new JButton("7");
    	  final JButton eight = new JButton("8");
    	  final JButton nine = new JButton("9");
    Last edited by javaStooge; 03-28-2014 at 01:44 AM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: ActionListener - JButtons

    Why on earth is this out of bounds?
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: ActionListener - JButtons

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.
    I'm coming upon my breaking point.

    ActionListener - JButtons-screen-shot-2014-03-27-7.49.58-pm.jpg
    Attached Thumbnails Attached Thumbnails ActionListener - JButtons-screen-shot-2014-03-27-7.49.58-pm.jpg  

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: ActionListener - JButtons

    The error message points to line 60. The posted code only has 4 lines. How do we know those are the same lines of code as caused the error?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,917
    Rep Power
    5

    Default Re: ActionListener - JButtons

    I do not know why you are getting the AIOBE because you are not showing all of your code. Also, you don't need to keep any objects of the buttons around once they are added to the Panel. Since you are using the same actionListener for each one you can get the button object that fired the event as follows inside your actionPerformed method.

    Java Code:
    Object obj = event.getSource();
    if (obj instanceof JButton) {
         JButton button = (JButton)Obj;
         // do something with the button.
    }
    You should check out the Oracle tutorials in my signature. They do a nice job of covering Listeners and event handling.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: ActionListener - JButtons

    Thank you Norm, for pointing out how to read the error message, that will be very helpful in the future! This is what the error is referring to. Why would this not be legit?

    Java Code:
    buttonArray[i].addActionListener(perform);
    Last edited by javaStooge; 03-28-2014 at 02:19 AM.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,917
    Rep Power
    5

    Default Re: ActionListener - JButtons

    I'm not certain why you posted that but there is nothing in that code that I can see which would cause an AIOBE. However, you are declaring the JButton array inside of the ATM constructor. That makes it a local variable to the constructor. It will go out of scope when the constructor finishes and will not be available to any other part of your program.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: ActionListener - JButtons

    Hey Jim, I was creating the array so that I would be able to instantiate each button to perform action in actionListener method. You are saying this is unnecessary? I have looked through some of the pages on different sites, it is very confusing, but I will take a look through this one. I shouldn't have taken this class online, bad idea! :)
    Last edited by javaStooge; 03-28-2014 at 02:21 AM.

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,917
    Rep Power
    5

    Default Re: ActionListener - JButtons

    Like you said/implied, you need to save references so you can get access to them again for some purpose. But in some cases, those objects only provide a means of collecting input. And when the event is fired you can get the value it generated:

    Please note: If the specific actionListener is only to be used by the JButtons and nothing else, then you can just use the ActionEvent method getActionCommand as follows:

    Java Code:
    for (int j = 0; j < 10; j++) {
       JButton b = new JButton(Integer.toString(j));
       b.setActionCommand(b.getText());
       b.setActionListener(perform);
       panel.add(b);
    }
    
    public void actionPerformed(ActionEvent event) {
         System.out.println(event.getText());
    }
    Try a small program with that adding those buttons to a panel and see how it works.

    Regards,
    Jim
    Last edited by jim829; 03-28-2014 at 02:41 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: ActionListener - JButtons

    Thanks for the help on this.
    Last edited by javaStooge; 03-28-2014 at 04:26 PM.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: ActionListener - JButtons

    Why would this not be legit?
    Java Code:
    buttonArray[i].addActionListener(perform);
    The above statement with this:
    Java Code:
            for(i = 0 ; i < buttonArray.length; i++){
    That explains it.
    i is defined OUTSIDE of the for loop so that it will be in scope after the for loop. The value of i will be 10 after the for loop and will cause an AIOOBE when the first statement is executed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. JButtons/ActionListener
    By philip1597 in forum New To Java
    Replies: 2
    Last Post: 12-03-2012, 02:57 AM
  2. jbuttons
    By sarah jain in forum AWT / Swing
    Replies: 3
    Last Post: 03-16-2011, 08:13 AM
  3. Please help!JButtons
    By fourpixel in forum AWT / Swing
    Replies: 6
    Last Post: 09-29-2010, 02:48 PM
  4. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 10:38 AM
  5. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 03:49 AM

Posting Permissions

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