-1

I am newly learning Java Event handling. I wrote a code but it shows an error while I try to call a Constructor of class MyGUI from the main method. Please have a look and explain the following error to me.

Error:

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

My code:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class MyGUI extends JPanel {
    JButton button;
    JTextField textField;
    JRadioButton radioButton;

    MyGUI(){
    add(new JButton("Button"));
    add(new JTextField(10));
    add(new JRadioButton("RadioButton"));

    }


     class MyHandler implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e) {

            if(e.getSource()==button)
            {
                JOptionPane.showMessageDialog(null, "Button has been clicked");
            }
            else if(e.getSource()==textField)
            {
                JOptionPane.showMessageDialog(null, "TextField has been clicked");
            }
            else if(e.getSource()==radioButton)
            {
                JOptionPane.showMessageDialog(null, "RadioButton has been clicked");
            }

        }

    }

public static void main(String[]args)
{
    MyGUI gui=new MyGUI();

    MyHandler handler=new MyHandler();    //Error Shows on this statement
    gui.button.addActionListener(handler);


    JFrame frame=new JFrame("Its a frame");
    frame.add(gui);
    frame.setVisible(true);
    frame.setSize(500,500);
    frame.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



}
}
Autonomous
  • 8,935
  • 1
  • 38
  • 77
raiyan106
  • 85
  • 1
  • 6
  • So..what is the error? – colti Aug 11 '14 at 18:46
  • `MyHandler` class is nested within MyGUI. You need to refer to it as `MyGUI.MyHandler`. – William Morrison Aug 11 '14 at 18:46
  • If you want others to help you debug, you need to add more details. Imagine I came up to you and said "here is my code I'm getting an error", what would be your first question? Same here, what does the error state? – scrappedcola Aug 11 '14 at 18:46
  • @colti when i call the new MyHandler() constructor, it shows an error. – raiyan106 Aug 11 '14 at 18:47
  • @raiyan106 You'll need to be more specific. – colti Aug 11 '14 at 18:48
  • @scrappedcola yes you are right man. I am sorry. The following error shows "No enclosing instance of type MyGUI is accessible. Must qualify the allocation with an enclosing instance of type MyGUI (e.g. x.new A() where x is an instance of MyGUI)." – raiyan106 Aug 11 '14 at 18:48
  • @raiyan106 you need to make your question make more sense by including more details that will help one understand your promble. Also showing the error message will important – Paullo Aug 11 '14 at 18:53
  • 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 04 '16 at 00:37

1 Answers1

2

The main method isn't the one to be setting up action listeners for MyGUI. Move the code that sets up the action listeners into the MyGUI constructor.

You'll also need to assign the new components you're creating to your instance variables.

MyGUI(){
    button = new JButton("Button");
    textField = new JTextField(10);
    radioButton = new JRadioButton("RadioButton");

    add(button);
    add(textField);
    add(radioButton);

    MyHandler handler = new MyHandler();
    button.addActionListener(handler);
}
rgettman
  • 176,041
  • 30
  • 275
  • 357