Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review: Java GUI Programming

Similar presentations


Presentation on theme: "Review: Java GUI Programming"— Presentation transcript:

1 Review: Java GUI Programming
R.E. Johnson, Frameworks=(Components + Patterns), CACM, 4(10):39-42, October 1997. J. Elliott, et al., Java Swing, 2nd edition, O’Reilly,

2 Outline Frameworks MVC model Elements of GUI frameworks
GUI components (views and widgets) Layout managers Handling events Lab: Timer applet

3 Object-Oriented Framework
A set of cooperating (abstract) classes and interface that: Represent reusable designs of software systems in a particular application domain, Provide semi-complete applications that can be specialized to produce custom applications, and Defines conventions for extending the abstract classes, implementing the interfaces, and allowing their instances to interact with one another

4 Examples GUI Small Network/Distributed/Web Applets, …
AWT, Swing, SWT (Eclipse) MFC, … Network/Distributed/Web RMI, J2EE, CORBA, Drupal (PHP), …

5 Why Frameworks? Benefits
To support reuse of design and implementations in particular application domains Benefits Quick and easy application development Reliable application (i.e., high confidence) Standardized applications

6 Characteristics of Frameworks
Extensibility Inversion of control Design patterns as building blocks Code written by programmer Library code Application (master) Component (slave) Library-based Framework code Code written by programmer Framework-based

7 Outline Frameworks MVC model Elements of GUI frameworks
GUI components (views and widgets) Layout managers Handling events Lab: Timer applet

8 GUI Framework in General
MVC metaphor for separation of concerns Event-based, implicit invocation style Single vs. multithreading

9 MVC Metaphor A way of cleanly breaking an application into three parts
Model for maintaining data, View for displaying all or a portion of the data, and Controller for handling events that affect the model or views. View Controller Model

10 Separating M from VC View Controller Model changes get data
from model changes model or view View Controller Model update view when data changes

11 Due to Separation … Multiple views and controllers for models.
New views and controllers can be added for the model. Changes in views are localized; such changes have minimal or no impact on the model.

12 Outline Frameworks MVC model Elements of GUI frameworks
GUI components (views and widgets) Layout managers Handling events Lab: Timer applet

13 GUI Framework Classes GUI components (views and widgets)
JButton, JLabel, JTextField, JPanel, … Layout managers FlowLayout, BorderLayout, GridBagLayout, … Events and event listeners ActionEvent, ActionListener, … Graphics, imaging, and other multimedia classes

14 Event Handling Mechanism to write control code Composed of: Event
Event source Event listener (or handler)

15 Event Handling (Cont.) Event Event source
A way for GUI components to communicate with the rest of application Implemented as event classes (e.g., ActionEvent) Event source Components generating events Examples: buttons, check boxes, combo boxes, etc.

16 Event Handling (Cont.) Event listener (or handler)
Objects that receives and processes events Must implement an appropriate listener interface Must inform the source its interest in handling a certain type of events (by registering) May listen to several sources and different types of events

17 Example // create a button JButton button = new JButton(“Start”);
// register an action listener button.addActionListener(new StartButtonActionListener()); // Action listener class class StartButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { // handle the event e … System.out.println(“Start button pressed!”); }

18 Using Anonymous Class // create a button
JButton button = new JButton(“Start”); // register an action listener button.addActionListener(new ActionListener { public void actionPerformed(ActionEvent e) { // handle the event e … System.out.println(“Start button pressed!”); } });

19 Using Lambda Notation (Java 8)
// create a button JButton button = new JButton(“Start”); // register an action listener button.addActionListener(e -> { // handle the event e … System.out.println(“Start button pressed!”); });

20 Example (Cont.) b: JButton h: ActionListener addActionListener(h)
<<create>> e: ActionEvent actionPerformed(e) Observer design pattern getSource()

21 Lab (Pair): Timer Applet
Write a timer applet incrementally (see handout) Use MVC by defining a model class, say TimerModel. init destroy start stop Applet lifecycle public class TimerApplet extends JApplet { private final TimerModel timerModel = new TimerModel(); public void init() { /* configure UI. */ } }


Download ppt "Review: Java GUI Programming"

Similar presentations


Ads by Google