我正在尝试从内部类触发事件,但它不起作用。这是我的代码:
抽象模型:
public abstract class AbstractModel {
public PropertyChangeSupport propertyChangeSupport;
public AbstractModel() {
propertyChangeSupport = new PropertyChangeSupport(this);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue,
newValue);
}
}
模型:
public class GUImodel extends AbstractModel {
// Variables
private final ArrayList tempResultsTable = new ArrayList();
private static boolean done;
//
// RUN PROGRAM
//
public ArrayList run(ArrayList iF) {
try {
final BackgroundThread myThread = new BackgroundThread();
myThread.init(iF);
myThread.execute();
} catch (Exception e) {
e.printStackTrace();
}
return tempResultsTable;
}
public void done() {
System.out.println("done() called");
boolean oldValue = done;
done = true;
firePropertyChange(GUIcontroller.DONE_PROPERTY, oldValue, done);
}
class BackgroundThread extends SwingWorker<Void, Void> {
private ArrayList inputsFilesDataList;
public void init(ArrayList iF) {
inputsFilesDataList = iF;
done = false;
}
@Override
public Void doInBackground() throws Exception {
for (int i = 0; i < inputsFilesDataList.size(); i++) {
System.out.println(i);
}
return null;
}
@Override
protected void done() {
try {
boolean oldValue = done;
done = true;
firePropertyChange(GUIcontroller.DONE_PROPERTY, oldValue, done);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
看法:
public class GUIview{
...
public void propertyChange(final PropertyChangeEvent event) {
if (event.getPropertyName().equals(GUIcontroller.DONE_PROPERTY)) {
String newTab = (String)event.getNewValue();
updateTab(newTab);
}
}
...
}
抽象控制器:
public abstract class AbstractController implements PropertyChangeListener {
public final ArrayList<AbstractFrame> registeredViews;
public final ArrayList<AbstractModel> registeredModels;
public AbstractController() {
registeredViews = new ArrayList();
registeredModels = new ArrayList();
}
public void addModel(AbstractModel model) {
registeredModels.add(model);
model.addPropertyChangeListener(this);
}
public void removeModel(AbstractModel model) {
registeredModels.remove(model);
model.removePropertyChangeListener(this);
}
public void addView(GUIview view) {
registeredViews.add(view);
}
public void removeView(AbstractFrame view) {
registeredViews.remove(view);
}
@Override
public void propertyChange(PropertyChangeEvent event) {
for (AbstractFrame view : registeredViews) {
view.propertyChange(event);
}
}
}
控制器
public class GUIcontroller extends AbstractController {
public static final String DONE_PROPERTY = "done";
ArrayList inputsFilesList = m_model.loadFromExcel();
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equals(GUIcontroller.DONE_PROPERTY)) {
m_view.getResultsModel().updateResultsTableDataList(
m_model.getTempResultsTable());
} else {
for (AbstractFrame view : registeredViews) {
view.propertyChange(event);
}
}
}
public runProgram(){
m_model.run(inputsFilesList);
}
}
主要的。
public class GUImain {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
createGUI();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void createGUI() {
InputsModel inputsModel = new InputsModel();
ResultsModel resultsModel = new ResultsModel();
GUImodel model = new GUImodel();
GUIcontroller controller = new GUIcontroller();
controller.addModel(model);
GUIview view = new GUIview(controller, model, inputsModel, resultsModel);
controller.addControllerListerners();
view.setVisible(true);
}
}
关于这个问题的任何想法?
我需要在后台线程中运行一些方法,因此我使用了一个扩展 SwingWorker 的内部类。一旦该线程完成,我需要触发一个事件来向我的控制器报告一些更改。
done() 方法中的“firePropertyChange(...)”行没有被执行。
相关问题:如果某个类ClassA扩展了ClassAA,并且其内部类ClassB扩展了ClassBB,那么内部类ClassB是否也扩展了ClassAA?