Results 1 to 7 of 7
  1. #1
    dumb_ass is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default How can I pass a boolean to notifyObservers()

    Hi everyone, I'm having a problem passing a Boolean object to notifyObservers() method.

    Java Code:
    class Model extends Observable
    {
       .....
       private Boolean booleanValue=new Boolean(false);
       .....
       .....
       public void setBooleanValue()
       {
            this.booleanValue=true
            setChanged();
            notifyObservers(this,this.booleanValue);
       }
    }
    
    class View implements Observer
    {
         .....
         public void update(Observable o,Object arg)
         {
             //do something
         }
    }
    From my understandings, whenever the method notifyObservers() is invoked , update() method will be called.
    I've no idea why I can't pass the booleanValue to notifyObservers() to make changes in update(). After all, it's just an object, isn't it ? Can someone help me out ?? Thanks everyone in advance.
    Last edited by dumb_ass; 03-04-2009 at 07:19 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    notifyObservers() is a method implemented by instances of Observable.

    In fact there are two such methods: one takes no arguments, the other takes a single Object argument.

    Perhaps you mean

    Java Code:
      /**
       * Sets the boolean value of the model to a given value and notifies observers.
       */
    public void setBooleanValue(boolean b)
    {
        booleanValue = b;
        notifyObservers(booleanValue);
    }
    But that's only a guess. What is the setBooleanValue() method supposed to do?

  3. #3
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    what do you mean by passing a boolean value?
    w/ a method from the class View?

    you can pass a boolean value if your method return a boolean value

    for example

    Java Code:
    public boolean isScorePass(int score) {
         
         if (score >= 75) {
              return true;
         } else {
             return false;
         }
      
    }
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    notifyObservers(Object, Object) is not a member of the Observable class, soi I'm guessing it's part of your Model class?

    If so, look at that method to try to figure out what's wrong.

  5. #5
    dumb_ass is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    Sorry , if I made all of ya guys confused. I'm a total noob and I start working on MVC in java and having a huge huge confusion. The program is, I put a button in the View and its state in the Model. If the button got pressed in View then controller class call setButtonPressed() and change its state in the Model and then Model update all of its observers . The problem is even though I've clicked the button , I've no idea how to get the Boolean value back in the update() method.

    Java Code:
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Observable;
    import java.util.Observer;
    
    class Model extends Observable
    {
        //the button hasn't been pressed
        private Boolean buttonPressed=new Boolean(false);
    
        //return the button state whether it's been pressed or not
        public Boolean getButtonPressed()
        {
            return this.buttonPressed;
        }
    
        //once the button has been pressed the state changes
        public void setButtonPressed(Boolean state)
        {
            this.buttonPressed=state;
            setChanged();
            notifyObservers(this.buttonPressed);
        }
    }
    class View implements Observer
    {
    
        private JButton aButton=new JButton("Click me");
        private Model model;
        private Controller controller;
    
        public View(Model model ,Controller controller)
        {
            this.model=model;
            this.controller=controller;
        }
        public void display()
        {
            JFrame window=new JFrame("Window");
            controller=new Controller(this.model);
            this.aButton.addActionListener(controller);
            this.model.addObserver(this);
            this.aButton.setActionCommand("clicked");
            window.add(this.aButton);
            window.pack();
            window.setVisible(true);
        }
        public void update(Observable o, Object arg)
        {
           
          // I'm trying to get the result back from the Model which is "true" boolean value but i dunno how i can do it
        }
    
    }
    class Controller implements ActionListener
    {
        private Model model;
    
        public Controller()
        {
            
        }
        public Controller(Model model)
        {
            this.model=model;
        }
    
        public void actionPerformed(ActionEvent e)
        {
            if("clicked".equals(e.getActionCommand()))
            {
                this.model.setButtonPressed(true);
            }
        }
    }
    public class Main
    {
        public static void main(String args[])
        {
            Model model=new Model();
            Controller controller=new Controller();
            View view=new View(model,controller);
            view.display();
    
        }
    }
    Last edited by dumb_ass; 03-04-2009 at 08:52 AM.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    As far as I can tell, it's already working. Maybe you're unfamiliar with the idea of casting? Try this...

    Java Code:
       public void update(Observable o, Object arg)
        {
    
          // I'm trying to get the result back from the Model which is "true" boolean value but i dunno how i can do it
    
    	//like this
    	Boolean thisIsYourAnswer = (Boolean) arg;
           	System.out.println("Yep, it's true, see --> "+thisIsYourAnswer);
        }

  7. #7
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    Your code is a completely working
    small scale MVC example.

    It does the following:
    Visually, the view displays a button
    within a frame. When the button is
    pressed, it records this status in
    the model's variable:

    private Boolean buttonPressed;

    Your problems is how to display this
    status to the outside world. You have
    correctly identified the method that
    requires code to do this.

    public void update(Observable o, Object arg){
    // I'm trying to get the result back from the Model
    // which is "true" boolean value but i dunno how i
    // can do it
    }

    You do not seem to realize that the
    buttonPressed value, and its source
    is being automatically delivered to
    the "view" by way of:

    notifyObservers(Object arg) // in the Model
    update(Observable o, Object arg){ // in the View

    These methods work hand-in-hand.
    HERE IS HOW YOU SHOULD INTERPRETE THE update() METHOD

    public void update(model, model.buttonPressed){
    // I'm trying to get the result back from the Model
    // which is "true" boolean value but i dunno how i
    // can do it
    }

    You can ignore the FACT that this
    method is delivering the
    buttonPressed object, and its source,
    or you can consider it in the code
    you design for your answer.
    You did not specify a particular motif.
    The answer you need can be
    provided by a single line of code
    within your update() method.

Similar Threads

  1. im not familiar with boolean in method...
    By PureAwesomeness in forum New To Java
    Replies: 19
    Last Post: 02-22-2009, 03:36 AM
  2. Simple Boolean
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 06:01 AM
  3. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 06:31 AM
  4. boolean variables
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-31-2007, 05:58 AM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 05:11 AM

Posting Permissions

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