Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

MVC for ActionListener

978140Dec 5 2012 — edited Dec 6 2012
Hi. 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?
This post has been answered by TPD-Opitz on Dec 6 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2013
Added on Dec 5 2012
3 comments
418 views