Unit 141 Further GUI Programming Learning Outcomes oExtend examples presented to write more useful applications. oWrite non-trivial, event-driven GUI applications.

Slides:



Advertisements
Similar presentations
Unit 121 A layout manager is an object that determines the manner in which components are arranged in a container. Each layout manager implements one of.
Advertisements

Graphical User Interfaces (Part IV)
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
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.
1 lecture 14 GUI PROGRAMMING IVLecture 15 Graphical User Interfaces (cont.) Overview  GUI Programming (cont.)  Example 1: Creating Multi-layout GUIs.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
Event-Driven Programming
Chapter 7 Event-Driven Programming and Basic GUI Objects.
1 lecture 12Lecture 15 Graphical User Interfaces (cont.) Overview  GUI Programming (cont.)  Example 1: Creating Multi-layout GUIs with Tooltips.  Example.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Graphical User Interfaces
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling Objects First with Java - A Practical.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
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.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CSE 219 Patterns in Programming More Design Patterns.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
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.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
CSE1030-HR GUI The Big Picture Building the View Building the Controller Separating the Concerns Going Further.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
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.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces 2.0.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Nested Classes and Event Handling Chapter 10. Overview We explain how to write Java code that responds to events. The Timer class can be used to respond.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
1 IM103 week 10 Enhancing the user interface By the end of lecture you should be able to:  distinguish between heavyweight and lightweight components;
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.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Lesson 33: Layout management and drawing – Java GUI.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Menus Pull-downs and popups. Tooltips –a ToolTip is a context-sensitive text string that is displayed in a popup window when the mouse rests over a particular.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
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.
제 14 장 고급 스윙 컴포넌트.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
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.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling © 2017 Pearson Education, Inc. Hoboken,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Object-Oriented Programming (Java), Unit 22
GUI III IS
Course Outcomes of Advanced Java Programming AJP (17625, C603)
1 Graphical User Interfaces
Advanced Programming in Java
Presentation transcript:

Unit 141 Further GUI Programming Learning Outcomes oExtend examples presented to write more useful applications. oWrite non-trivial, event-driven GUI applications. Introduction Example 1: Enhancing the Telephone Handset Example Example 2: Menu Test Self-check Exercise 1 Example 3: File Dialog Test Example 4: Popup Menu Test Self-check Exercise 2 Exercises

Unit 142 Introduction This is our concluding session on GUIs and event-driven programming. In our discussion on GUI-based programs so far, we have covered: –GUI design issues, GUI components and containers. –Basic event handling. –Basic layout management. We now cover typical GUI elements found in Windows environment including: –Menus and pop-up menus –File dialogs – Mnemonics and keyboard accelerator With this coverage the student's knowledge base of the basics would be broadened.

Unit 143 Introduction to Example 1 This example builds on Example 4 of the preceding section. Example 4 of the preceding section developed a simple GUI for a telephone handset. We will now add event-handling code to display typed numbers. Here is a sample output of the program:

Unit 144 Example 1: The Telephone Handset 1 import java.awt.*; import javax.swing.*; import java.awt.event.*; 2 class TelephoneTest extends TestGridBagLayout 3 implements ActionListener{ 4 public TelephoneTest(){ 5 Component components[] = getContentPane().getComponents(); 6 JPanel cancelPanel = (JPanel)components[13]; 7 JButton cancel = (JButton)cancelPanel.getComponent(0); 8 for(int i=0;i<components.length; i++){ 9 if(components[i] instanceof JButton) 10 ((JButton)components[i]).addActionListener(this); 11 } 12 cancel.addActionListener(this); 13 } 14 public void actionPerformed(ActionEvent ae) { 15 if (ae.getActionCommand().equals("Cancel")) 16 display.setText(""); 17 else 18 display.setText(display.getText()+ae.getActionCommand()); 19 } 20 public static void main(String [] args){ 21 new TelephoneTest().setVisible(true); 22 }}

Unit 145 Introduction to Example 2 In this example we demonstrate how menus, separator, mnemonic and accelerators can be added into an application. The output of the example is as follows:

Unit 146 Class Hierarchy for Menus Classes used to define menus are: oJMenuBar oJMenuItem oJMenu oJCheckButtonMenuItem oJRadioButtonMenuItem

Unit 147 Details of Program Output

Unit 148 Example 2: Menus and Mnemonics 1 import java.awt.event.*; 2 import javax.swing.*; 3 class MenuTest extends JFrame { 4 private JMenuBar menuBar = new JMenuBar(); 5 protected JMenu fileMenu = new JMenu("File"); 6 protected JMenuItem neW, open, quit, save, print; 7 private JMenuItem saveCurrent, saveAs, saveAll; 8 MenuTest(String title){ 9 super(title); 10 setJMenuBar(menuBar); 11 menuBar.add(fileMenu); 12 fileMenu.setMnemonic('F'); 13 fileMenu.add(neW = new JMenuItem ("New")); 14 fileMenu.add(open = new JMenuItem ("Open")); 15 open.setMnemonic('o'); 16 fileMenu.add(save = new JMenu ("Save")); 17 save.add(saveCurrent = new JMenuItem ("Save Current")); 18 save.add(saveAs = new JMenuItem ("Save As")); 19 save.add(saveAll = new JMenuItem ("Save All")); 20 fileMenu.add(save); 21 fileMenu.add(print = new JCheckBoxMenuItem ("Print")); 22 fileMenu.addSeparator();

Unit 149 Menus and Mnemonics (cont’d) 23 fileMenu.add(quit = new JMenuItem("Quit")); 24 quit.setMnemonic(KeyEvent.VK_Q); 25 quit.setAccelerator( 26 KeyStroke.getKeyStroke(KeyEvent.VK_Q,KeyEvent.CTRL_MASK)); 27 quit.addActionListener(new ActionListener(){ 28 public void actionPerformed(ActionEvent e){ 29 int result=JOptionPane.showConfirmDialog(MenuTest.this, 30 "Quit/Close Menu Demos Window?"); 31 if (result == JOptionPane.YES_OPTION) 32 System.exit(0); 33 } 34 }); 35 setSize(300,300); 36 } 37 public static void main(String args[]){ 38 MenuTest t = new MenuTest("Menus Test"); 39 t.setVisible(true); 40 } 41 }

Unit 1410 Introduction to Example 3 This example extends Example 2 to launch a file dialog on selecting the Open menu. The selected file name is simply printed on another display window. The output is as follows:

Unit 1411 Example 3: File Dialog Test 1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 class FileDialogTest extends MenuTest { 5 JEditorPane textPane= new JEditorPane(); 6 FileDialogTest(String title){ 7 super(title); 8 open.addActionListener( new ActionListener() { 9 public void actionPerformed(ActionEvent e ) { 10 FileDialog fd = new FileDialog(FileDialogTest.this); 11 fd.setVisible(true); 12 textPane.setText("Selected file: "+fd.getFile()); 13 } }); 14 getContentPane().add(textPane); 15 } 16 public static void main(String args[]){ 17 FileDialogTest t = new FileDialogTest("File Dialog Test"); 18 t.setVisible(true); } 21 }

Unit 1412 Introduction to Example 4 This example extends Example 2 of the preceding section to add pop-up menus.. The output looks like: The menu consists of three colors that can be used to reset a component’s background color.

Unit 1413 Example 4: Popup Menu Test 1 import java.awt.*; import java.awt.event.*; import javax.swing.*; 2 public class PopupMenuTest extends TestGridLayout{ 3 Component selectedComponent; 4 JPopupMenu colorMenu = new JPopupMenu(); 5 JMenuItem blue, white, yellow; 6 public PopupMenuTest(){ 7 setTitle("Popup Menu Test"); 8 colorMenu.add(blue=new JMenuItem("Blue")); 9 colorMenu.add(white=new JMenuItem("White")); 10 colorMenu.add(yellow=new JMenuItem("Yellow")); 11 Component components[] = getContentPane().getComponents(); 12 class MyListener extends MouseAdapter { 13 public void mousePressed(MouseEvent e){checkPopup(e);} 14 public void mouseClicked(MouseEvent e){checkPopup(e);} 15 public void mouseReleased(MouseEvent e){checkPopup(e);} 16 public void checkPopup(MouseEvent e) { 17 if (e.isPopupTrigger()){ 18 selectedComponent = e.getComponent(); 19 colorMenu.show(e.getComponent(),e.getX(),e.getY()); 20 } 21 } 22 }

Unit 1414 Popup Menu Test (cont’d) 23 MouseListener mylistener = new MyListener(); 24 for(int i=0;i<components.length-1; i++){ 25 JButton b = (JButton)components[i]; 26 b.addMouseListener(mylistener); 27 } 28 blue.addActionListener(new ActionListener () { 29 public void actionPerformed(ActionEvent ae){ 30 selectedComponent.setBackground(Color.blue); 31 }}); 32 white.addActionListener(new ActionListener() { 33 public void actionPerformed(ActionEvent ae){ 34 selectedComponent.setBackground(Color.white); 35 }}); 36 yellow.addActionListener(new ActionListener() { 37 public void actionPerformed(ActionEvent ae){ 38 selectedComponent.setBackground(Color.yellow); 39 }}); 40 } 41 public static void main(String[] args) { 42 new PopupMenuTest().setVisible(true); 43 } 44 }

Unit 1415 Review Exercises 1What are the event sources and event listeners in Example 1? 2Can the event handler in Example 1 be implemented in a single anonymous class? If not, why not? 3Modify Example 1 to use two inner classes instead of one. Can the two inner classes both be anonymous? Is the program with one inner class better than the one with two? Explain. 4Enhance Example 1 so that when the Redial button is pushed the last text displayed on the text component is displayed again. 5Write a Java program to illustrate how a component event can be handled. 6Modify Example 3 so that when a file is double-clicked from the file dialog indow the file is opened rather than displaying its name in the editor pane. 7When you run Example 4, you will find that the lower part of the window is not responding to the popup menu. Modify this program to ensure that that part of the window also responds to the popup menu events like the buttons in the window.