how to use JTextField to show a negative or positive result using ActionListener on the JTextField itself

Tags: , , , ,



My program is supposed to ask a user to write a number in a JTextField and then show the result if the number is negative or positive in another JTextField that is disabled. All of that must be done without a button.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
public class PositiveOrNegative extends JFrame { 
    JLabel l1, l2 ;
    JTextField t1, t2 ;

    public PositiveOrNegative() 
    {
        FlowLayout layout = new FlowLayout(); 
        this.setLayout(layout); 

        l1 = new JLabel("Enter a number "); 
        t1 = new JTextField(10) ;
        l2 = new JLabel("The number is  ");
        t2 = new JTextField(10) ;

        this.add(l1); 
        this.add(t1);
        this.add(l2);
        this.add(t2);
        t2.enable(false);
        t2.setBackground(Color.PINK);
    }

    public void JavaJTextFieldActionListner() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String n = t1.getText() ;
                int number = Integer.parseInt(n) ;
                if (number >= 0)
                    t2.setText("POSITIVE") ;
                else
                    t2.setText("NEGATIVE") ;
            }
        });
    }

    public static void main(String[] args) {
        PositiveOrNegative p = new PositiveOrNegative(); 
        p.setTitle("AWT SIGN"); 
        p.setBounds(300, 300, 300, 120); 
        p.setVisible(true); 
    } 
} 

Answer

In your example you’ve forget to call the method JavaJTextFieldActionListner. I’ve remove it and move the code into constructor. Also I’ve added handling of a wrong input. Here is the result:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField; 

public class PositiveOrNegative extends JFrame {

    JLabel l1, l2 ;
    JTextField t1, t2 ;

    public PositiveOrNegative() {
        FlowLayout layout = new FlowLayout(); 

        this.setLayout(layout); 

        l1 = new JLabel("Enter a number "); 
        t1 = new JTextField(10);
        l2 = new JLabel("The number is  ");
        t2 = new JTextField(10);

        this.add(l1); 
        this.add(t1);
        this.add(l2);
        this.add(t2);
        t2.setEditable(false);
        t2.setBackground(Color.PINK);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String n = t1.getText();
                try {
                    int number = Integer.parseInt(n);

                    if (number >= 0)
                        t2.setText("POSITIVE");
                    else
                        t2.setText("NEGATIVE");
                } catch (Exception ex) {
                    t2.setText("Not an integer");
                }
            }
        });
    }

    public static void main(String[] args) { 
        PositiveOrNegative p = new PositiveOrNegative(); 
        p.setTitle("AWT SIGN"); 
        p.setBounds(300, 300, 300, 120); 
        p.setVisible(true); 
    } 

}


Source: stackoverflow