Results 1 to 4 of 4
- 04-03-2011, 10:39 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Execute method in a class when event happens in another one.
Hello, I'm building a puzzle game and I got GameField class that extends JPanel and Piece class that extends JLabel.I got multiple pieces in the field. What's the easiest way to tell the GameField that MousePressed event has been executed in a piece an witch piece it is( I need that because I want to change the ZOrdering, so it's shown on the top). If you need any code, I'll add it later, but most of it is quite irrelevant.
-
you have your actionListener in the class which checks for the action, and the action it runs should be:
OtherClass.otherClassMethod()
- 04-03-2011, 12:01 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Cannot refer to a non-final variable gf inside an inner class defined in a different method.
/----- Here's what I do.
public class GameField extends JPanel {
public GameField(String path){
//Irrelevant stuff
//This adds the pieces to the pannel.
for(PieceData pd : pm.GetPieces())
{
Piece p = new Piece(pd,this); /* I've modded this, so I can reach the GameField class within a piece*/
p.setLocation(pd.getTrueLocation());
this.add(p);
}
}
/*This is the method I want to execute, when in an Piece p MousePressed is executed*/
public void Test(Piece p){
setComponentZOrder(p, 1);
}
}
//-------------- Here's piece class
public class Piece extends JLabel{
Piece(PieceData pd, GameField gf){
//Irrelevant
MouseAdapter MA = new MouseAdapter() {
public void mousePressed(MouseEvent e){
cp = e.getLocationOnScreen();
gf.Test(Piece.this); //The Error is Here
}
}
this.addMouseListener(MA);
}
}
//---- I've tried to make Piece class inner for GameField, but there was some error, so I can't use OtherClass.otherClassMethod() ; .
EDIT: I got what the error means, I need to pass GameField as a refference ( is that allowed in Java?)Last edited by mooncrosser; 04-03-2011 at 12:05 PM.
- 04-03-2011, 12:23 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
How to execute a class in a subdirectory in .jar file
By zeyarag in forum New To JavaReplies: 2Last Post: 11-23-2010, 05:49 PM -
How to execute a .class file from a Rightclick
By Norm in forum Suggestions & FeedbackReplies: 0Last Post: 09-09-2010, 09:58 PM -
JdbcRowsetImpl execute() method problem
By Aniczka in forum New To JavaReplies: 2Last Post: 03-06-2010, 01:29 AM -
How to use Timer class to schedule a task to execute once 5 seconds have passed
By Java Tip in forum java.utilReplies: 0Last Post: 06-26-2008, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks