it appears for a few seconds when the mouse cursor stays motionless

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

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
CS18000: Problem Solving and Object-Oriented Programming.
TCU CoSc Programming with Java Handling Events.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 16 Creating User Interfaces.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
Graphical User Interfaces Allow for interaction with –Buttons –Menus –Text Fields Two Java Libraries to assist in GUI Programming –AWT –Swing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
1 Creating User Interfaces. 2 Motivations A graphical user interface (GUI) makes a system user-friendly and easy to use. Creating a GUI requires creativity.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Using Inheritance to Customize Frames Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
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.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
UID – Event Handling and Listeners Boriana Koleva
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CHAPTER:07 JAVA IDE PROGRAMMING-II Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Swing / Session1 / 1 of 30 Module Introduction Distributed Computing in Java.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Object Oriented Programming.  Interface  Event Handling.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Getting Input. Text Fields A text field is a box that the user can type in Use the JTextField class JTextField tf1 = new JTextField(15); 15 is the field.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Software Construction LAB 08 Java Programming with SWING GUI Builder.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
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 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Handling User Events with Swing
Advanced User Interfaces
Java GUI.
Graphical User Interface (GUI) Components: Part 1 Java How to Program, 9 th Edition Chapter 14.
CSE 114 – Computer Science I Event Programming
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Chapter 7 Creating User Interfaces
Chapter 13 Creating User Interfaces
AWT Components and Containers
Chapter 7-3 (Book Chapter 14)
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Advanced GUIs and Graphics
Presentation transcript:

it appears for a few seconds when the mouse cursor stays motionless tool tip: a tool tip is a small and transient message about a container or atomic component it appears for a few seconds when the mouse cursor stays motionless on the container or atomic componentBIG QUESTIONlabel about the big question no need to create an object representing the tool tip just apply setToolTipText method on the existing cont. or comp. object: c.setToolTipText(text); JButton n1 = new JButton("#1"); p1.add(n1); // Button 1 is contained in p1 n1.setToolTipText("Nested Pannels");

button: JButton class a button is a rectangular area that, if clicked, creates an event object a button can contain a string text and/or an icon, like a label: buttonicon REWIND text create the button object: button = new JButton(); button icon REWIND text

set the font of the button: button.setFont(font); set the text and/or the icon: button.setText(text); button.setIcon(icon); if both text and icon, precise the position of the text relative to the icon: button.setHorizontalTextPosition( JButton.LEFT or JButton.CENTER or JButton.RIGHT); button.setVerticalTextPosition( JButton.TOP or JButton.BOTTOM); set enabled appearance (default appearance) or disabled appearance: button.setEnabled(true); button.setEnabled(false); establishes a character ( ‘a’ for instance) as an equivalent of the button: button.setMnemonic(character);

menu bar and menus: JMenuBar , JMenu , JMenuItem classes the menu bar contains several menus each of these menus is folded by default, but unfolds when we click it each menu , when unfolded, contains  some menu items (sorts of buttons)  eventually some separators between them  eventually other (sub)menus a menu item, if selected, creates an event object

menu bar folded menu unfolded sub menu first menu second menu third menu menu item n.0 menu item n.1 menu item n.2 sub menu n.3 menu item n.4 unfolded menu sub menu n.5 folded sub menu separator

create the menu bar object: menu_bar = new JMenuBar(); add the menu bar to a frame (or applet): frame.setJMenuBar(menu_bar); create a menu object: menu = new JMenu(); set the text and/or the icon for the folded menu: menu.setText(text); menu.setIcon(icon); add to or remove from the menu bar a menu: menu_bar.add(menu); menu_bar.remove(menu);

create a menu item object: menu_item = new JMenuItem(); set the text and/or the icon of the menu item: menu_item.setText(text); menu_item.setIcon(icon); if both text and icon, precise the position of the text relative to the icon: menu_item.setHorizontalTextPosition( JMenuItem.LEFT or JMenuItem.CENTER or JMenuItem.RIGHT); menu_item.setVerticalTextPosition( JMenuItem.TOP or JMenuItem.CENTER or JMenuItem.BOTTOM);

add to or remove from the menu a menu item, a separator or another menu (the elements are piled up in the order in which they are added): menu.add(menu_item); or menu.remove(menu_item); menu.addSeparator(); menu.add(other_menu); or menu.remove(other_menu); set enabled appearance (default appearance) or disabled appearance: menu.setEnabled(true); or menu.setEnabled(false); menu_item.setEnabled(true); or menu_item.setEnabled(false); establishes a character (‘a’ for instance) as an equivalent of the menu item: menu_item.setMnemonic(character);

check boxes: JCheckBox class any number of check boxes in a group can be selected at a given time Hakan Mehmet Fatma

first, create a panel panel with grid layout policy (any number of rows, 1 column) for each of the check boxes ci: ci = new JCheckBox(); ci.setText(text); eventually: ci.setIcon(icon); ci.setSelected(true or false); panel.add(ci); the current state (true or false) of check box ci is returned by ci.isSelected()

radio buttons: JRadioButton class at most one button at a time can be selected Yiltan Mehmet Ali

first, create a panel with grid layout policy (any number of rows, 1 column) for each of the radio buttons ri: ri = new JRadioButton(); ri.setText(text); eventually: ri.setIcon(icon); ri.setSelected(true or false); p.add(ri); initially, we should select either no button or just one button (if we select more than one button, then only the first one will actually be selected)

Then, group all radio buttons ri into a group of type ButtonGroup: g = new ButtonGroup(); g.add(r1); . . . . . . g.add(rn); the current state of radio button ri is returned by ri.isSelected()

text field: JTextField class one editable line of text at the end of which we type Return typing Return will trigger an event Return will NOT be represented by a newline character at the end of the string text this is a line of text currently edited

create the textfield object: textfield = new JTextField(); or textfield = new JTextField(desired number of characters); set the font of the text field: textfield.setFont(font); eventually, set an initial text: textfield.setText(text); obtain the modified text: textfield.getText() returns the current text select all the text: textfield.selectAll();

text area: JTextArea class several editable lines of text in a rectangular area corresponding to a certain number of rows and of columns line wrapping: when we arrive at the end of a row, the text line is wrapped onto the next row This is a full text currently edited; the number of rows is 4 and the number of columns is 29

create the textarea object: textarea = new JTextArea( desired number of rows , desired number of columns); set the font of the text area: textarea.setFont(font); turn line wrapping on: textarea.setLineWrap(true); enforce wrap between words: textarea.setWrapStyleWord(true); eventually, set an initial text: textarea.setText( row1 string + row2 string + …. + rown string );

obtain the modified text: textarea.getText() returns the current text select all the text: textarea.selectAll();

Scrollable text area: encapsulate the text area inside a scroll pane of type JScrollPane: scrollpane = new JScrollPane(textarea , vertical scrollbar policy , horizontal scrollbar policy); the scrollbar policies are defined as: vertical scrollbar policy = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED or JScrollPane.VERTICAL_SCROLLBAR_ALWAYS or JScrollPane.VERTICAL_SCROLLBAR_NEVER horizontal scrollbar policy = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED or JScrollPane. HORIZONTAL_SCROLLBAR_ALWAYS JScrollPane. HORIZONTAL_SCROLLBAR_NEVER then, add the scroll pane to the container if you have turned lined wrapping on, the horizontal scroll bar will never become active...

combo box: JComboBox class editable popup menu whose choices are some strings (stored in an array) we can select a choice string (it creates an event object) we can edit the selected choice string (as in a text field) (typing Return also creates an event object) choice n.1 folded combo box choice n.2 choice n.0 choice n.3 unfolded combo box selected choice

create the combo box object: combobox = new JComboBox(); or combobox = new JComboBox(array of strings); authorize the selected string to be edited: combobox.setEditable(true); select a certain choice string by specifying its index (starts from 0): combobox.setSelectedIndex(index); insert a new choice string at index position: combobox.insertItemAt(string , index); obtain the selected string: (String )combobox.getSelectedItem()

list: Jlist class create an instance of class JList: aaa bbb uuu ccc zzz www create an instance of class JList: list = new JList(array of strings); set the newly selected index: list.setSelectedIndex(index);

obtain the currently selected index list obtain the currently selected index list.getSelectedIndex() returns index test if there is no selection: list.isSelectionEmpty() returns true or false forces all strings to be non selected: list.clearSelection();

file chooser: JFileChooser class a.jpg b.gif u.jpg c.jpg z.gif w.jpg instruc. cancel All Files (*.*) File name: Look in: directory create an instance of class JFileChooser  with initial selection of the user's home directory: fc = new JFileChooser();  with initial selection of a given directory: fc = new JFileChooser(directory_name_string);

show the dialog box (related to frame): fc.showDialog(frame , instruction_string) returns value JFileChooser . APPROVE_OPTION or JFileChooser . CANCEL_OPTION or JFileChooser . ERROR_OPTION obtain the selected file: fc.getSelectedFile() returns an instance of File fc.getSelectedFile().getPath() returns a string = full path of the file

slider: JSlider class cursor that the operator can slide with the mouse along a (graduated) rule the value currently pointed at by the cursor can be read by the program the minimum value, maximum value and current value are all integers (int) major tick 6 12 18 24 minor tick current value pointed at by the cursor major interval minor interval

create the slider object: slider = new JSlider( JSlider.HORIZONTAL or JSlider.VERTICAL , min , max , init_value); modify the min, max and current values: slider.setMinimum(min); slider.setMaximum(max); slider.setValue(value);

by default, max value is at right if horizontal slider, at top if vertical slider  to have it at left if horizontal slider, at bottom if vertical slider: slider.setInverted(true); establish major and minor ticks according to some intervals: slider.setMajorTickSpacing(major_interval); slider.setMinorTickSpacing(minor_interval); display the ticks: slider.setPaintTicks(true); display the values at each major tick: slider.setPaintLabels(true); obtain the current value of the slider: slider.getValue()

Events created by containers and atomic components

the GUI managing thread implements the response to an event by calling a previously defined event method such event methods must be executed very fast so as not to delay the GUI managing thread

event source, event, event listener: event source object: frame applet panel button menu item check box radio button text field combo box list slider

 creates an event object of a certain type ActionEvent ChangeEvent FocusEvent KeyEvent MouseEvent ComponentEvent WindowEvent

 one or several event listener objects(attached to the event source object) respond to the corresponding type of event object by calling some specific event methods ActionListener ChangeListener FocusAdapter KeyAdapter MouseAdapter MouseMotionAdapter ComponentAdapter WindowAdapter

an event listener responding to an event of type X has for name XListener or Xadapter use Xadapter if you want to define only one of the possible event methods associated to event X

defining an event listener: an event listener is an object which contains the definition of one (or several) event method that event method receives the event object as argument = defined and created as an anonymous class which embeds the event method(s) reason why: no function pointer as in C…

define an event listener of events of type X with an event method and attach it to a source event object s: s.addXListener(new XListener() { public void event_method(XEvent e) { . . . . . . } } );

or, if there are several possible event methods associated to this type of event: s.addXListener(new XAdapter() { public void event_method(XEvent e) { . . . . . . } } ); inside the event methods of the event listeners, we can address with no restriction at all (no need of final…) the elements of the GUI = they are instance variables of the class of the GUI and not local variables or arguments of constructor Gui()

Example button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println( "button1 clicked"); } } ); panel1.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out.println( "mouse clicked inside the panel1"); } } );

table of events and associated event listeners and event methods: possible event sources event object information on event object e Attach a listener to the event source listener of the event object possible event methods button (if clicked) menu item (if selected) check box (if clicked) radio button (if clicked) text field (if Return typed) combo box (if item selected or Return typed) ActionEvent addActionListener ActionListener actionPerformed slider (if its cursor is being moved) ChangeEvent addChangeListener Change -Listener stateChanged list (if selection in the list changes) ListSelectionEvent addListSelection ListSelection valueChanged

frame (if keyboard focus applet is changed) FocusEvent addFocusListener FocusAdapter focusGained focusLost frame (if keyboard touched applet while frame or applet has keyboard focus) KeyEvent e.getKeyChar() addKeyListener KeyAdapter keyTyped (character key) e.getKeyCode() (KeyEvent.VK_SHIFT , KeyEvent.VK_UP) keyPressed (action or keyReleased modifier key)

panel (if mouse cursor is frame inside panel, frame applet or applet) MouseEvent (recommended for use with panel) e.getX() e.getY() e.getClickCount() e.getButton() (MouseEvent.BUTTON1 , MouseEvent.BUTTON2 , MouseEvent.BUTTON3) addMouseListener MouseAdapter mouseClicked mousePressed mouseReleased mouseEntered (the event mouseExited source) addMouseMotion -Listener MouseMotion -Adapter mouseDragged mouseMoved

frame applet ComponentEvent addComponent -Listener Component -Adapter componentMoved componentResized WindowEvent AddWindow Window windowClosed windowIconified windowDeiconified