===== CS 235 - Week 11 Lecture - 2021-11-01 ===== ===== TODAY WE WILL ===== * announcements * using lambdas to specify listeners more concisely * maybe more on Threads and Swing? * intro a few more GUI components and listeners * prep for next class * Reading? I think the following: * Chapter 10, Section 10.4.3 - specifying listeners more concisely, pp. 604-605 * Chapter 11 - some additional components and associated listeners * ArrayList reminder: Section 5.3 - Generic Array Lists - pp. 248-256 * Should start start working on Homework 7; due Friday, 11:59 pm * reminder: if you still need to, arrange to meet with your advisor this week for Spring 2022 advising! ===== Specifying listeners more concisely - Section 10.4.3, pp. 604-605 ===== * remember how, the last few weeks, we've been able to specify a Runnable instance with the help of a lambda expression (instead of creating a class implementing the Runnable interface to do so) because Runnable is a so-called functional interface; Looks like ActionListener is ALSO a functional interface! something like this is also OK for creating an ActionListener instance: ActionListener anActionListener = event -> { desired_action; desired_action; }; see a version of this in Button2Test.java ===== another useful GUI component: ===== * Swing version of a checkbox: JCheckBox * checkboxes: you want to visualize if some choice is selected or not, and have a set where none might be, some might be, all might be, checked; * the Java JCheckBox can actually support more than one listener! ActionListener (to make the JCheckBox sensitive to ActionEvents) ItemListener (to make the JCheckBox sensitive to to ItemEvents) * some useful JCheckBox methods: * one of its constructors expects the desired text that "accompanies" the checkbox * method isSelected expects nothing and returns whether the calling checkbox is currently checked, or selected * method setSelected expected a boolean and returns nothing and MAKES the calling checkbox be checked, or selected * a difference between ItemListener and ActionListener for JCheckBox objects: * a JCheckBox ItemListener can be fired by the setSelected method -- an ActionListener is fired when the calling checkbox's state is altered/changed (this can matter at the beginning of setup...) ===== JRadioButton class ===== * idea you SHOULD use with these, to meet user expectations: logically grouped such that selecting ONE radio button in the logical group causes any other that is currently selected to be UN-selected You construct a ButtonGroup object for each logical group of radio buttons, then you add the desired JRadioButton objects to that ButtonGroup (BUT!!!!! adding a JRadioButton to a ButtonGroup is separate from adding it to a JPanel or other container so you SEEEEE it!) * push of radio button can be an ActionEvent quite nicely; * yes, they have an isSelected method so you can ask if a particular radio is currently selected * weird quirk of ButtonGroup: * has a getSelection, yay! * it returns a not-very-useful ButtonModel reference, booo! * IF you explicitly set each JRadioButton to have an action command using setActionCommand, though, THEN the ButtonGroup's getSelection().getActionCommand() WILL give the label for the selected JRadioButton, at least!