Class 20. 2 Class 20 Objectives Use JCheckbox, JRadioButton, and a JComboBox in a UID.

Slides:



Advertisements
Similar presentations
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Advertisements

Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Lecture 19 Graphics User Interfaces (GUIs)
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
JCheckBox and JRadioButton State buttons –On/Off or true / false values –Java provides three types JToggleButton JCheckBox JRadioButton.
OOP (Java): GUI I/ OOP Objectives – –describe some of the GUI controls and their listeners; more appear in part 13 Semester 2,
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.
GUI programming Graphical user interface-based programming.
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.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
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.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
More GUI CSCE 190 – Java Instructor: Joel Gompert Lecture 8 – July 28, 2004.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
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.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
A simple swing example GETTING STARTED WITH WIND CHILL.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
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.”
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Basics of GUI Programming Chapter 11 and Chapter 22.
Ch15 簡單計算機 物件導向系統實務. Ch22_Main.java import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch22_Main { public static void main(String.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
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.,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Graphical User Interface (GUI)
1 Chapter 13 – More GUI Components Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Dept. of CSIE, National University of Tainan 10/21/2012 Working with Swing.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Introduction to Graphics
Lecture 16 More GUI programming
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Advanced Programming in Java
CiS 260: App Dev I Chapter 6: GUI and OOD.
CNT 4714: Enterprise Computing Spring 2010
Presentation transcript:

Class 20

2 Class 20 Objectives Use JCheckbox, JRadioButton, and a JComboBox in a UID

3 JCheckbox –A boolean GUI component that is either selected or not selected JRadioButton –A group of usually circular gui components that allow for only one in the group to be selected JComboBox –A drop-down list from which the user can make a selection (in AWT called Choice list)

4 Class JCheckBox is a JComponent creates a single box that can be selected or not selected

5 Write a windowed application that will display the following gui. After the user makes the selections, you program will tell the user how many classes they chose.

6

7 // Creating Checkbox buttons. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CheckBoxFrame extends JFrame { private String classes[] = {“Swim”, “Dance”,”Tennis”}; private JTextField nameField; private JCheckBox choices[]; private JLabel labelField; private JLabel label1; private JButton doneButton;

8 public CheckBoxFrame() { super( "JCheckBox Test" ); Container theWindow = getContentPane(); theWindow.setLayout(new FlowLayout()); doneButton = new JButton(“Done”); nameField = new JTextField( 20); label1 = new JLabel(“enter your name”); labelField = new JLabel(“Select the classes you“ + “want to take then click the \”Done\” button ”); nameField.setFont( new Font( "TimesRoman", Font.PLAIN, 14 ) ); theWindow.add(label1); theWindow.add( nameField); theWindow.add(labelField);

9 choices = new JCheckBox[classes.length]; for (int i = 0; i < classes.length; i++) { choices[i] = new JCheckBox(classes[i]); theWindow.add(choices[i]); } theWindow.add(doneButton); doneButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { buttonClicked(ev) }; } ); setSize(350,200); setVisible(true); } // end of constructor

10 public void buttonClicked( ActionEvent event ) { int classCount =0; for (int i=0; i< classes.length; i++) if ( choices[i].isSelected() ==true) ++classCount; JOptionPane.showMessageDialog( null, nameField.getText() + “ \n You have” + “ decided to take “ + classCount + “ class(es)”); }

11 public static void main(String a[]) { CheckBoxFrame cf = new CheckBoxFrame(); cf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } } // end of class

12 Add JRadioButtons to the previous application to allow user to choose MWF or Tues./Thurs classes.

13

14 // Creating Radio buttons. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RadioButtonTest extends JFrame { private String classes[] = {“Swim”, “Dance”,”Tennis”}; private JTextField nameField; private JCheckBox choices[]; private JLabel labelField1,labelField2; private JLabel label1; private JButton doneButton;

15 private JRadioButton dayChoice1; private JRadioButton dayChoice2; private ButtonGroup radioGroup; // new gui components

16 public RadioButtonTest() { super( “RadioButton Test" ); Container theWindow = getContentPane(); windowFrame.setLayout(new FlowLayout()); doneButton = new JButton(“Done”); nameField = new JTextField( 20); label1 = new JLabel(“Enter your name”); labelField1 = new JLabel(“Select the classes you “ + “want to take ”); labelField2 = new JLabel(“and the days, then click” + “the \”Done\” button”); nameField.setFont( new Font( "TimesRoman", Font.PLAIN, 14 ) ); theWindow.add(label1); theWindow.add( nameField); theWindow.add(labelField1); theWindow.add(labelField2);

17 // create logical relationship between JRadioButtons radioGroup = new ButtonGroup(); radioGroup.add( dayChoice1); radioGroup.add( dayChoice2 );

18 choices = new JCheckBox[classes.length]; for (int i = 0; i < classes.length; i++) { choices[i] = new JCheckBox(classes[i]); theWindow.add(choices[i]); } dayChoice1 = new JRadioButton(“MWF”,true); dayChoice2= new JRadioButton(“TTH”,false); theWindow. add(dayChoice1); theWindow.add(dayChoice2); theWindow.add(doneButton); doneButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { buttonClicked(ev) }; } ); setSize(350,200); setVisible(true); } // end of constructor

19 public void buttonClicked( ActionEvent event ) { int classCount =0; String daySelection; if (dayChoice1.isSelected() == true) daySelection = “MWF”; else daySelection = “TTH”; for (int i=0; i< classes.length; i++) if ( choices[i].isSelected() ==true) ++classCount; JOptionPane.showMessageDialog( null, nameField.getText() + “ \n You have” + “ decided to take “ + classCount + “ class(es)” + “on ” + daySelection); }

20 public static void main(String a[]) { RadioButtonTest rt = new RadioButtonTest(); rt.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }

21 Use a JComboBox for making a selection

22

23

24 // ComboBoxTest.java // Using a JComboBox to select an image to display. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxFrame extends JFrame { private JComboBox choicesJComboBox; private JLabel label; private String names[] = { “Aruba”, “Cancun”, “Dallas”, “Puerto Vallarta”, “San Juan” };

25 private double costs[] = { , , 0.0, , }; public ComboBoxFrame() { super( "Testing JComboBox" ); Container windowPane = getContentPane(); windowPane.setLayout( new FlowLayout() ); choicesJComboBox = new JComboBox( names ); choicesJComboBox.setMaximumRowCount( 3 ); windowPane.add( choicesJComboBox ); label = new JLabel(“Make a selection ” + “for your 7 day spring break trip” ); add( label );

26 choicesJComboBox.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { whatSelection(e); } ); setSize(350,200); setVisible(true); } // end of constructor private void whatSelection( ItemEvent event ) { if (event.getStateChange() == ItemEvent.SELECTED) { int selection = choicesJComboBox.getSelectedIndex(); JOptionPane.showMessageDialog(null, “You selected” + names[selection] + “for” + “ a total cost of “ + costs[selection]); }

27 public static void main(String a[]) { ComboBoxFrame cf = new ComboBoxFrame(); cf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }

28 Conclusion of class 20