March R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Buttons: Radio and Check BoxButtons: Radio and Check Box Lab 20Lab 20
March R. Smith - University of St Thomas - Minnesota Radio Buttons Based on old car radio tuner buttonsBased on old car radio tuner buttons You select one button in a setYou select one button in a set –If another was selected, it turns off –Only one button selected at a time You create the buttons, each with a titleYou create the buttons, each with a title You display themYou display them You create a ‘button group’You create a ‘button group’ You add the buttons to the groupYou add the buttons to the group
March R. Smith - University of St Thomas - Minnesota Check Boxes Just “on” or “off”Just “on” or “off” You create one, give it a titleYou create one, give it a title Optionally you ‘select’ it onOptionally you ‘select’ it on
March R. Smith - University of St Thomas - Minnesota Reading Radios and Check Boxes Two waysTwo ways –Easy way: use isSelected() method –Hard way: create an event handler Why use the easy way?Why use the easy way? –If you have a “Run” or “Calculate” button anyway –If you don’t care when the button value changes, just its final value Why use the hard way?Why use the hard way? –If you don’t have a “Calculate” button
March R. Smith - University of St Thomas - Minnesota Handling Multiple Buttons Option 1: create separate listenersOption 1: create separate listeners –This is a lot of coding, but it works Option 2: check the ActionEventOption 2: check the ActionEvent –The listener gets an ActionEvent object as a parameter –event.getSource() returns a reference to the pushed button –Write an if/else if/else structure to check the alternatives
March R. Smith - University of St Thomas - Minnesota Lab 20 Lab 19 plus two sets of buttonsLab 19 plus two sets of buttons Radio buttonRadio button –Select type of wood: oak, mahogany, rosewood –If it’s rosewood, add 20% to the cost, otherwise do nothing Check box: glass front or notCheck box: glass front or not –If checked, add $46.50 per bookcase Use “Layout Manager” featuresUse “Layout Manager” features –Make sure that related pieces stay together as well as possible –Hint: use “panels” inside either a grid or a border layout
March R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.