Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java event listener insode a class other than the extension to japplet

Status
Not open for further replies.

joelwenzel

Programmer
Jun 28, 2002
448
I have created a component, obj, which extends JPanel. I will put many of these components in my applet. I have added a MouseListener to the obj class (but not too the main applet class) but it doesn't seem to do anything. When I click the mouse, it should update some text that is drawn on the component but instead nothing happens

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class piece2 extends JPanel implements MouseListener {

Image pic;
int numClicks;
JLayeredPane layeredpane;


piece2(String imgPath, int xPos,int yPos, JLayeredPane LP)
{
layeredpane = LP;
numClicks = 0;
Toolkit toolkit = Toolkit.getDefaultToolkit();
pic = toolkit.getImage(imgPath);
setBounds(xPos, yPos, 70,70);
}

public void paint (Graphics g)
{
g.drawImage(pic,0,0,this);
g.drawString("xx "+numClicks,40,30);
//paintComponents(g);
}

public void mousePressed(MouseEvent e) {
numClicks++;
}

public void mouseMoved (MouseEvent e) {} // ignore these events
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) { }
public void mouseExited(MouseEvent e) {}

}



And in the applet, I just say

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class puzzlev1 extends JApplet{

piece2 mpiece;
JLayeredPane layeredpane;

public void init() {
setSize(300,400);
layeredpane = new JLayeredPane();
layeredpane.setPreferredSize(new Dimension(300, 400));

mpiece = new piece2("img.jpg",0,0,layeredpane);
layeredpane.add(mpiece,JLayeredPane.PALETTE_LAYER);
getContentPane().add(mpiece);
}
}

Note...the layered pane is there because once I see that it is registering clicks, the clicking on the component is going to change its position in the layered pane
 
ok...now even this simple mouse listener won't work. what am i screwing up

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//import java.applet.*;

public class puzzlev2 extends JApplet implements MouseListener {

int clicks;

public void init()
{
clicks = 0;
}

public void paint (Graphics g)
{
//clicks++;
g.drawString("P"+clicks,100,100);
}

public void mousePressed(MouseEvent e) {
clicks++;
repaint();

}

// ignore these events
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) { }
public void mouseExited(MouseEvent e) {}//*/
}
 
ok...stupid me, I forgot

addMouseListener(this);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top