Lecture 8 Object Oriented Programming Using Java

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
CS18000: Problem Solving and Object-Oriented Programming.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Dale Roberts GUI Programming using Java - Mouse Events Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
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.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
Intermediate Java1 An example that uses inner classes Week Four Continued.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
1 CSE 331 More Events (Mouse, Keyboard, Window, Focus, Change, Document...) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D.
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.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
Graphical User Interface Components: Part 1 Chapter 11.
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.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
OOP (Java): GUI II/ OOP Objectives – –describe some more GUI features: JPanel, and mouse listeners/adapters Semester 2,
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java's Graphical User Interface Toolkit
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Layout Managers Arranges and lays out the GUI components on a container.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
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.
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
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.
© Marty Hall, Larry Brown Web core programming 1 Handling Mouse and Keyboard Events.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
EE2E1. JAVA Programming Lecture 6 Event handling and building user interfaces with Swing.
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.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
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.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Events and Event Handling
Chapter 14 Event-Driven Programming
Chapter 12 Event-Driven Programming
Handling User Events with Swing
Event Handling Chapter 2 Objectives
Advanced User Interfaces
Computer Science 209 Graphics and GUIs.
Programming in Java Event Handling
GUI Programming III: Events
Event-driven programming for GUI
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
Chapter 7-3 (Book Chapter 14)
Chapter 16 Event-Driven Programming
Programming Graphical User Interface (GUI)
Making Java GUIs Functional
Final project.
Presentation transcript:

Lecture 8 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar

Mouse Event Handling Two interfaces MouseListener and MouseMotionListener MouseListener Methods mousePressed mouseClicked mouseReleased mouseEntered mouseExited MouseMotionListener Methods mouseDragged mouseMoved

Mouse Events public class MouseTracker extends JFrame implements MouseListener, MouseMotionListener { private JLabel lblstatus; public MouseTracker() { super("Mouse Events Demonstration"); Container content = getContentPane(); lblstatus = new JLabel(); content.add(lblstatus,BorderLayout.SOUTH); addMouseListener(this); addMouseMotionListener(this); setSize(300,300); setVisible(true); }

Mouse Events lblstatus.setText("Mouse Cliked At: " public void mouseClicked(MouseEvent event){ lblstatus.setText("Mouse Cliked At: " + event.getX() + "," + event.getY()); } public void mousePressed(MouseEvent event){ lblstatus.setText("Mouse Pressed At: " public void mouseReleased(MouseEvent event){ lblstatus.setText("Mouse Released At: " public void mouseEntered(MouseEvent event){ lblstatus.setText("Mouse Entered At: "

Mouse Events Write main section for public void mouseExited(MouseEvent event){ lblstatus.setText("Mouse outside of window "); } public void mouseDragged(MouseEvent event){ lblstatus.setText("Mouse Dragged At: " + event.getX() + "," + event.getY()); public void mouseMoved(MouseEvent event){ lblstatus.setText("Mouse Moved At: " Write main section for

Adapter Classes An adapter class implements an interface and provides a default implementation (with an empty method body) of every method in an interface. The programmer can extend the adapter class to inherit the default implementation of every method and can override them. Adapter Class Interface Componet Adapter ComponentListener ContainerAdapter ContainerListener FocusAdapter FocusListener KeyAdapter KeyListener MouseAdapter MouseListener MouseMotionAdapter MouseMotionListener WindowAdapter WindowListener

Using Adapter Classes public class AdapterClassTest extends JFrame { private JLabel lblstatus; public AdapterClassTest() { super("Adapter class demo"); Container content = getContentPane(); lblstatus = new JLabel(); content.add(lblstatus,BorderLayout.SOUTH); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent ev){ lblstatus.setText("Mouse Clicked at: " + ev.getX() + "," + ev.getY()); } }); setSize(300,300); setVisible(true); } }

Key event handling KeyListener interface is implemented in order to handle key events. public class KeyEventTest extends JFrame implements KeyListener{ private JTextArea txtarea; String line1 = "", line2 = "", line3 = ""; public KeyEventTest() { super("Key Event Handling"); txtarea = new JTextArea(); txtarea.setText("Press any key on keyboard...."); txtarea.setEnabled(false); txtarea.setDisabledTextColor(Color.GRAY); getContentPane().add(txtarea); addKeyListener(this); setSize(300,300); setVisible(true); }

Key event handling public void keyPressed(KeyEvent ev){ line1 = "Key pressed: " + ev.getKeyText(ev.getKeyCode()); this.setLine2and3(ev); } public void keyReleased(KeyEvent ev){ line1 = "Key released: " + ev.getKeyText(ev.getKeyCode()); public void keyTyped(KeyEvent ev){ line1 = "Key typed: " + ev.getKeyChar();

Key event handling Write main section for private void setLine2and3(KeyEvent ev){ line2 = "This key is: " + (ev.isActionKey() ? "":"not") + " an action key"; String temp = ev.getModifiersExText(ev.getModifiers()); line3 = "Modifiers keys pressed: " + (temp.equals("")? "none" : temp); txtarea.setText(line1 + "\n" + line2 + "\n" + line3 + "\n"); } Write main section for

Layout Managers Description Layout Manager FlowLayout Places components sequentially left to right as they were added BorderLayout Arranges the components into five areas (NORTH,SOUTH,EAST,WEST and CENTER) GridLayout Arranges the components into rows and columns Layout Managers are provided to arrange GUI components in a container for presentation purposes. These provides layout capabilities to place the components in a container

Layout Demonstration public class GridLayoutTest extends JFrame implements ActionListener { private JButton buttons[]; private final String names[] = {"one","two","three","four","five","six"}; private boolean toggle = true; private Container content; private GridLayout grid1,grid2; public GridLayoutTest() { super("Layout Test"); content = getContentPane(); grid1 = new GridLayout(2,3,5,5); grid2 = new GridLayout(3,2); content.setLayout(grid1);

Layout Demonstration buttons = new JButton[names.length]; for(int count=0; count < names.length; count++){ buttons[count] = new JButton(names[count]); buttons[count].addActionListener(this); content.add(buttons[count]); } setSize(300,150); setVisible(true);

Layout Demonstration public void actionPerformed(ActionEvent ev){ if(toggle) content.setLayout(grid2); else content.setLayout(grid1); toggle = false; content.validate(); } }// End of class Home Work on Layouts

JComboBox with JCheckBox public class ComboBoxTest extends JFrame implements ItemListener { private JComboBox cmboptions; private JCheckBox chkdb,chkoop,chkos,chkdld; public ComboBoxTest() { super("ComboBox Testing"); Container content = getContentPane(); content.setLayout(new FlowLayout()); chkdb = new JCheckBox("Database"); chkoop = new JCheckBox("OOP"); chkos = new JCheckBox("Operating Systems"); chkdld = new JCheckBox("DLD"); chkdb.addItemListener(this); chkoop.addItemListener(this); chkos.addItemListener(this); chkdld.addItemListener(this);

JComboBox with JCheckBox content.add(chkdb); content.add(chkoop); content.add(chkos); content.add(chkdld); cmboptions = new JComboBox(); content.add(cmboptions); setSize(300,300); setVisible(true); }

JComboBox with JCheckBox public void itemStateChanged(ItemEvent ev){ String itm = ""; String txt = ""; boolean found = false; if(ev.getSource() == chkdb){ itm = chkdb.getText(); } else if(ev.getSource() == chkoop){ itm = chkoop.getText(); else if(ev.getSource() == chkos){ itm = chkos.getText();

JComboBox with JCheckBox else if(ev.getSource() == chkdld){ itm = chkdld.getText(); } for(int x=0; x < cmboptions.getItemCount(); x++){ txt = cmboptions.getItemAt(x).toString(); if(itm == txt){ found = true; break; } } if(!found) cmboptions.addItem(itm); }