Anup Patel
Anup Patel

Reputation: 11

Java GUI Action Listener with Inner Classes

I am working on a LAB for one of my classes and am in need of some assistance.

I am building an Apartment Complex GUI which will have a menu system and individual functions between many different classes. The complex with consist of Tenants, Employees and a Bank.

I currently have the whole project working based out of the console but now I am assigned to convert it to a GUI interface.

This is the code in my main function for GUI:

   ApartmentComplex mavPlace = new ApartmentComplex(); //creates a new apartment complex object
   mavPlace.aptBank.setBalance(ANNUAL_BUDGET); //sets the apartment bank budget
   readFile(mavPlace); 

   mavPlace.goThroughAndAssignValues(mavPlace);
   JFrame frame = new JFrame("My First GUI");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(300,300);
   JButton button = new JButton("Press");
   frame.getContentPane().add(button); // Adds Button to content pane of frame
   frame.setVisible(true);

   button.addActionListener(new ActionListener()
            {
                    public void actionPerformed(ActionEvent e)
                    {
                        //Execute when button is pressed                          
                        mavPlace.lease(mavPlace);
                    }
            });

With the action listener, when the button is pressed it should call a lease function in another class of mine. From there I want it do go back to console output. The error netbeans is giving me is: local variable mavPlace is accessed from within inner class; needs to be declared final .... now I went an made the decleration final just to see what happened and it worked, but i couldnt edit my complex details so that was not possible.

What can i do?

Thank You!

Upvotes: 0

Views: 1283

Answers (2)

Jeremy Zhou
Jeremy Zhou

Reputation: 49

If you use Anonymous Class, you should set the parameter used in the class as final type in current block or as a member private variable.

class MyGUI
{
  ApartmentComplex mavPlace;
  public MyGUI()
  {
   JFrame frame = new JFrame("My First GUI");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(300,300);
   JButton button = new JButton("Press");
   frame.getContentPane().add(button); // Adds Button to content pane of frame
   frame.setVisible(true);
   mavPlace = new ApartmentComplex(); //creates a new apartment complex object
   mavPlace.aptBank.setBalance(ANNUAL_BUDGET); //sets the apartment bank budget
   readFile(mavPlace); 

   mavPlace.goThroughAndAssignValues(mavPlace);
   button.addActionListener(new ActionListener()
            {
                    public void actionPerformed(ActionEvent e)
                    {
                        //Execute when button is pressed                          
                        mavPlace.lease(mavPlace);
                    }
            });



  }


}

I think you should reconsider your structure of your program. If you told us the complete purpose, you would get better answer.

Upvotes: 0

clicky
clicky

Reputation: 865

Make your class implement the ActionListener interface and use this to add an action listener ie

button.addActionListener(this);

http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Upvotes: 1

Related Questions