This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "MVC" Watch "MVC" New topic
Author

MVC

Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
Hello I am trying to write a program where im using an MVC pattern but im having problems with my action listener, i want to when i press the button the action listener picks up that is what a dice object pressed and then call a method in the model which takes a dice object as a parameter.
in my viewClass i have this code

the dice creates a single dice object from a class which is called dice.

in my Controller i have the code


in my model i have this method



the reason in the controller i have .getActionCommand().equals("") is because when i press the dice button i dont seem to get anything. i was wondering if there was a way of representing the dice.so i could then pass it to the rollDice method
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Are the dice the GUI components? If so, they should be the ActionEvent's source.


Does this help, or does it confuse the matters even more?


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
Ahh that bit works but when i add it to my actionPerformed in my Controller



i get this message
Controller.java:19: non-static method rollDice(Dice) cannot be referenced from a static context

this happens when i put in the viewClass.rollDice(d); part
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What is viewClass? Is it a class? This would explain why you are getting this error. (As a side note, you should name it ViewClass instead. The Sun coding conventions suggest that class names start with upper-case; variable and function names should start with lower-case.) If viewClass is a class, then it looks like you need to either change rollDice() to a static method or you need to have a instance of the class in order to call the method on. Without more code, I'm not sure which would be appropriate. Perhaps you can post the portions of code from viewClass that are pertinent here. In particular, the "class viewClass" line (with possible "extends"), the rollDice() method, and declarations for all variables used in rollDice() will help us understand what is going on.

HTH

Layne


Java API Documentation
The Java Tutorial
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
making the method rollDice static and the variable numberOfRolls static worked, Thanks alot *smiles*
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It may have "worked", but you should also consider if that is a good design decision. Typically, making design decisions just to make the code work is a poor choice. Sorry if I seem a little skeptical. I just don't know enough about the code to determine if using a static method is necessarily the best choice to fix your code.

Anyway, I'm glad you got it working.

Layne
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
I Understand what you mean, and you've helped me to understand how to deal with that error thank you, I have just one more small question which is probably something stupid.
To make my code better in my view class i went from this


to this


this second piece of code compiles but when i try to run it i get

java.lang.IllegalArgumentException: illegal component position
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at viewClass.initializeComponent(viewClass.java:117)
at viewClass.<init>(viewClass.java:72)
at runDice.main(runDice.java:19)

does anyone know why?

[ February 24, 2005: Message edited by: Andy Rayner ]
[ February 24, 2005: Message edited by: Andy Rayner ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MVC