
-----------------------------------
gagster1233
Fri Jan 07, 2011 1:58 pm

Help With Button Pressed
-----------------------------------
i am having trouble getting my number program to work for my grade 11 final project. 

i am trying to get a program to start when a button is pressed and to restart whenever the button is pressed again. 

this is my code so far any help would be greatly appreciated 

 import java.awt.*;
import java.awt.event.*;
import java.util.Random; 
public class ButtonPressDemo
{
    public static void main (String 

-----------------------------------
Tony
Fri Jan 07, 2011 2:30 pm

Re: Help With Button Pressed
-----------------------------------

i am trying to get a program to start when a button is pressed and to restart whenever the button is pressed again. 

And what does the your program do now?

-----------------------------------
copthesaint
Mon Jan 10, 2011 3:12 am

Re: Help With Button Pressed
-----------------------------------
So I asume you want to reset the program to a certain state if you press this button no mater what state the button is in. That should be easy enough.
Let me just CnP your code in to net beans and see what ur doin.

hmm.Now I see what your trying to be, however this just wont work.

ActionListener a = new MyActionListener (); 


Now to explain why this does not work. first of all the ActionListener is just not initialized this way.
The class you are calling "ActionListener" is a blueprint and a is the object that you name the object you are making. Now that you have your "ActionListener" blueprint, you cant just go and build a "MyActionListener" object with the ActionListener blueprint. Those are too different blueprints and the other GUI wont accept your object.
The ActionListener create a listener for a gui component to tell you the behaviour of a Gui Item, like if it is highlighted, pushed, ect. and the ActionListener maps this. 

Read this tutorial [url=http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html]here try it again and post things that you've tried and we'll help you best we can, provided we have the time to :P.

-----------------------------------
DemonWasp
Mon Jan 10, 2011 10:54 am

Re: Help With Button Pressed
-----------------------------------
The class you are calling "ActionListener" is a blueprint and a is the object that you name the object you are making. Now that you have your "ActionListener" blueprint, you cant just go and build a "MyActionListener" object with the ActionListener blueprint. Those are too different blueprints and the other GUI wont accept your object.

Err, what? Nowhere in that gibberish did you make much sense, but when you did, you were wrong.

He has implemented class MyActionListener, which implements the ActionListener interface. Having run his code, it compiles and works just fine. In fact, I'm hard-pressed to say what's going wrong, because it seems like his code does exactly what he wants it to do.

-----------------------------------
2goto1
Mon Jan 10, 2011 11:31 am

Re: Help With Button Pressed
-----------------------------------
The class you are calling "ActionListener" is a blueprint and a is the object that you name the object you are making. Now that you have your "ActionListener" blueprint, you cant just go and build a "MyActionListener" object with the ActionListener blueprint. Those are too different blueprints and the other GUI wont accept your object.

Err, what? Nowhere in that gibberish did you make much sense, but when you did, you were wrong.

He has implemented class MyActionListener, which implements the ActionListener interface. Having run his code, it compiles and works just fine. In fact, I'm hard-pressed to say what's going wrong, because it seems like his code does exactly what he wants it to do.

Hard to say what issue gagster was having in the first place since gagster didn't really describe their roadblock or question. I'm not very familiar with Java Swing, but perhaps the fact that the same ActionListener instance is being assigned to both buttons may be causing the issue. Would it be wiser in Java to create separate ActionListener instances per button, rather than using the same ActionListener instance for both buttons?

-----------------------------------
DemonWasp
Mon Jan 10, 2011 11:56 am

RE:Help With Button Pressed
-----------------------------------
In general, it's wisest to use separate ActionListeners for separate items (just for the sake of code separation). However, I think OP's  problem lies in the fact that the definition of class "probability" occurs inside a while ( true ) loop that doesn't do anything else, other than produce output, and that while the event-handler thread is stuck in that loop, it will not respond to other mouse events, such as clicking on the "goodbye" button.

His problem should become obvious if he indents his code properly. It should be fixable pretty easily with some braces relocated.

-----------------------------------
copthesaint
Mon Jan 10, 2011 5:29 pm

RE:Help With Button Pressed
-----------------------------------
ah ha, opps. Demonwasp, dont be too hard on me :P I was tired when I answered that! lol...
-_- no more 3am answers from me.

-----------------------------------
gagster1233
Thu Jan 13, 2011 1:56 am

Re: Help With Button Pressed
-----------------------------------
i guess i wasnt specific enough, when i hit play again a window pops up that says "play again pressed" instead of running the program which is the probablility game

-----------------------------------
2goto1
Fri Jan 14, 2011 8:08 am

Re: Help With Button Pressed
-----------------------------------
That's strange. Where in your program does the text "play again pressed" get displayed? I don't see anything you've written in your code that would indicate that. 

One thing in your program that looks unusual is your ActionListener implementation. See snippet below:

[Code]else if (s.equals ("Play Again")) 
{ 
while (true) 

{ 

class probability 
{ 
public void main(String[]args) 
{ 
random(); 
} [/Code]

As-is, it looks like you're trying to run the stuff where the code  "class probability" starts. It won't get called as-is. 

Is there any reason why you're declaring a class within your while block? That seems a little strange. You'd be better off writing your classes separate from one another, so that in your source file you have all 3 classes defined separately. There's no hard and fast rule that says that you have to do this, but for new programmers, it's easier and cleaner to work this way: 

[code]public class ButtonPressDemo  {

}

class MyActionListener {

}

class probability {

}
[/code]

Then in your MyActionListener method, where Play Again is clicked, simply instantiate your probability class. I.e.:

[code]probability myProbability = new probability();[/code]

 
Once instantiated, you could call your random method:

[code]myProbability.random();[/code]

Ditch your probability.main() method, it will never get automatically called. Note that your Java program can only have public static void main(String[] args), which is your ButtonPressDemo.main().
