Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.

Similar presentations


Presentation on theme: "Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering."— Presentation transcript:

1 Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering & IT Software Engineering Department WWW.PALINFONET.COM Eng.Omar Al-Nahal Eng.Omar Al-Nahal

2 2 Event-Driven Programming b Programs with GUIs must respond to events, generated by GUI components, that indicate that specific actions have occurred. b A special category of classes, called listeners, wait for events to occur. b Therefore, a GUI program is composed of: the code that presents the GUI to the userthe code that presents the GUI to the user the listeners that wait for events to occurthe listeners that wait for events to occur the specific code that is executed when events occur.the specific code that is executed when events occur.

3 3 Event-Driven Programming b b There is a listener interface defined for each event type b b Each listener interface contains the abstract methods required to respond to specific events b b A listener class implements a particular listener interface b b Listeners are "added" to a particular GUI component b b When a component generates an event, the method corresponding to that event is executed in the listener

4 4 The GUI Program Model Listeners Program- specific GUI Event effects Add listeners Handle events

5 Common GUI Event Types and Listener Interfaces For each event-object type, there is typically a corresponding event- listener interface. An event listener for a GUI event is an object of a class that implements one or more of the event-listener interfaces from packages java.awt.event & javax.swing.event. Many of the event-listener types are common to both Swing and AWT components. Such types are declared in package java.awt.event To Make an Interactive GUI Program To make an interactive GUI program, you need: Components - buttons, windows, menus, etc. Events - mouse clicked, window closed, button clicked, etc. and event handlers (methods) Event listeners (interfaces) and event handlers (methods) - listen for events to be trigged, and then perform actions to handle them.

6 Event-Handling Model (1) ActionEvent AdjustmentEvent TextEvent ComponentEvent ItemEvent ContainerEvent FocusEvent PaintEvent WindowEvent InputEvent KeyEvent MouseEvent CLASS some event classes in package java.awt.event

7 Event-Handling Model (2) b b Three parts of the event-handling mechanism event source: the GUI component with which the user interacts event object: encapsulated information about the occurred event event listener: an object which is notified by the event source when an event occurs, and provides responds to the event b b The programmer must perform two tasks to process a GUI event 1.register an event listener An object of a class that implements one or more of the event-listener interfaces from packages java.awt.event and javax.swing.event 2.implement an event handling method Event-listener interface of package java.awt.event

8 8 Lists b b A list, in the Java GUI sense, is used to display a list selectable strings. b b A list component can contain any number of strings and can be instantiated to allow multiple selections within it. b b The size of the list is specified by the number of visible rows or strings within it. b b A scrollbar will automatically appear on the right side of a list if the number of items exceed the visible area.   A list is defined by the List class See List.java

9 9 Scrollbars b A scrollbar is a slider that indicates a relative position or quantity. b They are automatic on text areas and list components, but can be used independently. b The position of the slider in the range corresponds to a particular numeric value in a range associated with the scrollbar.  A scrollbar is defined by the Scrollbar class

10 10 Layout Managers   There are five predefined layout managers in the java.awt package: Flow layout Border layout Grid layout Card layout Grid bag layout b b Each container has a particular layout manager associated with it by default. b b A programmer can also create custom layout managers

11 11 Flow Layout b b Components are placed in a row from left to right in the order in which they are added. b b A new row is started when no more components can fit in the current row. b b The components are centered in each row by default. b b The programmer can specify the size of both the vertical and horizontal gaps between the components. b b Flow layout is the default layout for panels and applets See Flow.java

12 12 Grid Layout b b Components are placed in a grid with a user-specified number of columns and rows b b Each component occupies exactly one grid cell b b Grid cells are filled left to right and top to bottom b b All cells in the grid are the same size b Defines five locations each of which a component or components can be added North, South, East, West, and Center Border Layout See Grid.java See Border.java

13 13 Border Layout North South West East Center Sample: Color, ShowColor, Font, Menu212,Color ShowColorFontMenu212 Painter, PanelDemo, LinesRectsOvals, PainterPanelDemoLinesRectsOvals LookAndFeelDemo, BoxLayoutDemo, JDesktopPaneLookAndFeelDemo BoxLayoutDemoJDesktopPane

14 For More InformationSupplemental Reading For More Information - Supplemental Reading http://java.sun.com/docs/books/tutorial/ui/features/components.html http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComponent.html http://java.sun.com/developer/onlineTraining/new2java/divelog/part1/ http://java.sun.com/developer/onlineTraining/new2java/divelog/part2/index.jsp http://java.sun.com/developer/onlineTraining/new2java/divelog/part3/ http://java.sun.com/developer/onlineTraining/new2java/divelog/part4/ http://java.sun.com/developer/onlineTraining/new2java/divelog/part5 WWW.PALINFONET.COM


Download ppt "Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering."

Similar presentations


Ads by Google