combo box, check boxes, and radio buttons. radio buttons user can click radio buttons, just like...
TRANSCRIPT
Combo Box, Check Boxes, and Radio Buttons
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(mbutton);group.add(lbutton);
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 don’t 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);
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 !!
// Frame which allows the use to select it’s 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();
}
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);
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.GREEN);
}
}
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
}
}
// 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();
}
}
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() ) . . .
Combo Boxes
Combination of selection and text field
Create JComboBox object, then ‘add’ the items
JComboBox faceName = new JComboBox();faceName.addItem("Serif");faceName.addItem("SansSerif");. . .
(Any object type can be added to ComboBox --toString determines display)
Combo Boxes
To initialize the ComboBox selection:
facename.setSelectedItem(“Italics”);
To get user selection:sel= (String)faceName.getSelectedItem();
Cast needed because return type is Object !!
An ActionEvent is created by a ComboBox if the user makes a choice!!
Combo Boxes stored ‘indexed’ items
Another way to initialize the ComboBox selection:
facename.setSelectedIndex(0);
Accordingly, can get user selection:int ind = faceName.getSelectedIndex();
//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");
//method continues
//set up listener
class BoxListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (cbox.getSelectedItem().equals("Large 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;
}
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