Implementing MVC in Java And Various Other Random Topics - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Implementing MVC in Java And Various Other Random Topics

Description:

in a graphical Java program, this is usually made up of Listeners ... MVC In Java: Observer. interface java.util.Observer. void update(Observable o, Object arg) ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 17
Provided by: tomsla
Category:

less

Transcript and Presenter's Notes

Title: Implementing MVC in Java And Various Other Random Topics


1
Implementing MVC in Java(And Various Other
Random Topics)
2
Plan
  • Quick rehash of MVC
  • How to implement MVC in Java
  • Brief introduction to Timers (something youll
    need)
  • A new GUI component
  • Develop a small Java system using MVC with
    striking parallels to your project

3
MVC Rehash Model
  • maintains state information with methods to
    access and modify that state
  • independent (mostly) of the view and controller
  • we can do things to the model, but the model
    doesnt move itself
  • think of the model as a poseable action figure or
    a lump of clay
  • in a board game the game board, playing pieces,
    etc. are parts of the model

4
MVC Rehash View
  • displays the state of the model in some way
  • should only access the state of the modelit
    should not modify it

5
MVC Rehash Controller
  • what actually makes things happen
  • in a graphical Java program, this is usually made
    up of Listeners
  • in theory, a completely separate module from the
    model and views. in practice, the controller may
    be spread out a little and there may be some
    overlap (some listeners may be in the view class).

6
MVC Example Clock
  • model stores a time and has a way to get and set
    that time
  • views analog and digital displays
  • controller(s) us (we can wind the hands), the
    gears (or whatever) that change the time

7
MVC In Java Observer/Observable
  • model class extends java.util.Observable
  • view classes implement java.util.Observer
    interface
  • Observers get notified when the things they are
    observing are changed (so your view can update
    itself)

8
MVC In Java Observable
  • class java.util.Observable
  • public void addObserver(Observer o)
  • public void deleteObserver(Observer o)
  • public void deleteObservers()
  • public void notifyObservers()
  • public void notifyObservers(Object arg)
  • protected void setChanged()

9
MVC In Java Observer
  • interface java.util.Observer
  • void update(Observable o, Object arg)

10
Random change of topic Timers
  • Motivation
  • how do we animate things on the screen?
  • how do we control the flow of a game?

11
Timers
  • class javax.swing.Timer
  • Timer(int delay, ActionListener al)
  • public void start()
  • public void stop()
  • warning there is also a java.util.Timer. this
    can cause problems if you are importing
    javax.swing. and java.util. in the same file.

12
Timers example
  • import javax.swing. import java.awt.event.
  • public class TimerExample
  • public static void main(String args)
  • new JFrame().show() // just to keep the
    program alive
  • Timer tim new Timer(1000, new
    ActionListener()
  • public void actionPerformed(ActionEvent
    event)
  • JOptionPane.showConfirmDialog(null,
  • "Are we there yet?", "Title",
  • JOptionPane.YES_NO_OPTION,
  • JOptionPane.QUESTION_MESSAGE)
  • )
  • tim.start()

13
A new Component JComboBox
  • JComboBox()
  • JComboBox(Vector items)
  • JComboBox(ComboBoxModel model)Constructs a combo
    box. Can optionally pass a vector or model of
    items. (See DefaultComboBoxModel for a model
    implementation.)
  • addActionListener(ActionListener al)Causes an
    action event to be sent to listener al when the
    user selects or types a new item in the combo
    box.

14
Methods for Managing Items
  • addItem(Object item)
  • Object getItemAt(int index)
  • removeAllItems()
  • removeItem(Object item)
  • removeItemAt(int index)
  • note add and remove are the inherited methods
    from Componentdont confuse them with addItem
    and removeItem

15
Methods for the Selected Item
  • int getSelectedIndex()
  • Object getSelectedItem()
  • setSelectedItem(Object item)
  • setSelectedIndex(int index)
  • setEnabled(boolean enabled)
  • setEditable(boolean editable)If editable, the
    user can type new arbitrary values into the combo
    box.

16
Additional Information
  • Java Tutorial
  • http//java.sun.com/docs/books/tutorial/
  • Java Tutorial Timers
  • http//java.sun.com/docs/books/tutorial/uiswing/mi
    sc/timer.html
  • Java Tutorial Visual Index of Components
  • http//java.sun.com/docs/books/tutorial/uiswing/co
    mponents/
  • components.html
Write a Comment
User Comments (0)
About PowerShow.com