Running a method after switching cards in cardlayout

前端 未结 2 1302
醉话见心
醉话见心 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.

    0 讨论(0)
  • 2021-01-24 06:19

    You can just use a ComponentListener for the panel(s). When the panel becomes the view of the CardLayout, it will fire a component event and handled by componentShown in your listener (as well as the panel taken out of view, handling the componentHidden). Call your start() method there. This way you don't have to explicitly call the start() when the panel changes, as it be called for you.

    See How to Write Component Listeners for more details.

    Here is a simple example.

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Main {
    
        private static final String PANEL_A = "panelA";
        private static final String PANEL_B = "panelB";
    
        CardLayout layout = new CardLayout();
        JPanel panel = new JPanel(layout);
        ComponentListenerPanel p1 = new ComponentListenerPanel(PANEL_A);
        ComponentListenerPanel p2 = new ComponentListenerPanel(PANEL_B);
        JButton b1 = new JButton(PANEL_A);
        JButton b2 = new JButton(PANEL_B);
    
        public Main() {
            panel.add(p1, PANEL_A);
            panel.add(p2, PANEL_B);
    
            b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    show(PANEL_A);
                }
            });
            b2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    show(PANEL_B);
                }
            });
            JPanel buttonPanel = new JPanel();
            buttonPanel.add(b1);
            buttonPanel.add(b2);
    
            JFrame frame = new JFrame();
            frame.add(panel);
            frame.add(buttonPanel, BorderLayout.PAGE_END);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public void show(String panelName) {
            layout.show(panel, panelName);
        }
    
        private class ComponentListenerPanel extends JPanel {
            private String panelName;
    
            public ComponentListenerPanel(String panelName) {
                this.panelName = panelName;
                addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentHidden(ComponentEvent evt) {
                        stop();
                    }
                    @Override
                    public void componentShown(ComponentEvent evt) {
                        start();
                    }
                });
            }
    
            public void start() {
                System.out.println(panelName + " started");
            }
    
            public void stop() {
                System.out.println(panelName + " stopped");
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 300);
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new Main();
                }
            });
        }
    }
    

    Note you haven't actually said where the start method is, so this code/answer is just assuming you have some start method in custom panel. Hope I guessed right. In the future, or even now, you should always post an MCVE so that we don't have to do all this guessing.

    0 讨论(0)
提交回复
热议问题