Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't want to "freeze" your program. Instead you want to code it so that the listener that responds to the press of the OK button, will only do its action if the number buttons have been pressed previously. This will require that you give the class a variable that holds the results of the last xxx button presses (an array or list could work for this or even a simple String), and then in your ok button's listener, have an if block that checks this variable and if OK, performs its magic.</p> <p>For example you could have String called pinInput and have it = "" or the empty String (or better, a StringBuilder -- but this is not totally necessary for your simple program). In each number button listener, you concatenate the String representing the number pressed onto the pinInput String. In the OK button's listener you check to see if the pinInput's length is more than the minimum required characters long, and if so, take the last x characters from the String and test if they match the PIN.</p> <p><strong>Edit:</strong><br> You state:</p> <blockquote> <p>The ATM class will be the "main" of the program, and it will call an methode readInt() of class Keypad. it should be here that i get the number that the user introduced.</p> </blockquote> <p>You can allow the main class to add a listener to any other class if need be, so that it gets notified if a button is pressed.</p> <p>No matter how you look at this, you'll need to use listeners so that your program responds to events (is "event-driven"). That's how Java Swing GUI's work.</p> <p><strong>Edit 2</strong><br> For example here's code where the JFrame holds a JPanel that has JButtons on it, and the JFrame only responds if the buttons on the JPanel are pushed in a certain order -- A, then B, then OK:</p> <pre><code>import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class AnExample { private static void createAndShowGui() { AnExample mainPanel = new AnExample(); JFrame frame = new AnExMain(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class AnExMain extends JFrame { private JTextField field = new JTextField( "Press A, then B, then OK, in that order", 20); private Control control = new Control(this); public AnExMain() { add(field, BorderLayout.PAGE_START); add(new InnerPanel(control), BorderLayout.CENTER); } public void fieldSetText(String text) { field.setText(text); } } class Control { private AnExMain anExMain; private String data = ""; public Control(AnExMain anExMain) { this.anExMain = anExMain; } public void btnPressed(String actionCommand) { anExMain.fieldSetText(""); if (actionCommand.equals("OK")) { if (data.equals("AB")) { anExMain.fieldSetText("Buttons pressed in proper order!"); } data = ""; } else { data += actionCommand; if (data.length() &gt; 2) { data = data.substring(data.length() - 2); } } } } class InnerPanel extends JPanel { public InnerPanel(final Control control) { JButton btnA = new JButton("A"); JButton btnB = new JButton("B"); JButton btnOK = new JButton("OK"); add(btnA); add(btnB); add(btnOK); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { control.btnPressed(e.getActionCommand()); } }; btnA.addActionListener(listener); btnB.addActionListener(listener); btnOK.addActionListener(listener); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload