Results 1 to 7 of 7
- 03-04-2009, 05:42 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
How can I pass a boolean to notifyObservers()
Hi everyone, I'm having a problem passing a Boolean object to notifyObservers() method.
From my understandings, whenever the method notifyObservers() is invoked , update() method will be called.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 } }
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 06:19 AM.
- 03-04-2009, 06:09 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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
But that's only a guess. What is the setBooleanValue() method supposed to do?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); }
- 03-04-2009, 06:21 AM #3
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
- 03-04-2009, 06:38 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 03-04-2009, 07:41 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
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 07:52 AM.
- 03-05-2009, 06:31 AM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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); }
- 03-05-2009, 08:15 PM #7
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
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
-
im not familiar with boolean in method...
By PureAwesomeness in forum New To JavaReplies: 19Last Post: 02-22-2009, 02:36 AM -
Simple Boolean
By jigglywiggly in forum New To JavaReplies: 3Last Post: 01-01-2009, 05:01 AM -
boolean to string
By otoro_java in forum New To JavaReplies: 2Last Post: 01-30-2008, 05:31 AM -
boolean variables
By ravian in forum New To JavaReplies: 3Last Post: 12-31-2007, 04:58 AM -
Boolean Expression
By ritwik07 in forum New To JavaReplies: 3Last Post: 07-11-2007, 04:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks