Action Listener (JFrame) will not change the instance variable

51 views Asked by At

public class YourAge extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField fNameTextField;
    private JTextField initialTextField;
    private JTextField lNameTextFIeld;
    private JTextField dob;
    private int age;

    /**
     * Launch the application.
     */
    public void setAge(int i) {
        age = i;
    }
    
    public static void main(String[] args) { 
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    YourAge frame = new YourAge();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public YourAge() {

dob.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e)
            {
                
                    String sdob = dob.getSelectedText();
                    int months = Integer.parseInt(sdob.substring(0,2));
                    int days = Integer.parseInt(sdob.substring(3,5));
                    int years = Integer.parseInt(sdob.substring(6,10));
                    
                    age = (int) ((2024 - years) + (0.0833*(1 - months)) + 0.0323*(27 - days));
                    
                    
                    
            }
            

        });

}
}

The Action Listener is in the yourAge constructor, which extends the Jframe class. In the Jframe class, there is an instance variable "age" that I want this code to change, but for some reason it is not working.

I made a "setAge()" mutator method outside the Action to implement this, but it is only working outside the Action Listener, not inside.

0

There are 0 answers