 |
|

|
 |

12/01/09 - Checkboxes, Radiobuttons and Lists
|
 |
 |
 |
  | JRadioButtons for mutually-exclusive choices
|
 |
 |
 |
 |
  | import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class RadButtonDemo implements ActionListener { private JTextField txtChoice; public static void main(String [] args) { RadButtonDemo rbd = new RadButtonDemo(); } public RadButtonDemo() { final int WIDTH = 500; final int HEIGHT = 200; JFrame window = new JFrame("JRadioButton Demo"); window.setSize(WIDTH,HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set close operation Font font = new Font("Serif",Font.BOLD+Font.ITALIC,30); JPanel panBack = new JPanel(); panBack.setLayout(new FlowLayout()); panBack.setBackground(Color.yellow); JRadioButton radSmall = new JRadioButton("Small"); radSmall.setFont(font); radSmall.setBackground(Color.yellow); radSmall.addActionListener(this);
JRadioButton radMedium = new JRadioButton("Medium"); radMedium.setFont(font); radMedium.setBackground(Color.yellow); radMedium.addActionListener(this);
JRadioButton radLarge = new JRadioButton("Large"); radLarge.setBackground(Color.yellow); radLarge.setFont(font); radLarge.addActionListener(this);
// add radio buttons to group to make them mutually exclusive ButtonGroup group = new ButtonGroup(); group.add(radSmall); group.add(radMedium); group.add(radLarge);
// add radio buttons to panel panBack.add(radSmall); panBack.add(radMedium); panBack.add(radLarge);
// add textfield to bottom txtChoice = new JTextField("No size selected"); txtChoice.setFont(font); txtChoice.setEditable(false); window.setLayout(new BorderLayout()); window.add(BorderLayout.SOUTH,txtChoice);
// add panel to window window.add(BorderLayout.CENTER,panBack); window.setVisible(true); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (s.equals("Small")) { txtChoice.setText("Small selected!"); } else if (s.equals("Medium")) { txtChoice.setText("Medium selected!"); } else if (s.equals("Large")) { txtChoice.setText("Large selected!"); } } }
|
 |
 |
 |
 |
  | JCheckBoxes for option combinations
|
 |
 |
 |
 |
  | import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class CheckBoxDemo implements ActionListener { private JTextField txtChoice; private JCheckBox chkOlives = new JCheckBox("Olives"); private JCheckBox chkMushrooms = new JCheckBox("Mushrooms"); private JCheckBox chkAnchovies = new JCheckBox("Anchovies"); public static void main(String [] args) { CheckBoxDemo cbd = new CheckBoxDemo(); } public CheckBoxDemo() { final int WIDTH = 500; final int HEIGHT = 200; JFrame window = new JFrame("JCheckBox Demo"); window.setSize(WIDTH,HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set close operation Font font = new Font("Serif",Font.BOLD+Font.ITALIC,30); JPanel panBack = new JPanel(); panBack.setLayout(new FlowLayout()); panBack.setBackground(Color.yellow); chkOlives.setFont(font); chkOlives.setBackground(Color.yellow); chkOlives.addActionListener(this);
chkMushrooms.setFont(font); chkMushrooms.setBackground(Color.yellow); chkMushrooms.addActionListener(this);
chkAnchovies.setBackground(Color.yellow); chkAnchovies.setFont(font); chkAnchovies.addActionListener(this);
// add radio buttons to panel panBack.add(chkOlives); panBack.add(chkMushrooms); panBack.add(chkAnchovies);
// add textfield to bottom txtChoice = new JTextField("No extras selected"); txtChoice.setFont(font); txtChoice.setEditable(false); window.setLayout(new BorderLayout()); window.add(BorderLayout.SOUTH,txtChoice);
// add panel to window window.add(BorderLayout.CENTER,panBack); window.setVisible(true); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (s.equals("Olives")) { if (chkOlives.isSelected()) { txtChoice.setText("Olives selected!"); } else { txtChoice.setText("Olives deselected!"); } } else if (s.equals("Mushrooms")) { if (chkMushrooms.isSelected()) { txtChoice.setText("Mushrooms selected!"); } else { txtChoice.setText("Mushrooms deselected!"); } } else if (s.equals("Anchovies")) { if (chkAnchovies.isSelected()) { txtChoice.setText("Anchovies selected!"); } else { txtChoice.setText("Anchovies deselected!"); } } } }
|
 |
 |
 |
 |
  | import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;
public class JListDemo0 extends MouseInputAdapter { private String[] listContents = {"apple","banana","cherry","date","grapes","kiwi"}; private JList lstMain = new JList(listContents); private JScrollPane scrlMain = new JScrollPane(lstMain); private JTextField txtChoice = new JTextField(20); public static void main(String [] args) { JListDemo0 list = new JListDemo0(); } public JListDemo0() { final int WIDTH = 400; final int HEIGHT = 200; JFrame window = new JFrame("JList Demo 0"); window.setSize(WIDTH,HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set close operation Font font = new Font("Serif",Font.BOLD+Font.ITALIC,24); // set up GUI JPanel panMain = new JPanel(); panMain.setLayout(new BorderLayout()); panMain.add(txtChoice,BorderLayout.SOUTH); panMain.add(scrlMain,BorderLayout.CENTER); lstMain.setFont(font); lstMain.addMouseListener(this); txtChoice.setFont(font); txtChoice.setEditable(false); // add main panel to frame window.add(panMain); window.setVisible(true); } public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 1) { txtChoice.setText("Single clicked on " + lstMain.getSelectedValue()); } else if (e.getClickCount() == 2) { txtChoice.setText("Double clicked on " + lstMain.getSelectedValue()); } else if (e.getClickCount() == 3) { txtChoice.setText("Triple clicked on " + lstMain.getSelectedValue()); } } }
|
 |
 |
 |
 |
  | import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class JListDemo1 implements ActionListener { private DefaultListModel leftContents = new DefaultListModel(); private JList lstLeft = new JList(leftContents); private JScrollPane scrlLeft = new JScrollPane(lstLeft); private DefaultListModel rightContents = new DefaultListModel(); private JList lstRight = new JList(rightContents); private JScrollPane scrlRight = new JScrollPane(lstRight); public static void main(String [] args) { JListDemo1 list = new JListDemo1(); } public JListDemo1() { final int WIDTH = 600; final int HEIGHT = 210; JFrame window = new JFrame("JList Demo 1"); window.setSize(WIDTH,HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set close operation JPanel panCenter = new JPanel(); panCenter.setLayout(new GridLayout(2,1));
Font font = new Font("Serif",Font.BOLD,24); JButton btnRight = new JButton(">>"); btnRight.setFont(font); btnRight.addActionListener(this);
JButton btnLeft = new JButton("<<"); btnLeft.setFont(font); btnLeft.addActionListener(this);
panCenter.add(btnRight);//panCenter.add(panTop); panCenter.add(btnLeft);//panCenter.add(panBot); lstLeft.setFont(font); lstRight.setFont(font);
// add items to left list leftContents.addElement("apple"); leftContents.addElement("peach"); leftContents.addElement("banana"); leftContents.addElement("plum"); leftContents.addElement("strawberry"); leftContents.addElement("mango"); leftContents.addElement("cranberry"); leftContents.addElement("pumpkin"); leftContents.addElement("grape"); leftContents.addElement("blueberry"); leftContents.addElement("raspberry"); leftContents.addElement("kiwi"); window.setLayout(new GridLayout(1,3)); window.add(scrlLeft); window.add(panCenter); window.add(scrlRight); window.setVisible(true); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (s.equals(">>")) { Object selection = lstLeft.getSelectedValue(); rightContents.addElement(selection); leftContents.removeElement(selection); } else if (s.equals("<<")) { Object selection = lstRight.getSelectedValue(); leftContents.addElement(selection); rightContents.removeElement(selection); } } }
|
 |
 |
 |
 |
  | import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class JListDemo2 implements ActionListener { private DefaultListModel leftContents = new DefaultListModel(); private JList lstLeft; private JButton btnAdd; private JButton btnRemove; private JTextField txtEntry; public static void main(String [] args) { JListDemo2 list = new JListDemo2(); } public JListDemo2() { final int WIDTH = 600; final int HEIGHT = 300; JFrame window = new JFrame("JList Demo 2"); window.setSize(WIDTH,HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set close operation Font font = new Font("Serif",Font.BOLD,24); txtEntry = new JTextField(); txtEntry.setFont(font);
lstLeft = new JList(leftContents); lstLeft.setFont(font); JScrollPane scrlLeft = new JScrollPane(lstLeft);
JPanel panLeft = new JPanel(); panLeft.setLayout(new BorderLayout()); panLeft.add(BorderLayout.NORTH,txtEntry); panLeft.add(BorderLayout.CENTER,scrlLeft); btnAdd = new JButton("Add Text to List"); btnAdd.setFont(font); btnAdd.addActionListener(this);
btnRemove = new JButton("Remove Selected Item"); btnRemove.setFont(font); btnRemove.addActionListener(this); JPanel panRight = new JPanel(); panRight.setLayout(new GridLayout(2,1)); panRight.add(btnAdd); panRight.add(btnRemove); JPanel panMain = new JPanel(); panMain.setLayout(new GridLayout(1,2)); panMain.add(panLeft); panMain.add(panRight); // add main panel to frame window.add(panMain); window.setVisible(true); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (s.equals("Add Text to List")) { s = txtEntry.getText(); if (!s.equals("")) { leftContents.addElement(s); txtEntry.setText(""); } } else if (s.equals("Remove Selected Item")) { int i = lstLeft.getSelectedIndex(); if (i != -1) { // -1 indicates nothing is selected leftContents.removeElementAt(i); } } } }
|
 |
 |
 |
 |
  | JComboBox Demo (Drop-down list)
|
 |
 |
 |
 |
  | import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class JComboDemo implements ActionListener { private JComboBox cboMain; private JTextField txtChoice = new JTextField(); public static void main(String [] args) { JComboDemo list = new JComboDemo(); } public JComboDemo() { final int WIDTH = 300; final int HEIGHT = 200; JFrame window = new JFrame("JCombo Demo"); window.setSize(WIDTH,HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set close operation Font font = new Font("Serif",Font.BOLD,24); cboMain = new JComboBox(); cboMain.setFont(font); cboMain.addActionListener(this);
// add items to combo box cboMain.addItem("rutabaga"); cboMain.addItem("sweet potato"); cboMain.addItem("brussels sprouts"); cboMain.addItem("spinach"); cboMain.addItem("cauliflower"); // set up GUI JPanel panMain = new JPanel(); panMain.setLayout(new BorderLayout()); panMain.add(BorderLayout.NORTH,cboMain);
txtChoice.setFont(font); txtChoice.setText("Default choice " + cboMain.getSelectedItem()); panMain.add(BorderLayout.SOUTH,txtChoice);
// add main panel to frame window.add(panMain); window.setVisible(true); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (e.getActionCommand().equals("comboBoxChanged")) { int i = cboMain.getSelectedIndex(); String s2 = cboMain.getItemAt(i).toString(); txtChoice.setText("Selected " + s2); } } }
|
 |
 |
 |
 |
  | Putting JRadioButtons and JCheckBoxes together -- Pizza Order
|
 |
 |
 |
 |
  | Putting JLists and JComboBoxes and JRadioButtons and JCheckBoxes and JTextFields together -- Skateboard Designer
|
 |
 |
 |
 |
  | Read pp. 748-764 of the text.
|
 |
 |
 |
 |
  | Write a program to implement the Skateboard Designer program whose GUI is shown above, and have it calculate the subtotal, tax (6%) and total cost each time a change is made.
|
 |
 |
 |
 |
  | Alternative program: create your own program that includes lists, radio buttons, check boxes and text fields.
|
 |
 |
|


 |
 |
 |