You can create a ValueSubmittedListener interface
interface ValueSubmittedListener { public void onSubmitted(String value); }
and has an Editor implements it.
class Editor implements ValueSubmittedListener{ ... public void onSubmitted(String value) {
Toolbar then provide methods
class Toolbar { private List<ValueSubmittedListener> listeners = new ArrayList<ValueSubmittedListener>(); public void addListener(ValueSubmittedListener listener) { listeners.add(listener); } private void notifyListeners() { for (ValueSubmittedListener listener : listeners) { listener.onSubmitted(textfield.getText()); } } }
Then each time you need to send a new value to the editor (i.e., submitButton ActionListener ), just call the notifyListeners method.
UPDATE:
I forgot to mention that in initComponents of Main you need to register the Editor before the Toolbar :
private void initComponents() { JFrame mainframe = new JFrame("Main Frame"); Editor editor = new Editor(); Toolbar toolbar = new Toolbar(); toolbar.addListener(editor);
source share