2

Here is the code -

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public final class SetLabelForDemo {
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new JLabeledButton("foo:")); // new JLabeledButton("foo:") is the problem
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private final class JLabeledButton extends JButton{
        public JLabeledButton(final String s){
            super();
            JLabel label = new JLabel(s);
            label.setLabelFor(this);
        }
    }
}

And here is the error message -

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

I don't understand this error at all. To me, everything seems perfectly valid. Am I missing something?

BrendanMcK
  • 14,252
  • 45
  • 54
mre
  • 43,520
  • 33
  • 120
  • 170
  • This is a bad, bad question. :/ – mre Sep 29 '11 at 16:40
  • that sometimes happends if you leave to training the brain on this forum :-) for unbelivable question +1 – mKorbel Sep 29 '11 at 17:07
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 03 '16 at 23:56

4 Answers4

3

You'll have to declare your class JLabeledButton static since you instantiate it within a static context:

private static final class JLabeledButton extends JButton {
    ...
}

Because your method createAndShowGUI is static the compiler does not know for which instance of SetLabelForDemo you are creating the enclosed class.

Howard
  • 38,639
  • 9
  • 64
  • 83
3

The JLabeledButton class should be static. Else, it can only be instantiated as part of an enclosing SetLabelForDemo instance. A non static inner class must always have an implicit reference to its enclosing instance.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
3

I know you've accepted an answer, but the other way to solve it is to instantiate the inner class on an instance of the outer class. e.g.,

private static void createAndShowGUI() {
   final JFrame frame = new JFrame();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.getContentPane().add(
         new SetLabelForDemo().new JLabeledButton("foo:"));
   frame.pack();
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
}

This is a funny syntax, but it works. And this has nothing to do with Swing and all to do with use of inner classes inside of a static context.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • +1 for demonstrating instantiation of a non-static inner class (`JLabeledButton`) in a static context (`static void createAndShowGUI()`). – trashgod Sep 29 '11 at 17:58
2

Mark JLabeledButton as static class.

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94