By Alvin Alexander. Last updated: June 4, 2016
Here's another one of those postings that are just here to help my bad memory. I currently travel a fair amount, work on different systems and projects, and can't remember the Java syntax for adding a Java ActionListener to a JButton, so here's a little sample code to help me remember:
// java jbutton actionlistener example myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // handle the jbutton event here } });
Of course this assumes that I already have a JButton named myButton
, and I want to add an ActionListener to that button, and implement an actionPerformed
method. There are other ways to implement a Java JButton listener, but this is my preferred approach.