Search code examples
javajtextfield

How to check JTextField for an entered number


I’m trying to check the number button that has been pressed in an if statement. I’ve tried to google the question but perhaps I cannot phrase the question well enough.

Here is the code, I read that less code is easier to understand, so I’ve tried to condense my question as much as possible, I hope I haven't condensed it too much.

JButton One = new JButton("1");
One.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        textArea.append("1");
    }
});
if(textArea.equals("1")){
    System.out.println("test");//doesnt print
}

Solution

  • use a variable to store the text:

    String theText;
    theText = textArea.getText();
    

    then do string comparison:

    if(theText.equals("1")) {}
    

    or shorthand:

    if(textArea.getText().equals("1")) {}
    

    Also, the if statement should be inside the actionPerformed method otherwise it has already been executed (resulting in FALSE) by the time the button is clicked. Use textArea.append() if you want to append a new "1" to each previous "1" in the text area every time the button is clicked, otherwise use textArea.setText() to just continually overwrite the previous "1" that was set from any previous button click. A working example can be seen here:

    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JPanel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    
    public class Tester {
       public static void main(String args[]) {
          JButton aButton = new JButton("Button");
    
          JPanel aPanel = new JPanel();
          JTextArea aTextArea = new JTextArea();
          JFrame aFrame = new JFrame();
    
          aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          aFrame.setSize(200, 200);
    
          aPanel.add(aTextArea);
          aPanel.add(aButton);
    
          aFrame.add(aPanel);
          aFrame.setVisible(true);
    
          aButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   aTextArea.setText("1"); 
                   if(aTextArea.getText().equals("1")) {
                      System.out.println("Test Works");
                   }       
          }});
       }
    }
    

    UPDATE:

    If you would like to not only check the contents of the text area when the button is pressed, but whenever text is entered. Kind of the same "idea" to your if-statement being outside of the button action listener. You need to use either a FocusListener or a KeyListener on the text area. A focus listener will execute when you either click in, or click out of the text area. A key listener will execute on various types of key press/release etc. I think what you are looking for is a KeyListener based on your comments. I've provided an example of each, that works with my previous example:

     /* 
       This is probably not the one you want
     */
      aTextArea.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
            }
            public void focusLost(FocusEvent e) {  
               if(aTextArea.getText().equals("1")) {
                  System.out.println("Test Worls");
            }
      }});
    
      /*
        With this KeyListener
        The second someone types "1" in the text area
        It compares the strings, and the test works
      */
      aTextArea.addKeyListener(new KeyListener() {
         public void keyTyped(KeyEvent e) {
         }
    
         public void keyReleased(KeyEvent e) {
            String currentText = aTextArea.getText();
            if(currentText.equals("1")) {
               JOptionPane.showMessageDialog(null, "Test Works");
            }
         }
    
         public void keyPressed(KeyEvent e) {
         }
    
      });
    

    Try inserting the above code in the example I had previously provided. Remember to comment out the button ActionListener and to import the following:

    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;