2

I was reading about Swing Timers and the example really looks nothing like I was trying to do so I found it logically confusing to apply it to my program. I'm starting to think I don't even need a timer for this.

Here is what I am trying to do:

I am making a JFrame program that has the user enter a credit card number into a JTextField. Before they do this there is a JLabel that says "Please enter your number into the text field", then once they enter this into the field and hit enter, depending on whether my code determines that the card number is valid or not valid, the JLabel will change to "Invalid" or "Thank you, processing."

However, I have unsuccessfully found a way to make it change text based, it just seems to stay with whatever text I initially give it.

So could someone please look at my code and change it to do what I am asking? That would be excellent. You guys have been helpful in the past.

public class CreditGraphics {

    public String cardNum;
    public JFrame frame;
    public JPanel panel;
    public JLabel label;
    public JTextField text;
    public Timer timer;

    public CreditGraphics() {
        frame = new JFrame("HI");
        panel = new JPanel();
        label = new JLabel();    
        text = new JTextField(16);    
        panel.add(label);
        panel.add(text);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setPreferredSize(new Dimension(500, 500));
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true); 
        label.setText("Hi");          
        label.setText("Hello");
        text.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cardNum = text.getText();
                timer = new Timer(2000,this);
                timer.setInitialDelay(1000);
                timer.start();
            }                
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new CreditGraphics();

               }
        });
    }

    public void checkCard(){       

    }        
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
SoloSpirit
  • 87
  • 12

1 Answers1

3

You have some major problems with your Timer's ActionListener as it is adding the anonymous inner ActionListener object, the this in the Timer's constructor, to itself. So it will call the very same actionPerformed inside the Timer that is called by the JButton that starts the Timer -- quite confusing. If your program were to need a Timer, you would do well to make sure to give it its own ActionListener, and not the same ActionListener that you add to your JButton as you're doing now.

Most importantly, do you even need a Swing Timer? I don't think so since you don't appear to be wanting an action repeatedly happening every xxx milliseconds, or an action that occurs once after xxx milliseconds, and since all you want to do is change the text. I suggest that instead you simply change your JLabel's text in the anonymous inner ActionListener class, and just leave it at that. If your requirements are different, then you will want to clarify and expand on your question.

So in semi-pseudocode, something like:

public void actionPerformed(ActionEvent e) {
  String userText = text.getText();
  if (testIfTextValid(userText)) { // some method to test if input OK
    label.setText(INPUT_VALID);  // String constant for JLabel to display

    // here pass the userText to other parts of your code that needs to use it

  } else {
    label.setText(INPUT_INVALID);
  }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373