-1

I've class BubblesFrame which has inner class startnewGame :

 public class startNewGame implements ActionListener{
        public void actionPerformed(ActionEvent event){

            remove(panel);
            panel = new BubbleMainPanel();
            add(panel);
            validate();
            panel.repaint();

        }
    }

in class Menu i'd like to add event handler startNewGame

 ActionListener listener = new BubblesFrame.startNewGame();
        newGame.addActionListener(listener);

but it returns following error:

No enclosing instance of type BubblesFrame is accessible. Must qualify the allocation with an enclosing instance of type BubblesFrame (e.g. x.new A() where x is an instance of BubblesFrame)

what's wrong with my inner class?

Stan Fad
  • 1,124
  • 12
  • 23

3 Answers3

1

Make startNewGame class static.

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
Raman Shrivastava
  • 2,923
  • 15
  • 26
0

If your inner class is not static use this approach:

 ActionListener listener = new BubblesFrame().new startNewGame();

if startNewGame class is static you can do as you have done before:

ActionListener listener = new BubblesFrame.startNewGame();
ka4eli
  • 5,294
  • 3
  • 23
  • 43
0

You did instantiate your outer class, but you forgot to instantiate your inner class.

BubblesFrame f = new BubblesFrame();  //Instantiate outer class
ActionListener listener = f.new startNewGame(); //Instantiate inner class

I am more curious why would you want to expose your inner class when it is a listener for your BubblesFrame. You might want to look into your design pattern again.

Usually it is either an anonymous class or a private class.

user3437460
  • 17,253
  • 15
  • 58
  • 106