user2245494 user2245494 - 3 months ago 14
Java Question

What happens to the event object? Is it re-executing code?

I am learning object oriented programming from a book. I got pretty far but now I am stuck. Is

wExec
re-executing itself? I am not sure if this is even possible. Can someone clear this up for me. When I press the button "done" and exit executing the reference object I am so confused. Here is the code from each
class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RunApp {
public static void main(String[] args) {
MainApp app = new MainApp("hello world");
app.showApp();
}
}

class MainApp extends MmvcApp {
WmvcButton btnDone;
WmvcButton btnExit;

public MainApp(String title) {
super(title);
btnDone = new WmvcButton("Done", new WmvcExecutor() {
public void execute(ActionEvent event) {
System.out.println("done");
System.out.println("test 2");
}
});

btnExit = new WmvcButton("Exit", btnDone.getExecutor());

}
}

abstract class MmvcApp {
private static JFrame frame;
private static JPanel panel;
private static MmvcApp singletonApp = null;

public MmvcApp(String frameText) {
singletonApp = this;
initializeApp(frameText);
}

public void initializeApp(String frameText) {
frame = new JFrame(frameText);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 300));
frame.add(panel, BorderLayout.CENTER);
}

public static JFrame getFrame() {
return frame;
}

public static JPanel getPanel() {
return panel;
}

public static MmvcApp getApp() {
return singletonApp;
}

public void showApp() {
frame.pack();
frame.setVisible(true);
}
}

class WmvcButton extends WmvcController {
private JButton myButton;

public WmvcButton(String compText, WmvcExecutor wExec) {
super((JComponent) new JButton(), wExec);

myButton = (JButton) myComponent;

myButton.setText(compText);
myButton.addActionListener(this);
MmvcApp.getPanel().add(myButton);
}
}

class WmvcController implements ActionListener {
public JComponent myComponent;
private WmvcExecutor wExec;

public WmvcController(JComponent component, WmvcExecutor wExec) {
myComponent = component;
this.wExec = wExec;
}

public WmvcExecutor getExecutor() {
return wExec;
}

public void actionPerformed(ActionEvent event) {
if (wExec != null) {
wExec.execute(event);
System.out.println("test 1");
} else
System.out.println("not working ");
}
}

class WmvcExecutor {
public void execute(ActionEvent event) {
}
}

Answer

Both btnDone and btnExit have the same reference to WmvncExecuter. In these line here:

btnDone = new WmvcButton("Done", new WmvcExecutor() {
    public void execute(ActionEvent event) {
        System.out.println("done");
        System.out.println("test 2");
    }
});
btnExit = new WmvcButton("Exit", btnDone.getExecutor());

You are overriding the execute() method. It's the same as making a new class and overriding it there.

class WmvcExecuterTest extends WmvcExecuter{
    public void execute(ActionEvent event){
        System.out.println("done");
    }
}

So when ever you call execute() from a normal WmvnExecuter it wont do anything, but because you override execute(). It will perform the new code whenever execute() is called from that specific instance.

Comments