Running a method after switching cards in cardlayout

前端 未结 2 1323
醉话见心
醉话见心 2021-01-24 05:27

I\'m sure someone has asked this question before, but my google-fu is not strong today.

I have a JFrame that uses a CardLayout as its manager. How do I run a \"Start\" m

2条回答
  •  轮回少年
    2021-01-24 06:15

    I have an interface setup for each of the JPanels so that the method name will be identical in each

    So then the problem is getting the current panel that is visible when the panels are swapped so you can invoke the method.

    Check out Card Layout Focus for a class that extends CardLayout to provide a few helper methods to add additional functionality for the CardLayout. You would use the getCurrentCard() method.

    So your changePane(...) method might be something like:

    public final void changePanel(final WindowNames windowName) {
        //view.getCardLayout().show(view.getBasePanel(), windowName.getValue());
        RXCardLayout layout = view.getCardLayout();
        layout.show(view.getBasePanel(), windowName.getValue());
        MyInterface panel = (MyInterface)layout.getCurrentCard();
        panel.someMethod(...);
     }
    

    Of course you would also need to use the RXCardLayout as the layout manager for your main panel.

提交回复
热议问题