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

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

TCU CoSc Programming with Java Handling Events.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
Event Handling Events and Listeners Timers and Animation.
Lecture 15 Graphical User Interfaces (GUI’s). Objectives Provide a general set of concepts for GUI’s Layout manager GUI components GUI Design Guidelines.
GUI Programming using NetBeans. What is a GUI ? GUI – Graphical User Interface The (visual) interface between humans and computers Ranging from command.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Using Inheritance to Customize Frames Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components.
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
By: Stag and Fish; Party Animals.  Radio buttons are a way of letting users choose only one option of a list.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
CS Lecture 01 Frames and Components and events Lynda Thomas
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
SWING. AbstractButton Swing Buttons are subclasses of the AbstractButton class, which extends JComponent. Abstract class javax.swing.AbstractButton AbstractButton.
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
GUI Programming using NetBeans. RHS – SOC 2 GUI construction We have previously talked about elements in a (simple) GUI –Frames, Panes and Dialogs –Text.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Class Class 20 Objectives Use JCheckbox, JRadioButton, and a JComboBox in a UID.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Review_6 AWT, Swing, ActionListener, and Graphics.
Computer Science 209 GUIs Model/View/Controller Layouts.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
GUI Tutorial 1. A Bit of Philosophy on what to Teach  There are numerous libraries, frameworks, options  Modern GUIs are often developed using XML (e.g.,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
GUI Tutorial 2. What did we do last time?  Basic flow  instance variables, set up in ctor, close operation, size, visible  JFrame  Event-driven programming.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit 6 Graphical user interfaces 1.
Multiple buttons and action calls
Modular Event Handling
GUI Programming using NetBeans
A First Look at GUI Applications Radio Buttons and Check Boxes
Christopher Budo, Davis Nygren, spencer franks, Luke miller
A First Look at GUI Applications
Advanced User Interfaces
Java GUI.
CHAPTER 7 & 8 REVIEW QUESTIONS
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Tooltip -Radio Button.
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 12 GUI Basics.
Presentation 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