Ron
Ron

Reputation: 154

How to set a Frame to display a new Panel in swing?

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);

Upvotes: 1

Views: 4355

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

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.

Upvotes: 4

Related Questions