Listening / Handling JPanel Events

Good evening ladies and gentlemen,

I have a problem with Java Swing that I cannot solve, maybe you can help me. Here he is:

  • I have one JFrame that uses BorderLayout and many JPanels.
  • Every time I need to set a new screen (i.e. from the main menu, when the search button is clicked, go to the search menu), I simply remove the component (JPanel) that is in the center and the new screen (new JPanel) in center instead.
  • This way I don't name all of my header and footer objects every time I want to place a new screen.

Everything works fine with this system, except for this little problem: I want to run some methods every time I install a new JPanel or change back to an existing JPanel (generally speaking, a JPanel appears every time).

For this I tried to implement the ComponentListener componentShown (ComponentEvent e) and added the ComponentListener to the JPanel which I placed at the center of my JFrame and it did NOT work. After that I did some research and found out that this componentShown (@ComponentListener) method only works when the visibility of the JPanel changes (from invisible to visible or vice versa). Unfortunately I am not changing the visibility of the JPanel, just replacing it with another one: removing the current one and adding a new one. Below code illustrates how I am replacing JPanels.

// Get the JPanel located in the center of our JFrame
JPanel currentView = (JPanel) myFrame.getContentPane().getComponent( 2 );

if ( currentView != null )
{
   // Remove it from the JPanel         
   myFrame.getContentPane().remove( currentView );
}

// Add the new JPanel    
myFrame.getContentPane().add( otherView, BorderLayout.CENTER );

// Pack the JFrame and show it
myFrame.pack();

      

So, this is what I have. I would really appreciate it if you could help me.

+3


source to share


2 answers


I think this question corresponding to the HierarchyListener is for comparison



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

public class ContainerListener extends JFrame {

    private static final long serialVersionUID = 1L;

    public ContainerListener() {
        super("Test");
        setContentPane(new TestPanel());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] parameters) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ContainerListener containerListener = new ContainerListener();
            }
        });
    }

    private class TestPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        TestPanel() {
            setLayout(new FlowLayout(FlowLayout.LEFT));
            add(new JButton(new AbstractAction("Add label") {

                private static final long serialVersionUID = 1L;
                private int n = 0;

                @Override
                public void actionPerformed(ActionEvent event) {
                    TestPanel.this.add(new JLabel("Label " + ++n));
                    validate();
                }
            }));
            addHierarchyListener(new HierarchyListener() {

                @Override
                public void hierarchyChanged(HierarchyEvent e) {
                    System.out.println("Components Change: " + e.getChanged());
                    if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
                        if (e.getComponent().isDisplayable()) {
                            System.out.println("Components: " + e.getChanged());
                        } else {
                            System.out.println("Components: " + e.getChanged());
                        }
                    }
                }
            });
            addContainerListener(new ContainerAdapter() {

                @Override
                public void componentAdded(ContainerEvent event) {
                    System.out.println("componentAdded : " + event.getChild() + "containerName" + " was added");
                }
            });

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }
}

      

+2


source


I highly recommend listening to the advice given by @Jeffrey, but if you continue with this project then perhaps follow the ContainerListener

interface might be helpful.



If in doubt, consult the API.

+2


source







All Articles