addPropertyChangeListener() 从 JTabbedPane 上的 JPanel 到 JFrame

addPropertyChangeListener() to JFrame from a JPanel on a JTabbedPane

我正在创建一个带有多个选项卡 (JTabbedPane) 的程序,在选项卡中有一个 JPanel,其中包含我所有的内容。当我按下开始 (JButton) 时,我创建了一个新的 Task 实例(一个 class 扩展了 Swingworker )并且我想将我所有的 menuItems 设置为启用(false)。这是在 JFrame 上。

但我无法从 Jpanel 访问 JFrame

控制器class:

public class Controller {
    private Task task;

    public Controller() {
        newTask();
    }

    public void newTask(){
        task = new Task();
    }

    public Task getTask() {
        return task;
    }
}

帧class:

public class Frame extends JFrame implements PropertyChangeListener {

    private Controller controller;

    public Frame(String title, Controller controller) {
        super(title);
        this.controller = controller;
        controller.getTask().addPropertyChangeListener(this);
        JTabbedPane tabbedPane = new JTabbedPane();
        TabbedPane0 tabbedPane0 = new TabbedPane0(controller);
        JPanel jPanel = new JPanel();
        tabbedPane.add(tabbedPane0);
        tabbedPane.add(jPanel);
        add(tabbedPane);
        setSize(400, 500);
        setVisible(true);
        controller.getTask().TestPropertyChange();

    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName() == "changed") {
            System.out.println("Changed property, disabled all MenuItems that I added on this FRAME");
        }else if(evt.getPropertyName().equals("test")){
        System.out.println("Tested");
    }
    }
}

TabbedPane0 class:

public class TabbedPane0 extends JPanel {

    private Controller controller;

    public TabbedPane0(Controller controller) {
        this.controller = controller;
        JButton  button = new JButton("Start");
        add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                controller.newTask();
                /*My frame needs to be added to the TaskPropertyChangeListeners but I can't acces it*/
                controller.getTask().addPropertyChangeListener(Frame);
            }
        });
    }
}

任务class:

public class Task extends SwingWorker<Void, Void> {

    @Override
    protected Void doInBackground() throws Exception {
        System.out.println("Task Is executed");
        return null;
    }

    public void TestPropertyChange(){
        firePropertyChange("test", null,null);
    }
}

运行 class:

public class Run {

    public static void main(String[] args) {
        Controller controller = new Controller();
        new Frame("Whosebug Example TabbedPane", controller);
    }
}

我发现我可以使用这个:

这将获取我的父 JFrame,其中我的 JTabbedPane/Jpanel 添加到

controller.getTask().addPropertyChangeListener((JFrame) SwingUtilities.getWindowAncestor(FolderCreatorTab.this));