how to prompt a pop up message right after user input in jtextfield <p>I am new in Java and now trying to develop a simple program to support my research findings. In this program, I'll be displaying randomized images and users were to enter single integer in jtextfield provided. The users are required to count and answer the number of animals displayed. Since I want to develop the program as simple as possible, I imagine my tool will be straight forward. </p> <p><strong>Question 1:</strong> There will be a JTextField provided for user to input their answer. Is there any coding that could restrict user input right after a single integer(between 1-6) has been keyed in the box? As I need my program to be straight forward and easy to understand, I do not want user to click on any button so as to lead them to next image. May I know is there any way to restrict user input and lead the program to next image right after user enter a number without clicking any button? <a href="https://i.stack.imgur.com/erLvu.jpg" rel="nofollow">Question 1</a></p> <p><strong>Question 2:</strong> Right after the user key in the single integer answer, I would like to prompt a simple popup that includes a praise to user such as "Well done". Since the JOptionPane I'll be using will contain two buttons: "OK" and "cancel", may I know how to make sure the program leads to next randomized image after user click on "OK" button. <a href="https://i.stack.imgur.com/cUMMl.jpg" rel="nofollow">Question 2</a></p> <p><strong>Question 3:</strong> Is there any ways to keep the input cursor for typing just in the JTextField provided? This is because I want the program as simple as possible that user do not need to waste their time moving their mouse and click at the jtextfield to start entering their answer. I want my program to work like: everytime user enter a single integer, a pop up message appear, user click "OK" and lead to next randomized image. At the next image, user just have to enter the answer without moving the cursor to click at the jtextfield(Example:By referring to image question 2, after user click "OK", how to make the program leads to next randomized image as image question 1). May I know is there any way to develop the program in such way?</p> <p>This is my code.</p> <pre><code>package Project; import java.awt.*; import java.awt.event.*; import java.sql.*; public class Task implements KeyListener, ActionListener { static JFrame frame = new JFrame("FYP"); JPanel content = new JPanel(); JPanel content2 = new JPanel(); JLabel lbl2 = new JLabel(); JLabel labelUsername = new JLabel(""); JTextField textField = new JTextField(); long StartA=System.currentTimeMillis(); //connectionMSAccess Connection c; Statement s; ResultSet r; int sum=0; int Error=0; int total_test=0; static String inputID; static int index = 0; String[] imgFileHP = {"1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg","8.jpg","9.jpg","10.jpg", "11.jpg","12.jpg","13.jpg","14.jpg","15.jpg","16.jpg","17.jpg","18.jpg","19.jpg","20.jpg"}; String[] imgNo = {"5","5","4","6","4","6","3","4","5","3","5","4","3","5","4","5","6","4","4","6"}; List &lt;String&gt; pictureFileArray = Arrays.asList(imgFileHP); int randomNo; public Task(String inputID){ frame.setSize(2200,2500);; frame.setLocationRelativeTo(null);; frame.setVisible(true);; frame.getContentPane().setLayout(new BorderLayout(0,0)); Collections.shuffle(pictureFileArray); Task(inputID); } public void Task(String inputID){ JPanel panel = new JPanel(); JLabel labelUsername = new JLabel(""); frame.getContentPane().add(panel, BorderLayout.CENTER); panel.setBackground(Color.WHITE); Image im = new ImageIcon(Task.class.getResource("/images/" + imgFileHP[index])).getImage(); ImageIcon iconLogo = new ImageIcon(im); labelUsername.setIcon(iconLogo); panel.add(labelUsername); textField.setText(""); textField.setColumns(10); textField.setVisible(true); textField.addKeyListener(this); panel.add(labelUsername); panel.add(textField); frame.setVisible(true); int keyb = JOptionPane.showOptionDialog(null, "Well done!", "Message", JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE,null,null,null); if(keyb == JOptionPane.OK_OPTION) { frame.dispose(); } } public void actionPerformaed(ActionEvent ae){ if(!textField.getText().equals("")){ total_test += 1; if(booleanisNumeric(textField.getText())){ if(Integer.valueOf(imgNo[randomNo])==Integer.valueOf(textField.getText())){ System.out.println("Correct"); sum+=1; }else{ System.out.println("Invalid input"); Error+=1; } refreshFrame(); }else{ System.out.println("Null Input"); } } } public void refreshFrame(){ if(total_test&gt;=20){ System.out.println("Correct:" +sum+" Incorrect: "+Error); frame.dispose(); }else{ frame.getContentPane().removeAll(); //getContentPane().removeAll(); Task(inputID); } } public static void main(String args[]){ Task a = new Task(inputID); } @Override public void keyPressed(KeyEvent e){ } @Override public void keyTyped(KeyEvent e){ } public static boolean booleanisNumeric(String str){ try{ Integer.valueOf(str); } catch(NumberFormatException nfe){ return false; } return true; } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } } </code></pre>