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

  • SEARCH
  • Home
  • 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 8992219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:54:12+00:00 2026-06-15T22:54:12+00:00

I’m obviously missing something here. How can I change a swing Frame to display

  • 0

I’m obviously missing something here.

How can I change a swing Frame to display an entirely new Panel?

As in either:
1. display an entirely new panel with new content
or
2. display the same panel cleared with my new added

I have displayed my new panel in an alternative program and it creates and displays correctly.

However when the panel with my buttons have previously been in the frame it never displays my new panel.

I call contentPane.revalidate(); a few times in my code to update my buttons which updates them flawlessly the problem only occurs when I’m trying to remove the old buttons and add new.

After having added my new panels to the contentPane I have tried:

contentPane.validate();
contentPane.revalidate();
contentPane.removeAll();        
contentPane.repaint();
setContentPane(contentPane);

but it never changes.

 @Override
public void actionPerformed(ActionEvent e) 
{
    String buttonPressed = e.getActionCommand();
    int pos = Integer.valueOf(buttonPressed);

    if (Control.model.cardsRemaining == 0) 
    {
        contentPane.removeAll();
        //contentPane.validate();
        //contentPane = new JPanel();
        //contentPane.add(createSuccess());
        contentPane = createSuccess();
        contentPane.revalidate();
        contentPane.repaint();      

        System.out.println("entered success");
    }

    else 
    {
        System.out.println("Cards left: " + Control.model.cardsRemaining);
        action = Control.model.ReceiveCardsTurned(pos);

        keypadArray[pos].setIcon(myIcons[pos]);
        currentTime.setText("" + Control.model.time);
        currentScore.setText("" + Control.model.score);

        System.out.println("this card: " + pos + "last card: "
                + Control.model.lastCard);

        if (action == "unturn") 
        {
            try 
            {
                Thread.sleep(1000);
            }

            catch (InterruptedException e1) 
            {
                e1.printStackTrace();
            }

            contentPane.revalidate();
            keypadArray[pos].setIcon(back);
            keypadArray[Control.model.lastCard].setIcon(back);
        }

        //System.out.println(action);
    }
}



private JPanel createSuccess() 
{
    //final 
    JPanel Success = new JPanel();
    JLabel image = new JLabel(success);
    Success.add(image);

    return Success; 
}

Also this is further up in my code:

ImageIcon success = new ImageIcon("icons/success.png");

And:

JPanel contentPane = new JPanel();

just tried:

contentPane = createSuccess(); 
frame.setContentPane(contentPane);  
frame.pack();                

line 268 is: frame.setContentPane(contentPane);

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at pairs.GUI.actionPerformed(GUI.java:268)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Thank you my friend that worked perfectly:

contentPane.removeAll();
contentPane.add(createSuccess());
setContentPane(contentPane);
  • 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-15T22:54:14+00:00Added an answer on June 15, 2026 at 10:54 pm

    You code assigns a new reference to the variable contentPane, this does not change what’s already on the screen, it simply moves what contentPane was referencing (presumably what’s on screen) to something else, leaving what’s on screen exactly where it is.

    Try adding the new panel to contentPane

    contentPane.removeAll();
    contentPane.add(createSuccess());
    

    If you you want to switch views, CardLayout is a better choice.

    If you simply want to “reset” the view, you’ll have to actually write code to return the contents of the view back to a “default” state. In this case, you’re actually better of creating a custom component that contains the required fields for the view and provide management methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.