With JFC, to make the buttons took and behave like radio buttons, you have to use JRadioButton. Let's continue our one-topping example, using JRadioButton in a button group to allow only one checkbox to be selected.
for a_checkbox in checkboxes: panel.remove(a_checkbox)
group = ButtonGroup()
from javax.swing import JRadioButton rb = JRadioButton #rb is JRadioButton class.
#Remember classes are first class objects in Python. pepperoni = rb("pepperoni")
for a_radiobutton in radiobuttons: group.add(a_radiobutton) panel.add(a_radiobutton)
pepperoni.selected = 1 frame.pack()
Try these exercises:
• Change the event handler to work with JRadioButton, and then look up Abstract Button, the superclass of JButton, JCheckBox, and JRadioButton. You might also try adding picture icons to represent the different toppings.
• With the technique we used to replace checkboxes with radio buttons, replace radio buttons with toggle buttons (javax. swing. JToggleButton). Can you think of ways to switch between all three? Hift: JToggleButton has the same properties that JCheckBox and JRadioButton have. Remember that JButton gets much of its functionality from AbstractButton, so all of its properties—icon, texe mnemonic s, and so fotta—work for these classes as well. Ia you want to add icons or change text, yvu do it in the same way.
Was this article helpful?