Skip to content

Instantly share code, notes, and snippets.

@vhbui02
Created April 22, 2023 05:05
Show Gist options
  • Save vhbui02/ce860ff7596f224ba9bf8e20fa474d64 to your computer and use it in GitHub Desktop.
Save vhbui02/ce860ff7596f224ba9bf8e20fa474d64 to your computer and use it in GitHub Desktop.
[Java] Some notes worth to take #java

Adding action listener

Nested class implements ActionListener and overrides actionPerformed.

public class Foo{
    Foo(){
        something.addActionListener(new ButtonListener());
    }
    //...
    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            //...
        }
    }
}

Current class implements ActionListener and overrides actionPerformed.

public class Foo implements ActionListener{
    Foo(){
        something.addActionListener(this);
    }
    //...
    public void actionPerformed(ActionEvent e){
        //...
    }
}

Anonymous class.

public class Foo{
    Foo(){
        something.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                //...
            }
        });
    }
}

Source: https://stackoverflow.com/q/5451010/9122512

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment