1

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?

  • Are you using an IDE that has form creation capabilities? That's the first thing you should try. – Robert Harvey Apr 2 '14 at 22:04
  • I have eclipse, but I was hoping to take a coding approach to it. – user2738698 Apr 2 '14 at 23:43
  • Convolution is a necessary evil with swing. That's why IDE's are there to help you with it; it's a lot of heavy lifting if you code it by hand. – Robert Harvey Apr 2 '14 at 23:44
  • Ah kk, got to tough it out! – user2738698 Apr 3 '14 at 2:36
1

Is there a less convoluted way to allow data to flow between components?

Yes. It's called a model. In a model / view / controller pattern (MVC), an instance of the model is passed to the view so that the values in the model can be read by the view. The values in the model are updated by controller classes.

In a Java Swing application, you generally have many controller classes, each responsible for a portion of the view.

Take a look at my John Conway’s Game of Life in Java Swing article, to see how a fairly simple Java Swing application makes use of the MVC pattern.

  • The link is broken. – Piovezan Dec 27 '19 at 1:36
-1

What I normally do is have a JPanel that holds a reference to all the elements involved. This JPanel implements ActionListener so you can just pass "this" to every element (JTextField, JButton, etc.) that you create.

This way you have access to all the objects (they are instance variables) and listen to all the events within the same class.

If things get too messy then the ActionListener could be an external class (a "controller") that could hold a reference to more than one JPanel.

Some code:

public class TestJPanel implements ActionListener
{
    private JButton button;
    private JTextArea textArea;
    private JCheckBox checkBox;

    public TestJPanel() {
        button = new JButton("I'm a button");
        button.addActionListener(this);

        checkBox = new JCheckBox();
        checkBox.addActionListener(this);

        textArea = new JTextArea();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button)
        {
            textArea.setText("Button clicked");
        }
        else if (e.getSource() == checkBox)
        {
            textArea.setText("Checkbox clicked");
        }
    }
}
  • 1
    This example mixes view elements and listeners together, making it a poor example of how to use Swing. SRP and loose coupling are powerful tools for reusable and easy to understand software. – user22815 Dec 30 '14 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.