0

I'm modifying some existing Java code for an assignment and I can't figure out how to call a function of an existing object from within an ActionListener.

There will only ever be one instance of "myGame".

Here is the relevant code;

public class myGame extends JFrame { 
    public myGame() { 
        //...snip...

        statsBar = new JLabel(""); 
        add(statsBar, BorderLayout.SOUTH); 

        add(new Board(statsBar)); 

        setResizable(false); 
        setVisible(true);

        addMenubar();
    } 

    private void addMenubar() {
        JMenuBar menubar = new JMenuBar();
        JMenu topMnuGame = new JMenu("File");
        JMenuItem mnuSolve = new JMenuItem("Solve");
        mnuSolve.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

              // freshGame.solveGame();
              // this is where I need to call the solveGame function
              // for the "freshGame" instance.
              solveGame();

            }
        });
        topMnuGame.add(mnuSolve);
        menubar.add(topMnuGame);
}
    public static void main(String[] args) { 

      myGame freshGame = new myGame();

    }

}

.

public class Board extends JPanel { 

public Board(JLabel statsBar) {
    this.statsBar = statsBar; 

    //..snip..

    addMouseListener( new gameAdapter() ); 
}

    public void solveGame() {
    // .. do stuff with object ..
    }

}

So my question is, how can I call "solveGame()" from within the "myGame" class using the "freshGame" instance?

AgentOrange
  • 65
  • 2
  • 11

2 Answers2

1

Short generic answer:

In Java, if you have an object, also called an instance of a class, for example

MyClass myObj = new MyClass();

Then you can access the non-static members of the class for that object like this:

myObj.myMethod();

Where ever you are going to call a method, you need a reference to the right object, so pass it as argument to method that needs it:

class OtherClass {
    // snip constructors etc
    public void otherMethod(MyClass obj) {
        obj.myMethod();
    }
}

Alternatively, pass it as constructor parameter and store it in a private member variable, so you call it from methods later.

class SomeClass {

    private final MyClass someMyClass;

    SomeClass(MyClass someMyClass) {
        this.someMyClass = someMyClass;
    }

    public void someMethod() {
        this.someMyClass.myMethod();
    }
}
Community
  • 1
  • 1
hyde
  • 60,639
  • 21
  • 115
  • 176
0

I don't understand fully whats your problem. What i understood is you are not able to call solveGame() function with freshGame object which is instance of your myGame class.

1. Your solveGame() function is in Board Class so you can only call it by using Board instance.
2 So you have to create instance of Board in your myGame class to use it, may be like following.

public class myGame extends JFrame { 
  private Board board;
  public myGame() { 
    //...snip...

    statsBar = new JLabel(""); 
    board= Board(statsBar ) // initializing Board Class . you can do your self 

    add(statsBar, BorderLayout.SOUTH); 
    add(new Board(statsBar)); 
    setResizable(false); 
    setVisible(true);
    addMenubar();
} 

private void addMenubar() {
    JMenuBar menubar = new JMenuBar();
    JMenu topMnuGame = new JMenu("File");
    JMenuItem mnuSolve = new JMenuItem("Solve");
    mnuSolve.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

          // freshGame.solveGame();
          // this is where I need to call the solveGame function
          // for the "freshGame" instance.
          solveGame();

          // now you can call 
            board.solveGame();
        }
    });
    topMnuGame.add(mnuSolve);
    menubar.add(topMnuGame);
 }
public static void main(String[] args) { 
    myGame freshGame = new myGame();
 }
}
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
  • Thank you, this helped me get it working as intended. I was forgetting to assign the Board object to the global variable, your answer helped me realize this. – AgentOrange Oct 23 '13 at 07:50