17.9 Lambda Event Handlers

In Section 12.11, you learned how to implement an event handler using an anonymous inner class. Some event-listener interfaces—such as ActionListener and ItemListener—are functional interfaces. For such interfaces, you can implement event handlers with lambdas. For example, the following statement from Fig. 12.21:

imagesJComboBox.addItemListener(   new ItemListener() // anonymous inner class   {      // handle JComboBox event      @Override      public void itemStateChanged(ItemEvent event)      {         // determine whether item selected         if (event.getStateChange() == ItemEvent.SELECTED)            label.setIcon(icons[               imagesJComboBox.getSelectedIndex()]);      } ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.