import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /** * A GUI application that demos several additional * components * * @author Sharon Tuttle * @version 2015-10-26 */ public class ComponentPlay { /** * creates a ComponentPlayFrame * * @param args not used here */ public static void main(String args[]) { EventQueue.invokeLater( new Runnable() { public void run() { ComponentPlayFrame mainFrame = new ComponentPlayFrame(); mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mainFrame.setVisible(true); } }); } } /** * A frame with a panel that is painted/drawn upon */ class ComponentPlayFrame extends JFrame { // data fields private final static int DEFAULT_WIDTH = 800; private final static int DEFAULT_HEIGHT = 450; /** * constructs a ComponentPlayFrame instance */ public ComponentPlayFrame() { this.setTitle("ComponentPlay"); this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add ComponentPlay panel to frame ComponentPlayPanel panel = new ComponentPlayPanel(); this.add(panel); } } /** * a JPanel subclass with several component examples */ class ComponentPlayPanel extends JPanel { // data fields private final static Font DISPLAY_FONT = new Font("Dialog", Font.PLAIN, 20); private JCheckBox[] tacoToppings; private static final String[] TOPPINGS = {"Sour cream", "Cheese", "Cilantro", "Lettuce", "Salsa", "Pico de Gallo", "Guacamole", "Pepperoni", "Onion", "Greek yogurt"}; private JTextField tacoToppingsSummary; private JRadioButton[] tacoProteins; private static final String[] PROTEINS = {"Lean beef", "Steak", "Chicken", "Greasy Pork", "Pepperoni", "Tofu", "Nothing"}; private JTextField proteinChoice; /** * construct a ComponentPlayPanel instance * */ public ComponentPlayPanel() { this.setLayout(new GridLayout(1, 4)); //----- // create a panel for selecting 0 or more taco toppings //----- JPanel ckBoxPanel = new JPanel(); ckBoxPanel.setLayout(new BorderLayout()); ckBoxPanel.setBorder(new EtchedBorder()); Box tacoToppingsBox = Box.createVerticalBox(); // create a checkbox for each taco topping tacoToppings = new JCheckBox[TOPPINGS.length]; for (int i=0; i < tacoToppings.length; i++) { tacoToppings[i] = new JCheckBox(TOPPINGS[i]); tacoToppings[i].setFont(DISPLAY_FONT); tacoToppings[i].addItemListener( new TacoToppingChoice()); tacoToppingsBox.add(tacoToppings[i]); } ckBoxPanel.add(tacoToppingsBox, BorderLayout.CENTER); // summary of taco toppings should appear here tacoToppingsSummary = new JTextField(40); tacoToppingsSummary.setEditable(false); ckBoxPanel.add(tacoToppingsSummary, BorderLayout.PAGE_END); this.add(ckBoxPanel); //----- // create a panel for selecting exactly one protein //----- JPanel proteinPanel = new JPanel(); proteinPanel.setLayout(new BorderLayout()); proteinPanel.setBorder(new EtchedBorder()); Box proteinBox = Box.createVerticalBox(); // need to logically collect radio buttons in a ButtonGroup! ButtonGroup proteinGroup = new ButtonGroup(); // create a radio button for each taco protein tacoProteins = new JRadioButton[PROTEINS.length]; //boolean selected = true; for (int i=0; i< PROTEINS.length; i++) { tacoProteins[i] = new JRadioButton(PROTEINS[i]); // NOTICE we add each radio button to a logical ButtonGroup -- // so it can make sure ONLY ONE taco protein is // selected at any one time proteinGroup.add(tacoProteins[i]); // (we need to set a radio button's action command to be able to grab // it from an ActionEvent) tacoProteins[i].setActionCommand(PROTEINS[i]); proteinBox.add(tacoProteins[i]); } proteinPanel.add(proteinBox, BorderLayout.CENTER); this.add(proteinPanel); } /** * I'd like to re-display (and update) the taco * toppings summary every time the user * checks OR unchecks a taco topping */ private class TacoToppingChoice implements ItemListener { /** * re-populate the taco toppings summary * textfield every time the state of a * taco topping changes * * @param event a taco topping check or uncheck */ public void itemStateChanged(ItemEvent event) { String tacoToppingsResponse = "customer wants: "; for (int i=0; i<TOPPINGS.length; i++) { if (tacoToppings[i].isSelected()) { tacoToppingsResponse += tacoToppings[i].getText() + " "; } } tacoToppingsSummary.setText(tacoToppingsResponse); } } }