Graphical User Interfaces User interaction problem: User can spontaneously change the value (state) of a user interface object x (e.g., a check box or scrollbar), often called the subject You want a set of observer objects o1, o2, ... — not necessarily GUI objects — to “know about” every change in x’s value Other (non-GUI) situations are similar Solution: ???
Solution #1: Use Polling x o1 o1 Each observer continually (periodically, occasionally) polls the subject to ask if its state has changed Inefficient, cumbersome...
Solution #2: Use Events x x event o1 Each observer goes about its own business, until the subject tells it that its value has changed
Implementing Solution #2: The Observer Pattern Subject expects each observer to register with the subject to indicate its interest in subject’s events Subject keeps track of this (dynamic) set of interested observers When its state changes, subject calls a specific callback method of each registered observer, passing as parameter an event object that describes the state change
Example: Simple GUI Demo interface that any observer of a subject that is a JButton object must implement actionPerformed is the callback method <<interface>> ActionListener + actionPerformed (ActionEvent e) a DemoFrame object in GUIDemo_1 is the observer (it implements ActionListener) a frame is a window on the screen that acts as a container for GUI objects (aka GUI components) a JButton object in DemoFrame is the subject (its events are of interest) JFrame GUIDemo_1 DemoFrame … + actionPerformed (ActionEvent e) DemoFrame JButton holds a reference to a DemoFrame object
A Little More Detail call this method from main to make sure program can be stopped! JFrame … + setDefaultCloseAction (int operation) + show ( ) + getContentPane ( ) : Container + pack ( ) <<interface>> ActionListener + actionPerformed (ActionEvent e) call this method from main to display the frame (window) on the screen call this method from DemoFrame to get the content pane that contains the frame’s GUI components call this method from DemoFrame to organize the GUI components in the frame’s content pane using the layout manager you’ve selected call this method from DemoFrame to add an observer for any subject that is a JButton object DemoFrame … + actionPerformed (ActionEvent e) JButton … + addActionListener (ActionListener o)
This Specific Case of the Observer Pattern A JButton object (the subject) expects each observer to call addActionListener to indicate interest in that JButton’s events Each JButton object keeps track of its own set of ActionListener objects When its state changes, a JButton object calls actionPerformed on each registered ActionListener object, passing as a parameter an ActionEvent object
Java GUI Fundamentals JFrame JTextArea JPanel JButton This is a simple sample application that uses most (if not all) the fatures you’ll need for the calculator application JButton
Java GUI Packages java.awt java.awt.event javax.swing ... These are the main packages for GUI stuff There are others, but these should be the primary places where you should look in the documentation when you need information
Basic Java GUI Widgets (Swing) All in package javax.swing: JFrame JPanel JButton JTextArea JScrollPane ... Use widgets starting with J from the swing library They are more portable, nicer looking, and more powerful than AWT widgets
Layout Managers A layout manager allows you to arrange components inside panels without providing specific coordinates of widgets In packages java.awt, javax.swing: FlowLayout (default for JPanel) BorderLayout (default for JFrame content pane) GridLayout ... Briefly describe each of those listed drawing pictures to explain the difference in how they arrange components