Search code examples
javaswingthisanonymous-class

Java Swing passing instance of class vs anonymous class


I'm following a Swing tutorial and I found two ways to make the below code work. Essentially we are creating a frame and placing buttons and a text area in it. As far as I can tell they achieve the same functionality however I am curious as to what the difference is underneath/ if one would be a better approach for larger projects.

This is the way it was shown in the tutorial:

public class MainFrame extends JFrame {

    private TextPanel textPanel;
    private ButtonPanel buttonPanel;

    public MainFrame(String title){
        this.setTitle(title);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        this.setLayout(new BorderLayout());


        this.textPanel = new TextPanel();
        this.buttonPanel = new ButtonPanel();

        this.buttonPanel.setTextListener(new TextListener(){
            public void deliverText(String text) {
                textPanel.appendText(text);
            }
        });     

        this.add(textPanel, BorderLayout.CENTER);
        this.add(buttonPanel, BorderLayout.SOUTH);
        this.setVisible(true);
    }

This is another approach I discovered while messing around with the code:

public class MainFrame extends JFrame implements TextListener{

    private TextPanel textPanel;
    private ButtonPanel buttonPanel;

    public MainFrame(String title){
        this.setTitle(title);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        this.setLayout(new BorderLayout());

        this.textPanel = new TextPanel();
        this.buttonPanel = new ButtonPanel();
        this.buttonPanel.setTextListener(this); 

        this.add(textPanel, BorderLayout.CENTER);
        this.add(buttonPanel, BorderLayout.SOUTH);
        this.setVisible(true);
    }

     public void deliverText(String text){
        this.textPanel.appendText(text);
     }

}

TextListener is an interface that basically communicates between a button and the text area so that when a button is pressed the text inside of that button will appear in the text area.

Are either of these methods considered a better approach? Is there actually a significant difference that I am just not aware of?

To me it seems that the second approach (where MainFrame implements TextListener) may be a more expensive way to handle things because we are passing an entire instance of MainFrame when all that is really needed is the information about TextListener. I imagine that in a larger program where MainFrame has a lot of components within it this would begin to be a problem. Is this the correct way to think about it? When I pass the argument "this" is a copy of MainFrame created wasting memory or is the same memory location of the original copy referenced?


Solution

  • Since you have a simple example here, it really isn't a problem which you use.

    There also a third option - create a seperate class that implements the interface on its own. That option allows a constructor to be defined for the listener. Sometimes that is useful.

    Now, this is only a reference, so no copies are made, so no more memory is used. I'd argue that not instantiating an anonymous class takes less resources.

    Basically, how I was explained this approach was when there were many different Buttons all with this added as the interface. In the implementation of the interface method, the source of the button was checked using a if statement. That takes less resources than adding a separate anonymous classes for handling the same event for each component.