Search code examples
javaactionlistenerjtextfielddecrement

Trouble with ActionListener and an IF statement


I am currently having two problems both at which i have been trying to solve for the past 3 hours.

  1. I cant get the input--; to decrement if input is not == to 0

  2. I cant get the JTextField input to update the the value i assign to it once the program is running. Ill type 22 in the running program click start and it will go to "test99".enter image description here Pictures is an example of how i entered the value 66 then i pressed start and test99 came up instead of test66

I hope I was able to explain my problem to an extent you will be able to understand. I have read a lot of documentation about action listeners but currently the idea wont click for me. Any constructive criticism is welcome.

I also simplified my problem down as best as I could.

package test;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JButton;

public class test {

private JFrame frame;
private JButton btnStart;

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

/**
 * Create the application.
 */
public test() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */

private void initialize() {
    frame = new JFrame();
    JLabel Output = new JLabel("Time left: ");
    Output.setHorizontalAlignment(SwingConstants.CENTER);
    frame.getContentPane().add(Output, BorderLayout.CENTER);
    JTextField Input = new JTextField();
    btnStart = new JButton("start");

    Input.setText("99");
    int input = (int) (Double.parseDouble(Input.getText()));

    Input.setHorizontalAlignment(SwingConstants.CENTER);
    frame.getContentPane().add(Input, BorderLayout.SOUTH);
    Input.setColumns(10);
    frame.getContentPane().add(btnStart, BorderLayout.NORTH);
    frame.setBounds(100, 100, 300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Input.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {

          }
        });


    Timer t = new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Output.setText("test" + input);

            if (input == 0) {
                ((Timer) e.getSource()).stop();
            }
            input--;
        }

    });

    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            t.start();
        }
    });

}

}


Solution

  • I recommand to declare your input variables not in your function, but in your class. Otherwise you run into scope problems. Example:

    public class test {
    
      private JFrame frame;
      private JButton btnStart;
      private int input;
      private JTextField Input;
    
      //...
    
    }
    

    Should fix the issue :)

    I am not enterily sure about the second issue but if you want to count down from the entered value, you have to update your action listener:

    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            input = (int) (Double.parseDouble(Input.getText()));
            t.start();
        }
    });