Search code examples
javaswingjframeborder-layout

Buttons do not seem to respond when pressed


Again, hello everyone. I'm having more trouble with my encryption code - The button presses don't seem to be registering. The code to detect the button presses is below.

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == enc && !in.getText().equalsIgnoreCase("")) {
        out.setText(EncDec.e(in.getText(), 5));
        System.out.println("Button pressed (Encrypt)");
    }
    else if(e.getSource() == dec && !in.getText().equalsIgnoreCase("")) {
        out.setText(EncDec.d(in.getText()));
        System.out.println("Button pressed (Decrypt)");
    }
}

This question is indeed related to this one, and thanks to Pshemo for letting me know :)

I have the System.out.println() to tell me when a button is pressed, and nothing is printed to console when I press either of the buttons named below. If you need more information, just ask and I will be happy to add it.

I tried adding addActionListener() to the code, but I don't know how to use it...


Solution

  • I assume that this question is related to this one.

    All you need to do is to add object of your class (that implements ActionListener) with addActionListener to your Buttons, like:

    EncDecExample decExample = new EncDecExample();
    enc.addActionListener(decExample);
    dec.addActionListener(decExample);
    enc.setVisible(true);
    dec.setVisible(true);