I have been trying to use classes that implement ActionListener to respond to user input. I'm consistently noticing that for an action listener to communicate the event to other objects, it has to contain the object. Like,
The object I want to get updated when action_src is clicked:
public class Display_Field {
JTextField display;
public Display_Field ( JButton action_src ) {
action_src.addActionListener( new Open_Dialog_Click( this ) );
}
public void setText( String text ) {
display.setText( text );
}
}
The ActionListener I plan to add to action_src:
public class Open_Dialog_Click implements ActionListener {
private Display_Field display;
public void actionPerformed( ActionEvent action ) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog( null );
display.setText( chooser.getSelectedFile().getName() );
}
public Open_Dialog_Click( Display_Field display ) {
this.display = display;
}
}
There is so many convolutions! Like, the display needs to be constructed with a button that adds an ActionListener that needs to be constructed with the display...
Is there a less convoluted way to allow data to flow between components?
I have considered making a central class that houses all the listeners, acting as wiring for any and all intercomponent communication, but that seems like the class is doing too much for its own good. ...or is that how it is done after all?