Reputation: 25
So I'm doing a project for school where I'm supposed to simulate a supermarket. I'm pretty far into it, but I came across a problem I haven't had before. Long story short, (among others) I have a controller class and a JFrame class.
In the main class and method, I have an object of the Controller class. In the controller cass I have an object of the JFrame class. When I run the project, the main makes a controller, which makes a JFrame.
Now, I want to add a button into the JFrame that signals the controller I want to 'move on' the simulation. I'm kind of stumped as to how to accomplish this. Basically, I want to add a button into the JFrame that will call a method in the controller class when clicked. I'm happy to post code if necessary, but it's more a problem of 'how do I do this?' than fixing something that is broken.
Upvotes: 1
Views: 130
Reputation:
Extend the JFrame with your own JFrame, which has an overloaded constructor that takes an instance of the Controller. Then when you create an instance of the JFrame from the Controller you pass it a reference to itself using the "this" keyword. When the button inside the JFrame needs to notify the controller it should be able to call some getController() method. Of course, this is all conjecture without seeing some actual code :)
Upvotes: 1