Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:01:54+00:00 2026-06-01T15:01:54+00:00

Good evening ladies and gentlemen, I have a problem with Java Swing that I

  • 0

Good evening ladies and gentlemen,

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

  • I have one JFrame which uses BorderLayout, and many JPanels.
  • Every time I need to put up a new screen (i.e. from the Main Menu, when Search button is clicked, go to the Search Menu), I simply remove the component (JPanel) which is located in the center, and put the new screen (new JPanel) in the center instead.
  • This way, I don’t call all my header and footer objects every time I want to put up a new screen.

Everything works fine with this system except this little problem: I want to trigger some methods everytime I put up a new JPanel or change back to an existing JPanel (generally speaking, everytime a JPanel appears).

In order to do that, I tried to implement ComponentListener’s componentShown(ComponentEvent e) method, and added a ComponentListener to a JPanel which I put up in the center of my JFrame, and it did NOT work. After this, I did some research and found out that this componentShown (@ComponentListener) method only works when the visibilty of the JPanel is changed (from invisible to visible or the opposite). Unfortunately, I’m not changing the visibility of a JPanel, just replacing it with another one: removing the current one, and adding the new one. Below code illustrates how I replace the 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 here is what I have. I would really appreciate it if you could help me out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:01:55+00:00Added an answer on June 1, 2026 at 3:01 pm

    I think that this issue corresponding with HierarchyListener, for comparing

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening, i need to know how can i have one .h file that
Good Evening, The problem is that i have both xcode 3.2 and xcode 4
Good evening ladies and gentlemen! Searching for a cross-browser solution to embed commercial fonts
Good evening, In my app that I'm currently developing, I have a class that
Good evening, I am developing a set of Java classes so that a container
Good evening. I'm developing an android application that uses Facebook SDK. I've no problem
Good evening I have a JTable that I built with a TableModel how to
Good evening, here is what I am trying to achieve, I currently have a
Good Evening All, I have a textbox that needs to accept 14 digits plus
Good evening, people! I'm trying to solve a rather simple problem, but.. well, it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.