Results 1 to 6 of 6
  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation Observer/Observable help...

    Greetings,

    I have been studying how Observer/Observable can be implemented on Model-View-Controller pattern. Here's what I have done so far...

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Observable;
    import java.util.Observer;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class Testing implements Observer {
    
        public Testing() {
            model.addObserver(this);
            loadListener1();        
        }
    
        private void loadListener1() {
            view1.submitButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    model = new Model(view1.data1Field.getText(), view1.data2Field.getText());
                    model.makeChange();
                    model.notifyObservers();
                }
            });
        }  
    
        public void update(Observable o, Object arg) {
            System.out.println("Notify - Observer");
        }
    
        public static void main(String[] a) {
            Testing testing = new Testing();
        }
    
        private View view1 = new View("1");    
        private Model model = new Model();
    
    }
    
    class View extends JFrame  {
    
        public View(String frame) {
            super("Frame: " + frame);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300, 300);
            setVisible(true);
            setLayout(new GridLayout(3, 2));
            add(data1Label); add(data1Field);
            add(data2Label); add(data2Field);
            add(submitButton); add(cancelButton);
        }
    
        private final JLabel data1Label = new JLabel("Data1");
        private final JLabel data2Label = new JLabel("Data2");
        public final JTextField data1Field = new JTextField();
        public final JTextField data2Field = new JTextField();
        public final JButton submitButton = new JButton("Submit");
        public final JButton cancelButton = new JButton("Cancel");
    
    }
    
    class Model extends Observable {
    
        public Model() {
        }
    
        public Model(String data1, String data2) {
            setData1(data1);
            setData2(data2);
        }
    
        public String getData1() {
            return data1;
        }
    
        public final void setData1(String data1) {
            this.data1 = data1;
        }
    
        public String getData2() {
            return data2;
        }
    
        public final void setData2(String data2) {
            this.data2 = data2;
        }
    
        public void makeChange() {
            setChanged();
            notifyObservers();
            System.out.println("Notify - Observable");
        }
    
        private String data1;
        private String data2;
    
    }
    I can't get the update method of Observer working.

    Can you guide me thru?
    Your reply is highly appreciated.

    Thanks,
    Cyril H.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hey, please don't cross-post without telling us. This is considered very rude behavior on this, and most other programming fora. To see why this is a problem, please look at this link: JavaRanch: BeForthrightWhenCrossPostingToOtherSites


    To fix this, please provide for us links to all cross-posts, and please do so in a reasonable time frame.
    Last edited by Fubarable; 01-14-2011 at 05:22 PM.

  4. #4
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    Apologies for cross-posting this thread.
    I can't get the update method of Testing running.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    This thread is solved on the other link. I apologize for the cross-post.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Similar Threads

  1. Observable/Observer JComboBox
    By rudeboy in forum Advanced Java
    Replies: 5
    Last Post: 11-27-2010, 07:03 AM
  2. Pattern Observer
    By brontolo in forum Advanced Java
    Replies: 6
    Last Post: 09-02-2010, 03:40 PM
  3. Problem with observer pattern
    By livnihai in forum New To Java
    Replies: 5
    Last Post: 10-11-2009, 03:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •