1 / 15

Combo Box, Check Boxes, and Radio Buttons

Combo Box, Check Boxes, and Radio Buttons. Radio Buttons. User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive

cleta
Download Presentation

Combo Box, Check Boxes, and Radio Buttons

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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(mbutton);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 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);

  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 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(); }

  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.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 } }

  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.addItem("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 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!!

  12. Combo Boxes stored ‘indexed’ items Another way to initialize the ComboBox selection: facename.setSelectedIndex(0); Accordingly, can get user selection:int 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(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; }

  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

More Related