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.

Slides:



Advertisements
Similar presentations
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Advertisements

Graphical User Interfaces, 2D Graphics & Game Programming.
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.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Chapter 9 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Event Handling Events and Listeners Timers and Animation.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine: Interfaces and Polymorphism.
Graphical User Interfaces
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
GUIs Mimi Opkins CECS277.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
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 GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In order to display a drawing in a frame, define a class that extends.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine and Ten Graphics.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 9 Chapter.
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Review_6 AWT, Swing, ActionListener, and Graphics.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Chapter 9 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Event Handling and GUI Components.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Events and Event Handling
CompSci 230 S Programming Techniques
Computer Science 209 Graphics and GUIs.
Chapter 10 – Interfaces.
Event-driven programming for GUI
Chapter 12 Event Handling
Web Design & Development Lecture 12
Final project.
Presentation transcript:

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 to create a GUI which allows the user to enter two values, and have them summed when a button is pressed. Continued…

We will need two textfields, one for each value to be summed ………… To create a JTextField component, call a JTextfield class constructor. / / Supply a button that the user can press to indicate that the input is ready for processing final int FIELD_WIDTH = 10; // In characters JTextField op1 = new JTextField(FIELD_WIDTH); OR JTextField op1 = new JTextField(“0”); OR final int FIELD_WIDTH = 10; // In characters JTextField op1 = new JTextField(“0”, FIELD_WIDTH);

It is good form to label a textfield, so that the user knows what it is for: * / JLabel op1Label = new JLabel(“Operand 1: "); JLabel op2Label = new JLabel(“Operand 2: "); final int FIELD_WIDTH = 10; // In characters JTextField op1TxtFld = new JTextField(“0”, FIELD_WIDTH); JTextField sumTxtFld = new JTextField(“0”, FIELD_WIDTH);

We will also need to create the button … JButton addBtn = new JButton(“ADD”); //The button's actionPerformed method reads the user input from the //input text fields, converts to int, sums them, and sets output textfield. class AddListener implements ActionListener { public void actionPerformed(ActionEvent event) { int op1Int = Integer.parseInt(op1TxtFld.getText() ); int op2Int = Integer.parseInt(op2TxtFld.getText() ); int sum = op1Int + op2Int; sumTxtFld.setText(“” + sum); } // create listener object and install on button AddListener alisten = new AddListener(); addBtn.addActionListener(alisten); * note: textfields from main are used, must be declared as Final

So,.. Component declarations would be …… * / JLabel op1Label = new JLabel(“Operand 1: "); JLabel op2Label = new JLabel(“Operand 2: "); final int FIELD_WIDTH = 10; // In characters final JTextField op1TxtFld = new JTextField(“0”, FIELD_WIDTH); final JTextField sumTxtFld = new JTextField(“0”, FIELD_WIDTH);

So we have………… public class Adder { public static void main (String[] args) { JLabel op1Label = new JLabel(“Operand 1: "); JLabel op2Label = new JLabel(“Operand 2: "); final int FIELD_WIDTH = 10; // In characters final JTextField op1TxtFld = new JTextField(“0”, FIELD_WIDTH); final JTextField op2TxtFld = new JTextField(“0”, FIELD_WIDTH); final JTextField sumTxtFld = new JTextField(“0”, FIELD_WIDTH); JButton addBtn = new JButton(“ADD”); class AddListener implements ActionListener { public void actionPerformed(ActionEvent event) { int op1Int = Integer.parseInt(op1TxtFld.getText() ); int op2Int = Integer.parseInt(op2TxtFld.getText() ); int sum = op1Int + op2Int; sumTxtFld.setText(“” + sum); } AddListener alisten = new AddListener(); addBtn.addActionListener(alisten); //components are all ready to put on panel !!

JPanel panel = new JPanel(); panel.add(op1TxtFld); panel.add(op2TxtFld); panel.add(addBtn); panel.add(sumTxtFld); panel.add(op1Label); panel.add(op2Label); JFrame theframe = new JFrame(); theframe.setSize(100,200 ); theframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); theframe.add(panel); theframe.setVisible(true); } //end main } // end class

Mouse Events Use a mouse listener to capture mouse events Implement the MouseListener interface: Continued… public interface MouseListener { void mousePressed(MouseEvent event); // Called when a mouse button has been pressed on a component void mouseReleased(MouseEvent event); // Called when a mouse button has been released on a component void mouseClicked(MouseEvent event); // Called when the mouse has been clicked on a component void mouseEntered(MouseEvent event); // Called when the mouse enters a component void mouseExited(MouseEvent event); // Called when the mouse exits a component }

Mouse Events mousePressed, mouseReleased : called when a mouse button is pressed or released mouseClicked : if button is pressed and released in quick succession, and mouse hasn't moved mouseEntered, mouseExited : mouse has entered or exited the component's area

Mouse Events Add a mouse listener to a component by calling the addMouseListener method: Continued… public class MyMouseListener implements MouseListener { // Implements five methods } MouseListener listener = new MyMouseListener(); component.addMouseListener(listener);

Mouse Events Sample program: enhance RectangleComponentViewer program of Chapter 5; when user clicks on rectangle component, move the rectangle

File RectangleComponent.java 01: import java.awt.Graphics; 02: import java.awt.Graphics2D; 03: import java.awt.Rectangle; 04: import javax.swing.JComponent; 05: 06: /** 07: This component lets the user move a rectangle by 08: clicking the mouse. 09: */ 10: public class RectangleComponent extends JComponent 11: { 12: public RectangleComponent() 13: { 14: // The rectangle that the paint method draws 15: box = new Rectangle(BOX_X, BOX_Y, 16: BOX_WIDTH, BOX_HEIGHT); 17: } 18: Continued…

File RectangleComponent.java 19: public void paintComponent(Graphics g) 20: { 21: super.paintComponent(g); 22: Graphics2D g2 = (Graphics2D) g; 23: 24: g2.draw(box); 25: } 26: 27: /** 28: Moves the rectangle to the given location. x the x-position of the new location y the y-position of the new location 31: */ 32: public void moveTo(int x, int y) 33: { 34: box.setLocation(x, y); 35: repaint(); 36: } Continued…

File RectangleComponent.java 37: 38: private Rectangle box; 39: 40: private static final int BOX_X = 100; 41: private static final int BOX_Y = 100; 42: private static final int BOX_WIDTH = 20; 43: private static final int BOX_HEIGHT = 30; 44: }

Mouse Events Call repaint when you modify the shapes that paintComponent draws box.setLocation(x, y); repaint(); Mouse listener: if the mouse is pressed, listener moves the rectangle to the mouse location Continued…

Mouse Events class MousePressListener implements MouseListener { public void mousePressed(MouseEvent event) { int x = event.getX(); int y = event.getY(); component.moveTo(x, y); } // Do-nothing methods public void mouseReleased(MouseEvent event) {} public void mouseClicked(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} } All five methods of the interface must be implemented; unused methods can be empty

RectangleComponentViewer Program Output Figure 4: Clicking the Mouse Moves the Rectangle

File RectangleComponentViewer2.java 01: import java.awt.event.MouseListener; 02: import java.awt.event.MouseEvent; 03: import javax.swing.JFrame; 04: 05: /** 06: This program displays a RectangleComponent. 07: */ 08: public class RectangleComponentViewer 09: { 10: public static void main(String[] args) 11: { 12: final RectangleComponent component = new RectangleComponent(); 13: 14: // Add mouse press listener 15: 16: class MousePressListener implements MouseListener 17: { Continued…

File RectangleComponentViewer2.java 18: public void mousePressed(MouseEvent event) 19: { 20: int x = event.getX(); 21: int y = event.getY(); 22: component.moveTo(x, y); 23: } 24: 25: // Do-nothing methods 26: public void mouseReleased(MouseEvent event) {} 27: public void mouseClicked(MouseEvent event) {} 28: public void mouseEntered(MouseEvent event) {} 29: public void mouseExited(MouseEvent event) {} 30: } 31: 32: MouseListener listener = new MousePressListener(); 33: component.addMouseListener(listener); 34: Continued…

File RectangleComponentViewer2.java 35: JFrame frame = new JFrame(); 36: frame.add(component); 37: 38: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 39: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 40: frame.setVisible(true); 41: } 42: 43: private static final int FRAME_WIDTH = 300; 44: private static final int FRAME_HEIGHT = 400; 45: }

Self Check 7.What would happen if you omitted the call to repaint in the moveTo method? 8.Why must the MousePressListener class supply five methods?

Answers 7.The rectangle would only be painted at the new location when the component is repainted for some other reason, for example, when the frame is resized. 8.It implements the MouseListener interface, which has five methods.