Object–oriented programming Graphic interfaces

Marcin Młotkowski

11th April, 2012

Programmer’s life Swing Traps and solutions

Outline

1

Programmer’s life

2

Swing Windows and widgets Event handling

3

Traps and solutions Programmer’s life, part 2 Model–View–Controller

Marcin Młotkowski

Object–oriented programming

2 / 41

Programmer’s life Swing Traps and solutions

1st stage

Specification Implement a system for libraries.

Marcin Młotkowski

Object–oriented programming

3 / 41

Programmer’s life Swing Traps and solutions

A part of implementation public class Book { private String title; private String author; private int edition; public Book(String t, String a, int w) { this.title = t; this.author = a; this.edition = w; } public String toString() { return "ook " + this.title + " " + this.author; } } Marcin Młotkowski

Object–oriented programming

4 / 41

Programmer’s life Swing Traps and solutions

2nd stage

Extended specification "I need an editor in window!"

Marcin Młotkowski

Object–oriented programming

5 / 41

Programmer’s life Swing Traps and solutions

Java implementation public class Book { private String title; private String author; private int edition; public Book(String t, String a, int w) { ... } public String toString() { ... } public void Edit() { ... } }

Marcin Młotkowski

Object–oriented programming

6 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Outline

1

Programmer’s life

2

Swing Windows and widgets Event handling

3

Traps and solutions Programmer’s life, part 2 Model–View–Controller

Marcin Młotkowski

Object–oriented programming

7 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Introduction to Swing

Overview better version of AWT (Abstract Window Toolkit); independence from view and operating system; support for internationalization. Heavy usage of classes, objects, and inheritance.

Marcin Młotkowski

Object–oriented programming

8 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Components of Swing

"Visual" elements Windows, widgets, menus. "Invisible" elements Events, listeners.

Marcin Młotkowski

Object–oriented programming

9 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Where is Swing?

import javax.swing.*; import java.awt.*; import java.awt.event.*;

Marcin Młotkowski

Object–oriented programming

10 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Class hierarchy javax.swing.* JComponent

JLabel

Frame

JTextComponent JFrame

JTextField

Marcin Młotkowski

Object–oriented programming

11 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Rapid solution

Marcin Młotkowski

Object–oriented programming

12 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Main window

JFrame frame = new JFrame("Edit book"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Marcin Młotkowski

Object–oriented programming

13 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Main window

JFrame frame = new JFrame("Edit book"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container container = frame.getContentPane(); GridLayout layout = new GridLayout(4, 2); container.setLayout(layout);

Marcin Młotkowski

Object–oriented programming

13 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Author

JLabel author_label = new JLabel("Author"); container.add(author_label); JTextField author = new JTextField(this.author, 40); container.add(author);

Marcin Młotkowski

Object–oriented programming

14 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Title

JLabel title_label = new JLabel("Title"); container.add(title_label); JTextField title = new JTextField(this.title, 40); container.add(title);

Marcin Młotkowski

Object–oriented programming

15 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Edition

JLabel wydanie_label = new JLabel("Edition"); container.add(wydanie_label); JTextField edition = new JTextField(Integer.toString(this.edition), 40); container.add(edition);

Marcin Młotkowski

Object–oriented programming

16 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Save button

JButton b = new JButton("Save"); b.addActionListener(this); container.add(b);

Marcin Młotkowski

Object–oriented programming

17 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

End of window building

frame.pack(); frame.setVisible(true);

Marcin Młotkowski

Object–oriented programming

18 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Clicking response

public class Book implements ActionListener { public void actionPerformed(ActionEvent e) { // Eg. take data from widget: JTextfield.getText();

} public void Edit() { b.addActionListener(this); } }

Marcin Młotkowski

Object–oriented programming

19 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Result

Marcin Młotkowski

Object–oriented programming

20 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Events

Event Clicking a mouse button, pressing a key, timeout, closing a window etc. Widget A widget connected to the event, eg. key pressed. Event handling A method executed after an event.

Marcin Młotkowski

Object–oriented programming

21 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Event subscription in Swing

A widget which "receives an event" Each widget has a list of classes, which implements an interface ActionListener. After event occurrence, each listener is notified of the event. Listener Must implement a method actionPerformed(ActionEvent), which is invoked after an event.

Marcin Młotkowski

Object–oriented programming

22 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Implementation public class Book implements ActionListener { public void actionPerformed(ActionEvent e) { ... } public void Edit() { ... JButton b = new JButton("Save"); b.addActionListener(this); ... } }

Marcin Młotkowski

Object–oriented programming

23 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Listeners, summary

The object may register any number of Listeners; each listener is notified of the event; Listener implements interface EventListener and derivates, including ActionListener.

Marcin Młotkowski

Object–oriented programming

24 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Listener implementation, 1st approach

public class Book implements ActionListener { b.addActionListener(this); }

Marcin Młotkowski

Object–oriented programming

25 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Listener implementation, 2nd approach

public class Book { class MyListener implements ActionListener { public void actionPerformed(ActionEvent e) { ... } } ... b.addActionListener(new MyListener()); ... }

Marcin Młotkowski

Object–oriented programming

26 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Listener implementation, 3rd approach

b.ActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { ... } } );

Marcin Młotkowski

Object–oriented programming

27 / 41

Programmer’s life Swing Traps and solutions

Windows and widgets Event handling

Anonymous class

could be defined "in the fly"; no need to think of a name of the class; anonymous class has an access to the fields of the "larger" class.

Marcin Młotkowski

Object–oriented programming

28 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Outline

1

Programmer’s life

2

Swing Windows and widgets Event handling

3

Traps and solutions Programmer’s life, part 2 Model–View–Controller

Marcin Młotkowski

Object–oriented programming

29 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Extension of our application

" I need it on my mobile phone, palmtop etc"

Marcin Młotkowski

Object–oriented programming

30 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Extension of our application

" I need it on my mobile phone, palmtop etc" Implementation public class Book { public void Edit() { ... } public void EditPhoneVersion() { ... } public void EditPalmtopVersion() { ... } }

Marcin Młotkowski

Object–oriented programming

30 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Result

"core" implementation of Book: 15 lines; implementation of edition (Swing only): 40 lines

Marcin Młotkowski

Object–oriented programming

31 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Is Book a good name?

Marcin Młotkowski

Object–oriented programming

32 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

What is inside the class Book?

implementation a notion of a book; implementation of the interaction with a user

Marcin Młotkowski

Object–oriented programming

33 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Design principle

Single responsibility principle Each class/object should have a single responsibility.

Marcin Młotkowski

Object–oriented programming

34 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Introduction

Design pattern MVC: Model–View–Controller one of the first design patterns; designed for GUI implementations.

Marcin Młotkowski

Object–oriented programming

35 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Interaction with environment

Book

Edit book

Marcin Młotkowski

- author: String - title: String - edition: int

Object–oriented programming

36 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

MVC

Model Data with relations among data View Data view (screen, printer, html) Controller "Something" what modifies a state of Model (eg. buttons).

Marcin Młotkowski

Object–oriented programming

37 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Cooperations

A controller has a reference to a Model and as a reaction to user action (pressing a key), call methods of Model which modifies a state of Model. View (or Views) listen changes of Model and update thier views.

Marcin Młotkowski

Object–oriented programming

38 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

MVC in Swing

Book

Edit book

Marcin Młotkowski

- author: String - title: String - edition: int

Object–oriented programming

39 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Code improvement

Split the previous class into two classes: public class Book { ... }

public class BookSwing { public BookSwing(Book k) { ... } }

Marcin Młotkowski

Object–oriented programming

40 / 41

Programmer’s life Swing Traps and solutions

Programmer’s life, part 2 Model–View–Controller

Summary

Splitting class into more classes (using responsibility) results in more natural components. MVC allow independent implementation of a model ant its graphic interfaces.

Marcin Młotkowski

Object–oriented programming

41 / 41