Re: Object-oriented applet
Please copy the full text of the error messages and paste them here. Don't edit them.
Where do you add an actionListener to the Button object?
Re: Object-oriented applet
Quote:
Originally Posted by
Norm
Please copy the full text of the error messages and paste them here. Don't edit them.
Here are the compiler error I get when I try to compile:
- Buttons.java with the Pushed object in line 12 of Buttons.java not commented out
Code :
.\Pushed.java:7: <identifier> expected
buttons.addActionListener(buttons.button);
^
.\Pushed.java:7: <identifier> expected
buttons.addActionListener(buttons.button);
^
.\Pushed.java:7: package buttons does not exist
buttons.addActionListener(buttons.button);
^
.\Pushed.java:7: package buttons does not exist
buttons.addActionListener(buttons.button);
^
4 errors
- Pushed.java with the Pushed object in line 12 of Buttons.java commented out
Code :
Pushed.java:7: <identifier> expected
buttons.addActionListener(buttons.button);
^
Pushed.java:7: <identifier> expected
buttons.addActionListener(buttons.button);
^
2 errors
- Pushed.java with the Pushed object in line 12 of Buttons.java not commented out
Code :
Pushed.java:7: <identifier> expected
buttons.addActionListener(buttons.button);
^
Pushed.java:7: <identifier> expected
buttons.addActionListener(buttons.button);
^
2 errors
As I mentioned in my OP, the Buttons.java compiles without error when the Pushed object in line 12 of Buttons.java is commented out. In fact, the GUI elements load in the appletviewer , but no action is performed when the button is pressed.
Quote:
Originally Posted by
Norm
Where do you add an actionListener to the Button object?
I tried to add the ActionListener to the Button object by adding it to the Button instance in the Buttons object I created in line 7 of Pushed.java:
Code :
buttons.addActionListener(buttons.button);
This is the line that return all the compiler warnings, so I'm not sure if my syntax is correct or if what I am trying to do is possible in the way that I am trying to do it.
Re: Object-oriented applet
Does the Buttons class have a method: addActionListener()?
What method contains the line with the errors?
Re: Object-oriented applet
Executable code has to be inside of a method or constructor (or static/instance initializer). In the body f the class, outside of any method / constructor / initializer you can have only member declarations.
Put that line buttons.addActionListener(buttons.button); inside a method or constructor.
db
Re: Object-oriented applet
Quote:
Originally Posted by
Norm
The button member of the Buttons class is not an ActionListener. The Pushed class is an ActionListener. To get a reference to a Pushed object in the Pushed class, use the this reference.
A reference to a Pushed object?
How do I add an ActionListener to the Button object in the GUI if the Pushed object contains no Button objects?
Re: Object-oriented applet
Does the Buttons class have a method: addActionListener()?
Re: Object-oriented applet
Quote:
Originally Posted by
Norm
Does the Buttons class have a method: addActionListener()?
Not unless I implement the ActionListener interface on it.
When I implement ActionListener on Buttons and put buttons.addActionListener(buttons.button) in side the constructor in the Pushed class, the code looks like this:
Code Java:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code = Buttons.java width = 100 height = 100> </applet>
*/
public class Buttons extends Applet implements ActionListener
{
Label label = new Label("Push the button!");
Button button = new Button("Push me!");
Pushed pushed = new Pushed();
public void init ()
{
this.add(button);
this.add(label);
}
public void actionPerformed(ActionEvent ae)
{
}
}
Code Java:
import java.awt.event.*;
public class Pushed implements ActionListener
{
Buttons buttons = new Buttons();
Pushed()
{
buttons.addActionListener(buttons.button);
}
public void actionPerformed(ActionEvent ae)
{
String actionStr = ae.getActionCommand();
if (actionStr.equals("Push Me!"))
{
System.out.println("Stop pushing my buttons!");
}
}
}
The compiler returns another error:
Code :
Pushed.java:9: cannot find symbol
symbol : method addActionListener(java.awt.Button)
location: class Pushed
this.addActionListener(buttons.button);
^
1 error
I thought that I could access the addActionListener method from an instance of the Buttons class if I implemented the ActionListener interface on the Buttons object.
That said, I understand how to make the applet work if I wrote it all in one class, but nowI would like to understand how to get two class to create one applet.
Re: Object-oriented applet
Quote:
Does the Buttons class have a method: addActionListener()?
Not unless I implement the ActionListener interface on it.
You're missing my point. The Buttons class extends Applet. Neither of those two classes has a method: addActionListener
Quote:
cannot find symbol
symbol : method addActionListener(java.awt.Button)
This last error message you posted shows another class that does NOT have the method: addActionListener
You need to read the API doc for the classes you are using to see if they have a method you are trying to use. Some do, many don't.
Re: Object-oriented applet
I'm confused because, in the past, I have been able to add ActionListeners to Button objects in Applet classes.
For instance the following code compiles and runs as desired:
Code Java:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code = "CalculatorApplet2" width = 225 height = 300>
</applet>
*/
public class CalculatorApplet2 extends Applet implements ActionListener
{
private TextField fArgTxt;
private TextField sArgTxt;
private TextField ansTxt;
public void init ()
{
// Construct the TextFields
this.fArgTxt = new TextField(25);
this.sArgTxt = new TextField(25);
this.ansTxt = new TextField(25);
Button adds = new Button("+");
Button subs = new Button("-");
Button mults = new Button("*");
Button divs = new Button("/");
// add the button to the layout
this.add(fArgTxt);
this.add(sArgTxt);
this.add(ansTxt);
this.add(adds);
this.add(subs);
this.add(mults);
this.add(divs);
fArgTxt.addActionListener(this);
sArgTxt.addActionListener(this);
adds.addActionListener(this);
subs.addActionListener(this);
mults.addActionListener(this);
divs.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
double fArgVal = Double.parseDouble(fArgTxt.getText());
double sArgVal = Double.parseDouble(sArgTxt.getText());
String actionStr = ae.getActionCommand();
if (actionStr.equals("+"))
{
double sumVal = fArgVal + sArgVal;
ansTxt.setText("" + sumVal);
}
if (actionStr.equals("-"))
{
double difVal = fArgVal - sArgVal;
ansTxt.setText("" + difVal);
}
if (actionStr.equals("*"))
{
double prodVal = fArgVal * sArgVal;
ansTxt.setText("" + prodVal);
}
if (actionStr.equals("/"))
{
double quotVal = fArgVal / sArgVal;
if (Double.isInfinite(quotVal))
{
ansTxt.setText("Error: Division by zero");
}
else
{
ansTxt.setText("" + quotVal);
}
}
}
}
I understand everything is done all in one class here but adding the ActionListeners does not cause the compiler errors that the compiling the code in the OP or the following code does:
Code Java:
import java.applet.*;
import java.awt.*;
/*
<applet code = ButtonPushed.java width = 100 height = 100>
</applet>
*/
public class ButtonPushed extends Applet implements ActionListener
{
Button button = new Button("Push me!");
Label label = new Label("Push the button");
public void init()
{
this.add(button);
this.add(label);
button.addActionListener(this);
label.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String actionStr = ae.getActionCommand();
if (ae.equals("Push Me!"))
{
label.setText("Don't push my buttons!");
}
}
}
Re: Object-oriented applet
Quote:
I have been able to add ActionListeners to Button
Yes, the Button class has an addActionListener method
Re: Object-oriented applet
Quote:
Originally Posted by
Norm
Yes, the Button class has an addActionListener method
So why can't I add an ActionListener to a Button object inside the Buttons object?
I understand that I can't add the ActionListener to the Buttons buttons object because the class of which it is an instance does not have the addActionListener() method, but the class of which buttons.button is an instance (i.e., the Button class) does have an addActionListener() method.
Re: Object-oriented applet
Quote:
add an ActionListener to a Button object inside the Buttons object
You can do that.
Quote:
add the ActionListener to the Buttons buttons object because the class of which it is an instance does not have the addActionListener() method, but the class of which buttons.button is an instance (i.e., the Button class) does have an addActionListener() method.
Look at how you are calling the addActionListener method. What type is the argument passed in the method call?
Re: Object-oriented applet
Quote:
Originally Posted by
Norm
Look at how you are calling the addActionListener method. What type is the argument passed in the method call?
I see it now: the documentation say the addActionListener() method must take an ActionListener argument.
I'm sorry that my understanding is torturously slow in coming. I think that part of my problem was that I was interpreting the this in button.addActionListener(this) to being saying "add an ActionListener to this button" and not "add this ActionListner to button". I also seem to have missed the fact that addActionListener() took an ActionListener type argument, despite having read through the documentation several times.:o
That said, the applet seems to be throwing some sort of run-time exception because it prints the following text repeatedly:
Code :
at Buttons.<init>(Buttons.java:13)
at Pushed.<init>(Pushed.java:5)
I don't know what is happening at those lines, because the message is run off the accessible window before I can read it.
Re: Object-oriented applet
Your code is recursively calling itself. Add print outs to both of the constructors to see what I mean.
The following doesn't work because the problem is outside of the constructors.
To see where each constructor is called from add this statement to both constructors:
try{throw new Exception("who called?");}catch(Exception x){x.printStackTrace();}
It will print a stack trace showing who called
Re: Object-oriented applet
Quote:
Originally Posted by
Norm
Your code is recursively calling itself. Add print outs to both of the constructors to see what I mean.
The following doesn't work because the problem is outside of the constructors.
To see where each constructor is called from add this statement to both constructors:
try{throw new Exception("who called?");}catch(Exception x){x.printStackTrace();}
It will print a stack trace showing who called
Can you show where to add the try/throw/catch blocks to get the stack trace to print?
I have modified my code like this:
Code Java:
import java.applet.*;
import java.awt.*;
/*
<applet code = Buttons.java width = 100 height = 100> </applet>
*/
public class Buttons extends Applet
{
Label label = new Label("Push the button!");
Button button = new Button("Push me!");
Pushed pushed = new Pushed();
public Buttons()
{
try
{
throw new Exception("who called?");
}
catch(Exception x)
{
x.printStackTrace()
}
}
public void init ()
{
this.add(button);
this.add(label);
}
}
Code Java:
import java.awt.event.*;
public class Pushed implements ActionListener
{
public Pushed ()
{
Buttons buttons = new Buttons();
(buttons.button).addActionListener(this);
try
{
throw new Exception("who called?");
}
catch(Exception x)
{
x.printStackTrace();
}
}
public void actionPerformed(ActionEvent ae)
{
String actionStr = ae.getActionCommand();
if (actionStr.equals("Push Me!"))
{
System.out.println("Stop pushing my buttons!");
}
}
}
It still only prints what I noted above.
Re: Object-oriented applet
Do you see the recursion?
The Buttons class creates a Pushed object:
Pushed pushed = new Pushed();
The Pushed class creates a Buttons object:
Buttons buttons = new Buttons();
The Buttons class creates a Pushed object:
Pushed pushed = new Pushed();
The Pushed class creates a Buttons object:
Buttons buttons = new Buttons();
The Buttons class creates a Pushed object:
Pushed pushed = new Pushed();
The Pushed class creates a Buttons object:
Buttons buttons = new Buttons();
The Buttons class creates a Pushed object:
Pushed pushed = new Pushed();
The Pushed class creates a Buttons object:
Buttons buttons = new Buttons();
The Buttons class creates a Pushed object:
Pushed pushed = new Pushed();
The Pushed class creates a Buttons object:
Buttons buttons = new Buttons();
....
until the system runs out of memory.
You need to think through when to create what objects in what order.
Re: Object-oriented applet
Quote:
Originally Posted by
Norm
Do you see the recursion?
until the system runs out of memory.
You need to think through when to create what objects in what order.
I should have been more clear. I did see the recursion, but I was asking about why it was throwing exceptions and how I could get it to print the exceptions and their causes.
Re: Object-oriented applet
Quote:
why it was throwing exceptions and how I could get it to print the exceptions and their causes.
What exceptions is it throwing? Please copy and paste the error message here.
Re: Object-oriented applet
Quote:
Originally Posted by
Norm
What exceptions is it throwing? Please copy and paste the error message here.
I don't know and I can't copy and paste them here because they have been run off the window before I can stop the applet.
All I get is:
Code :
at Pushed.<init>(Pushed.java:7)
at Buttons.<init>(Buttons.java:12)
I'm sorry that I am not more specific, but the applet runs too fast for me to capture what ever "at Pushed.<init>(Pushed.java:7)" and "at Buttons.<init>(Buttons.java:12) " refers to.
Re: Object-oriented applet
What you've posted is the call stack which is verrrrrrrrrrrrrrrry long because of the recursion.
Now look at your code and play computer with it. Where does the cross calls to the creation of the other class occur?
Break that and the recursion will stop.
Re: Object-oriented applet
Here are the exceptions that the applet throws:
Code :
java.lang.Exception: who called?
at Buttons.<init>(Buttons.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:619)
Re: Object-oriented applet
Quote:
java.lang.Exception: who called?
That's the code I had you put in for debugging. You can comment it out now. Its for the case where you want to see the call stack to show who called a method.
Re: Object-oriented applet
Quote:
Originally Posted by
Norm
That's the code I had you put in for debugging. You can comment it out now. Its for the case where you want to see the call stack to show who called a method.
So is the applet actually throwing an exception when the code is commented out?
The problem that I am having is that, with the call to the Pushed constructor in the Buttons class commented out, there is no Pushed object that handles the ActionEvent created when I push the "Push Me!" button. Or so I think, because the action in the actionPerformed() method in the Pushed object is not performed (i.e., "Stop pushing my buttons!" is not printed to the console.
How do I get the objects to communicate without creating an infinite loop?
Re: Object-oriented applet
You need to pass a reference to one class to the other. For example in Buttons pass a reference to itself (this) in the constructor of the Pushed class.
Pushed pushed = new Pushed(this);
and in the Pushed class, receive the reference in a constructor:
Pushed(Buttons b) {