 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
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.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Graphical User Interfaces
Object-Oriented Analysis and Design
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.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/16) Internationalization and Locales Joel Adams and Jeremy Frens Calvin.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
GUI programming Graphical user interface-based programming.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/14) Object Oriented Programming Joel Adams and Jeremy Frens Calvin.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/20) More Swing Widgets and Listeners Joel Adams and Jeremy Frens Calvin.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
For (int i = 1; i
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Graphics in Java Dr. Andrew Wallace PhD BEng(hons) EurIng
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Review_6 AWT, Swing, ActionListener, and Graphics.
GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
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.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
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:
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©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
Lecture 15 Basic GUI programming
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
JButton Chapter 11 Lecture 3.
Web Design & Development Lecture 11
A First Look at GUI Applications
A Quick Java Swing Tutorial
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Graphical user interface-based programming
A Quick Java Swing Tutorial
Constructors, GUI’s(Using Swing) and ActionListner
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/15) MVC and Swing Joel Adams and Jeremy Frens Calvin College

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(2/15) Review: The Java Class Hierarchy We’ve seen that Java provides a unified class hierarchy… Almost everything in Java is ultimately derived from the Object class… Object Component Container JComponent AbstractButton JButton Number DoubleIntegerLong Throwable ErrorException IO Exception Runtime Exception

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(3/15) Java User Interface Widgets Original Java had the awt package of user-interface widgets: Java 1.2 added the swing package of lighter-weight widgets swing package Object Component ButtonCanvasCheckboxScrollbar … Container PanelScrollPaneWindow … JComponent AbstractButton JButton JFile Chooser JLabelJPanelJScrollPaneJTabbedPane …

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(4/15) Java strongly encourages UI designers to use the model-view- controller (MVC) design pattern, in which: Designing User Interfaces The application’s core functionality is isolated in a model class: This pattern lets you modify the UI (or add additional ones) without touching the app’s core functionality. The application’s user-interface is isolated in a view class: The application’s control is isolated in a controller class: Model Object View JFrame Controller i View i Controller Listener

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(5/15) Example Let’s build an app with a background-color-changing button. Basic Design: Sample Model Object Sample View JFrame Sample Controller Action Listener  SampleView provides the UI, and a method to modify its background  SampleModel provides the new background color  SampleController acts as mediator between them To change background Push Me SampleController

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(6/15) Example: SampleModel SampleModel is the simplest of our three classes: import java.awt.Color; A model “knows” nothing about the classes that use it… public class SampleModel { } private Color [] myColors = {Color.white, Color.red, Color.green, Color.blue}; private int myIndex = 0; public SampleModel() { } // nothing to do public Color getNextColor() { myIndex = (myIndex+1) % myColors.length; return myColors[myIndex]; }

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(7/15) Example: SampleView … defines instance variables for the GUI’s widgets: import java.awt.Color; import java.awt.event.ActionListener; import javax.swing.*; // JLabel, JButton,... public class SampleView extends JFrame {... private JPanel myPane; private JButton myButton; We could also define a JLabel instance variable for our “To change background” label, but we’ll avoid that for now

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(8/15) Example: SampleView (ii) The constructor actually builds the GUI: Our class only needs handles to GUI widgets if it needs to access them after they have been created… … public SampleView() { myPane = new JPanel(); myPane.add(new JLabel("To change background color")); myButton = new JButton("Push me"); myPane.add(myButton); this.setContentPane(myPane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }...

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(9/15) Example: SampleView (iii) Pressing a JButton triggers an ActionEvent object, that is usually handled by an ActionListener object… What to do in response is the responsibility of our controller, so our view needs to provide a method to let its controller register itself as the ActionListener for our JButton :... public void setButtonActionListener(ActionListener al) { myButton.addActionListener(al); }... The API The API tells us the messages we can send to a JButton …

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(10/15) Example: SampleView (iv) Since our GUI has to let its controller change its background color, we need a method to do this…... public void setBackgroundColor(Color c) { myPane.setBackground(c); } } // end of class SampleView The API The API tells us the messages we can send to a JPanel … Given a Model and View, we just need a Controller…

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(11/15) Example: SampleController To handle a JButton ’s ActionEvent, a controller must implement the ActionListener interface: import java.awt.Color; import java.awt.event.ActionListener; public class SampleController implements ActionListener {... private SampleModel myModel; private SampleView myView; In our design, our controller has-a model and has-a view…

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(12/15) Example: SampleController (ii) Its constructor initializes its two components: … public SampleController() { myModel = new SampleModel(); myView = new SampleView(); myView.setButtonActionListener(this); myView.pack(); // or myView.setSize(w, h); myView.setVisible(true); }... Note that our controller uses our view’s method to register itself as the ActionListener for the view’s JButton.

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(13/15) Example: SampleController (iii) To implement the ActionListener interface, our controller must define method actionPerformed() … public void actionPerformed(ActionEvent ae) { Color c = myModel.getNextColor(); myView.setBackgroundColor(c); }... Each press of the JButton in our view, the JRE sends the actionPerformed() message to that button’s ActionListener (i.e., our controller), causing this method to be executed.

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(14/15) Example: SampleController (iv) Finally, our controller must define the main() method so that execution begins with itself: … public static void main(String [] args) { SampleController sc = new SampleController(); } } // end of class SampleController Now, when we run our app: java SampleController our main() method creates an instance of the controller, which creates instances of the model and view, and then begins handling user-events (clicks on the JButton ).

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(15/15) Exercise Go to today’s exercise and complete parts I, II, and III