Now I have 2 classes. The first one is called UIController and the other one is called MainJFrame.

I would like to know how to make a listener for a JFrame which has a JComboBox inside. Everytime a ComboBox submit a new data the JFrame will tell the UIController that there is a change and the UIController can retrieve the message from what the users put.

I don't know how to do it since if I use actionListener then I have to check whether the action is from the JComboBox or not. However, in the UICOntroller, there is only MainJFrame class.

I want to use the UIController to receive command from the user and then the UIController will decide what to do with the other part of GUI...

Thank you very much.

Recommended Answers

All 2 Replies

I'm not sure what it is you're trying to do, but you can always use interfaces to make it possible for your listener to 'command' your other class

When you have a controller and a GUI it normally goes like this:
The controller is responsible for invoking the GUI, and passes itself as a parameter to the GUI constructor, so the GUI has a reference to the controller.
The GUI is responsible for all listeners on the GUI components.
The controller has methods for handing whatever has to be done when the user inputs something - but at the logical level, not in terms of GUI components. The GUI responds to its own GUI events, then calls the appropriate controller method.
Eg if the GUI has a text field for product name and an OK button , then the gui has the listener on the OK button. When the user presses OK the GUI responds, retrieves the product name, and calls the controller with something like controller.productSelected(productEntryField.getText());

This way the GUI, and only the GUI, deals with GUI objects. The controller, and only the controller, deals with the processing logic that is needed, regardless of how the GUI works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.