简体   繁体   中英

Passing a message from one inner class to another inner class.

I'm having some problems with passing a message from one inner class to another inner class in an application I'm writing.

    public class MealButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent evt){
        int selectedRow = theFoodTable.getSelectedRow();
        Food theTempFood = FoodTableUI.this.theFoodCntl.getCurrentFood(selectedRow);
        theMealList.add(theTempFood);
        MealPlannerPopupUI thePopup = new MealPlannerPopupUI(theFoodCntl);
        if(endMeal == true){
            FoodTableUI.this.setVisible(false);
            FoodTableUI.this.theFoodCntl.getMealPlannerUI(theMealList); 
        }
    }
}

     public class NoButtonListener implements ActionListener{
     public void actionPerformed(ActionEvent evt){  
        MealPlannerPopupUI.this.setVisible(false);
        //MealPlannerPopupUI.this.theFoodCntl.getMealPlannerUI();
        //
    }
}

Pasted above are the two relevant classes. I need to be able to pass a boolean from NoButtonListener over to MealButtonListener . MealButtonListener is located in a class called FoodTableUI and NoButtonListener is located in a class called MealPlannerPopupUI . I'm not sure how to get a message between these two inner classes.

Generally, listener classes should have no data members, public or private, and no need to communicate with each other. Any state should be held by the enclosing class, not the listener class.

Some people feel that listener class methods should do nothing but call an enclosing class method, which actually does all the work. I'm on the fence about this. But if you find yourself having to call some method from the listener yourself, then you should definitely move that method's "meat" to the enclosing class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM