Results 1 to 4 of 4
  1. #1
    mooncrosser is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you have your actionListener in the class which checks for the action, and the action it runs should be:
    OtherClass.otherClassMethod()

  3. #3
    mooncrosser is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    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 01:05 PM.

  4. #4
    mooncrosser is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks for your help, I got it figured out. I did made Piece part of GameField as an inner class. I had to add private static final long serialVersionUID = 1L; in Piece (thanks Eclipse), I have no idea what it means:). I'll check it out.

Similar Threads

  1. Replies: 5
    Last Post: 01-26-2011, 07:38 PM
  2. Replies: 2
    Last Post: 11-23-2010, 06:49 PM
  3. How to execute a .class file from a Rightclick
    By Norm in forum Suggestions & Feedback
    Replies: 0
    Last Post: 09-09-2010, 10:58 PM
  4. JdbcRowsetImpl execute() method problem
    By Aniczka in forum New To Java
    Replies: 2
    Last Post: 03-06-2010, 02:29 AM
  5. Replies: 0
    Last Post: 06-26-2008, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •