MVC for ActionListener
978140Dec 5 2012 — edited Dec 6 2012Hi. I am having one Main class(main-package) who creates GUI class(view-package). The GUI class contains JButtons.
Anyone have an idea how I can add ActionListeners for my JButtons in a "controll-package" without getting cyclic dependencies?
Example:
Main:
Gui gui = new GUI();
MyListener list = new MyListener(gui); // To allow the listener(controller) give feedback to GUI
GUI: Create a button and set its action to MyListener
MyListener list = new MyListener();
JButton a = new JButton();
a.addActionListener(list);
MyListener: Contains the gui and gives feedback to it after click
private GUI gui;
*public MyListener(GUI gui){*
this.gui = gui;
*}*
*public void actionPerformed(){*
gui.setMyTest("Button Clicked!");
*}*
But problem with this is the cyclic dependency I get. Any hint on approach?