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.

Slides:



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

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.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Lecture 19 Graphics User Interfaces (GUIs)
Final Review. Review A Binary Search divides a sorted data structure into two equal parts and each time throws away the part that cannot contain the item.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
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.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Consolidation. Code making (i.e. making ciphers) is undertaken by a cryptographer whereas cryptanalysts try to break ciphers in order to gain intelligence.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Interfaces & Polymorphism part 2:
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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,
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.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Copyright © Curt Hill First Windows Program GUIs in Eclipse.
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 ( 李德成 )
Layout Managers Arranges and lays out the GUI components on a container.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
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.
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
© 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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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 Handling CS 21a: Introduction to Computing I First Semester,
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
© 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Chapter 8 Event Handling.
CompSci 230 S Programming Techniques
Java Applet What is a Java Applet? How is applet compiled?
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Introduction to Event Handling
Chapter 7-2 (Book Chapter 14)
Advanced Programming in Java
Chapter 7-2 (Book Chapter 14)
CiS 260: App Dev I Chapter 6: GUI and OOD.
Final Review.
Presentation transcript:

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 and convert it to an integer so arithmetic can take place Use the plus operator

3 Expanding the program from class05: When the user clicks on the Add button, get the data in each of the text fields and add the two values together

4 In case the user clicks on the add button without typing anything into one or both of the textfields, add code to the previous problem from class 5 that will initially place a 0 in the two textfields textField1.setText(“0”); textField2.setText(“0”);

5 How do we get the text that the user enters in a text field? textField1.getText() How do we put the value that the user entered from one text field into a different text field? textField3.setText(textField1.getText());

6 How do we get the text that the user enters in a certain text field and add the value to the text that the user enters into a different text field and place the answer in a third text field?

7

8 Crucial to your understanding: Data entered into text fields is what we call text or “string” data. In text format, this data cannot be used in any mathematical operations. Therefore, once you get the text, you have to convert the text to integer so that math can be performed. Additionally, if you want the mathematical answer to go back into a text field, you have to do the reverse… convert the integer to text (string) data. All of the above would be done when the add button is clicked.

9 So first, add the code to allow an action to take place when the “add” button is clicked with the mouse. Add to import statements: import java.awt.event.*; Add to user interface code: button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {calculateJButtonActionPerformed(e); } } )

10 Before method main, add the code for the method calculateJButtonActionPerformed. This method will contain the code that defines WHAT is to be done when the button is clicked. private void calculateJButtonActionPerformed(ActionEvent e) { textField3.setText(String.valueOf( Integer.parseInt (textField1.getText()) + Integer.parseInt(textField2.getText() ) ) ) } // end of method Gets the text entered by the user in textField1 Gets the text entered by the user in textField2

11 private void calculateJButtonActionPerformed(ActionEvent e) { textField3.setText(String.valueOf( Integer.parseInt (“23”) + Integer.parseInt(“42”) ) ) } Gets the text entered by the user in textField1 Gets the text entered by the user in textField2 Data (23 and 42 are text and not integers); therefore, these numbers have to be converted to integers for use in arithmetic. This is done through the parseInt method.

12 private void calculateJButtonActionPerformed(ActionEvent e) { textField3.setText(String.valueOf( ) ) } Data (23 and 42 are text and not integers); therefore, these numbers have to be converted to integers for use in arithmetic. This is done through the parseInt method.

13 private void calculateJButtonActionPerformed(ActionEvent e) { textField3.setText(String.valueOf(65 ) ) } The sum 65 is an integer but we want to put it in textField3. For data to go into a textField it has to be text (a string) not an integer. String.valueOf converts the 65 to a string (text characters).

14 private void calculateJButtonActionPerformed(ActionEvent e) { textField3.setText(“65” ) } The setText method then puts the 65 into textField3.

15 Putting it all together:

16 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GuiTest extends JFrame { private JLabel textLabel1,textLabel2,outputLabel; private JTextField textField1, textField2, textField3; private JButton button1, button2; public GuiTest() { Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout());

17 textLabel1= new JLabel ("Enter score 1"); contentPane.add(textLabel1); textField1= new JTextField(10); contentPane.add(textField1); textField2 = new JTextField(10); contentPane.add(textField2); textField1.setText("0"); textField2.setText("0"); textLabel2 = new JLabel("Enter score 2"); outputLabel = new JLabel("The answer is:"); textField3 = new JTextField(10); textField3.setText("0"); textField3.setEditable(false);

18 contentPane.add(textLabel2); button1 = new JButton ("Add"); button2 = new JButton("Subtract"); button1.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent e) { calculateActionPerformed(e); } } );

19 contentPane.add(button1); contentPane.add(button2); contentPane.add(outputLabel); contentPane.add(textField3); setTitle("Testing My Gui"); setVisible(true); setSize(450, 300); } public void calculateActionPerformed(ActionEvent e) { textField3.setText(String.valueOf( Integer.parseInt(textField1.getText()) + Integer.parseInt(textField2.getText()))); }

20 public static void main(String a [ ]) {GuiTest myFrame = new GuiTest(); myFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } // end of main } // end of class

21 Conclusion of Class 6