Combo Box, Check Boxes, and Radio Buttons - PowerPoint PPT Presentation

About This Presentation
Title:

Combo Box, Check Boxes, and Radio Buttons

Description:

Title: CHAPTER 1 Author: faculty Last modified by: SMCCCD Created Date: 5/7/2002 12:28:18 AM Document presentation format: On-screen Show Company: University of ... – PowerPoint PPT presentation

Number of Views:172
Avg rating:3.0/5.0
Slides: 16
Provided by: facul353
Category:
Tags: box | boxes | buttons | check | combo | java | radio

less

Transcript and Presenter's Notes

Title: Combo Box, Check Boxes, and Radio Buttons


1
Combo Box, Check Boxes, and Radio Buttons
2
Radio Buttons
  • User can click radio buttons, just like other
    buttons
  • BUT Radio buttons are mutually
    exclusive
  • This is achieved by placing all associated
    buttons in a ButtonGroup object. The ButtonGroup
    turns 1 button off when the next one is turned on
  • sButton new JRadioButton("Small")mButton
    new JRadioButton("Medium") lButton new
    JRadioButton("Large")ButtonGroup group new
    ButtonGroup()group.add(sbutton)group.add(mbutt
    on)group.add(lbutton)

3
Radio Buttons continued
  • It is good practice to initialize one of the
    buttons to ON
  • sButton.setSelected(true)
  • The ButtonGroup is not a visual component.
  • (You dont place it into a panel)
  • The buttons still need to be placed into a
    panel individually.
  • panel.add(sButton)
  • panel.add(mButton)
  • panel.add(lButton)

4
Radio Buttons continued
  • Your code can tell if a RadioButton is selected
  • if (sButton.isSelected())
  • . . .
  • One usually wants to check which button is
    selected WHEN THE USER HAS clicked on ONE!!
  • An ActionEvent is created when the user clicks
    on one of the buttons .. so this code is in an
    ActionListener !!

5
  • // Frame which allows the use to select its
    background color
  • import javax.swing.
  • import java.awt.event.
  • import java.awt.Color
  • import java.awt.BorderLayout
  • public class RadDemo extends JFrame
  • public RadDemo()
  • createpanel() //set
    up panel
  • pack()

6
  • public void createpanel()

  • //set up button and put in panel
  • final JRadioButton redbtn new
    JRadioButton("RED")
  • final JRadioButton bluebtn new
    JRadioButton("BLUE")
  • final JRadioButton greenbtn new
    JRadioButton("GREEN")
  • ButtonGroup grp new ButtonGroup()
  • grp.add(redbtn)
  • grp.add(bluebtn)
  • grp.add(greenbtn)
  • final JPanel btnpanel new JPanel()
    //put on panel
  • btnpanel.add(redbtn)
  • btnpanel.add(bluebtn)
  • btnpanel.add(greenbtn)

7
  • redbtn.setSelected(true)
    //default button/color is red
  • btnpanel.setBackground(Color.RED)
  • class BtnListen implements ActionListener
    //define listener class
  • public void actionPerformed (ActionEvent
    e)
  • if (redbtn.isSelected() )
  • btnpanel.setBackground(Color.RED)
  • else if (bluebtn.isSelected() )
  • btnpanel.setBackground(Color.BLUE
    )
  • else
  • btnpanel.setBackground(Color.GREE
    N)
  • BtnListen blisten new BtnListen()
    //create and register listener object
  • redbtn.addActionListener(blisten)
  • bluebtn.addActionListener(blisten)
  • greenbtn.addActionListener(blisten)
  • getContentPane().add(btnpanel,
    BorderLayout.CENTER) //put on frame

8
  • // main to create and show frame
  • public class ColorPanel
  • public static void main (String args)
  • //create and test one of these frames
  • JFrame newframe new RadDemo()
  • newframe.show()

9
Check Boxes
  • Similar to radio button, not mutually exclusive
  • JCheckBox it new JCheckBox("Italic")
  • JCheckBox bld new JCheckBox(Bold)
  • Don't place into button group
  • in Action listener ..
  • If (it.isSelected() )
  • . . .

10
Combo Boxes
  • Combination of selection and text field
  • Create JComboBox object, then add the items
  • JComboBox faceName new JComboBox()faceName.add
    Item("Serif")faceName.addItem("SansSerif"). .
    .
  • (Any object type can be added to ComboBox
    --toString determines display)

11
Combo Boxes
  • To initialize the ComboBox selection
  • facename.setSelectedItem(Italics)
  • To get user selectionsel (String)faceName.getSe
    lectedItem()
  • Cast needed because return type is Object !!
  • An ActionEvent is created by a ComboBox if the
    user makes a choice!!

12
Combo Boxes stored indexed items
  • Another way to initialize the ComboBox selection
  • facename.setSelectedIndex(0)
  • Accordingly, can get user selectionint ind
    faceName.getSelectedIndex()

13
  • //method returns a panel with a combo box on it
  • public JPanel makebottom()
  • final JTextField outbox new JTextField("
    15.00", 10)
  • //set up combo
    box and add a listener
  • final JComboBox cbox new JComboBox()
  • cbox.addItem("Large Price")
  • cbox.addItem("Medium Price")
  • cbox.addItem("Small Price")
  • cbox.setSelectedItem("Large Price")

14
  • //method continues

  • //set up listener
  • class BoxListener implements ActionListener
  • public void actionPerformed(ActionEv
    ent e)
  • if (cbox.getSelectedItem().equals("La
    rge Price"))
  • outbox.setText("15.00")
  • else if (cbox.getSelectedItem().equals
    ("Medium Price"))
  • outbox.setText("10.00")
  • else
  • outbox.setText("8.00")
  • BoxListener clisten new BoxListener()
  • cbox.addActionListener(clisten)
  • JPanel temp new JPanel()
    //set up panel
  • temp.setBackground(Color.blue)
  • temp.add(cbox)
  • temp.add(outbox)
  • return temp

15
  • JPanel temp new JPanel()
    //set up panel
  • temp.setBackground(Color.blue)
  • temp.add(cbox)
  • temp.add(outbox)
  • return temp
  • //This method is used in file PizzaFrame.java
Write a Comment
User Comments (0)
About PowerShow.com