Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot Invoke Method in Container

 
Vic Brown
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot call a Method that's in an applet from a components code. The component I've created (ImageButton) by having a seperate file that extends Canvas and implements MouseListener.
//--- Seperate File (IconApplet.java) ------>
import java.applet.*;
import java.awt.*;
import java.net.URL;
import java.awt.event.*;
import java.awt.image.*;
public class IconApplet extends Applet implements ActionListener {
Label myLabel = null;

public void init() {
URL imgURL = null;
try {
imgURL = new URL(getDocumentBase(), "bean.gif");
} catch (Exception ignore) {
}
Image beanImg = getImage(imgURL);
ImageButton myCanvas = new ImageButton(beanImg);
myLabel = new Label("Here is some text.", Label.RIGHT);
setLayout(new GridLayout(1, 0));
add(myCanvas);
add(myLabel);
}
public void myChange(String strChange){
myLabel.setText(strChange);
}
}
//-------Seperate File (ImageButton.java) ----->
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class ImageButton extends Canvas implements MouseListener {
Image img = null;
public ImageButton(Image picture){
addMouseListener (this);
img = picture;
setSize(80, 80);
setBackground(Color.blue);
}
public void init() { }
public void paint(Graphics g) {
g.drawImage(img, 8, 8, 16, 16, this);
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
setBackground(Color.red);
repaint();
//This is where I cannto call myChange("some text");
//from the container that created this component
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vic,
You don't have a handle on the IconApplet parent so you need to walk up the tree starting from your self. If you add the following at the point of your comments (inside mouseClicked) routine it should work for you.

Regards,
Manfred.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic