Results 1 to 13 of 13
Thread: ActionListener - JButtons
- 03-28-2014, 12:41 AM #1
Senior Member
- Join Date
- Jan 2014
- Posts
- 106
- Rep Power
- 0
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); } } } }
- 03-28-2014, 12:44 AM #2
Senior Member
- Join Date
- Jan 2014
- Posts
- 106
- Rep Power
- 0
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);
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.
- 03-28-2014, 01:42 AM #3
Senior Member
- Join Date
- Jan 2014
- Posts
- 106
- Rep Power
- 0
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();}
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.
- 03-28-2014, 01:48 AM #4
Re: ActionListener - JButtons
Why on earth is this out of bounds?If you don't understand my response, don't ignore it, ask a question.
- 03-28-2014, 01:50 AM #5
Senior Member
- Join Date
- Jan 2014
- Posts
- 106
- Rep Power
- 0
- 03-28-2014, 02:01 AM #6
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.
- 03-28-2014, 02:05 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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. }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-28-2014, 02:08 AM #8
Senior Member
- Join Date
- Jan 2014
- Posts
- 106
- Rep Power
- 0
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.
- 03-28-2014, 02:13 AM #9
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-28-2014, 02:15 AM #10
Senior Member
- Join Date
- Jan 2014
- Posts
- 106
- Rep Power
- 0
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.
- 03-28-2014, 02:27 AM #11
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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()); }
Regards,
JimLast 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
- 03-28-2014, 04:17 PM #12
Senior Member
- Join Date
- Jan 2014
- Posts
- 106
- Rep Power
- 0
Re: ActionListener - JButtons
Thanks for the help on this.
Last edited by javaStooge; 03-28-2014 at 04:26 PM.
- 03-28-2014, 04:25 PM #13
Re: ActionListener - JButtons
Why would this not be legit?Java Code:buttonArray[i].addActionListener(perform);
Java Code:for(i = 0 ; i < buttonArray.length; i++){
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
-
JButtons/ActionListener
By philip1597 in forum New To JavaReplies: 2Last Post: 12-03-2012, 02:57 AM -
jbuttons
By sarah jain in forum AWT / SwingReplies: 3Last Post: 03-16-2011, 08:13 AM -
Please help!JButtons
By fourpixel in forum AWT / SwingReplies: 6Last Post: 09-29-2010, 02:48 PM -
Help with JButtons...
By ashton in forum New To JavaReplies: 8Last Post: 01-26-2009, 10:38 AM -
JButtons
By jadaleus in forum Advanced JavaReplies: 4Last Post: 10-17-2008, 03:49 AM
Bookmarks