Inter-class event handling

I have a main class with the Editor class (with JTextPane) and the toolbar class (with JList and Jbutton, I don't want to use JToolBar). These two classes are composed of many components, and I would not want to mix them in one class. I want the editor and toolbar to exchange data. Say I write "Hello" in the toolbar, and then click "Submit." I want the text box to show me "Hello." I create classes as follows:

public class Main{ public MainGUI(){ initComponents(); } private void initComponents(){ JFrame mainframe=new JFrame("Main Frame"); Editor editor=new Editor(); Toolbar toolbar=new Toolbar(); mainframe.getContentPane().setLayout(new BorderLayout()); mainframe.getContentPane().add(editor, BorderLayout.CENTER); mainframe.getContentPane().add(toolbar, BorderLayout.NORTH); mainframe.setVisible(true); } } public class Editor extends JPanel{ public Editor(){ super(); initComponents(); } private void initComponents(){ JTextPane textpane=new JTextPane(); this.setLayout(new BorderLayout()); this.add(textpane, BorderLayout.CENTER); } } public class Toolbar extends JPanel{ public Toolbar(){ super(); initComponents(); } private void initComponents(){ JTextField textfield=new JTextField(); JButton submitbutton=new JButton("Submit"); this.setLayout(newFlowLayout()); this.add(textfield); this.add(submitbutton); } } 

How do I implement event handling between the toolbar and the editor?

+4
source share
2 answers

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) { // add text to JTextPane. } } 

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); // register listener ... } 
+5
source

The basic principle is to pass links as needed so that every object can access the objects that it needs.

For example, if your β€œsend” means adding text to a text field (which you have on the toolbar) in the editor text panel, you can do the following:

 public class Main{ public MainGUI(){ initComponents(); } private void initComponents(){ JFrame mainframe=new JFrame("Main Frame"); Editor editor=new Editor(); Toolbar toolbar=new Toolbar(editor); mainframe.getContentPane().setLayout(new BorderLayout()); mainframe.getContentPane().add(editor, BorderLayout.CENTER); mainframe.getContentPane().add(toolbar, BorderLayout.NORTH); mainframe.setVisible(true); } } private final JTextPane textpane=new JTextPane(); public class Editor extends JPanel{ public Editor(){ super(); initComponents(); } private void initComponents(){ this.setLayout(new BorderLayout()); this.add(textpane, BorderLayout.CENTER); } public void appendText(final String text) { this.textpane.setText( this.textpane.getText()+text ); } } public class Toolbar extends JPanel{ private final Editor editor; public Toolbar(final Editor editor){ super(); this.editor = editor; initComponents(); } private void initComponents(){ final JTextField textfield=new JTextField(); JButton submitbutton=new JButton("Submit"); submitbutton.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent event) { editor.appendText( textfield.getText() ); } }); this.setLayout(newFlowLayout()); this.add(textfield); this.add(submitbutton); } } 
+2
source

All Articles