Question

I have a JFrame which contains a JPanel with the JButton "Press Me"

enter image description here

Pressing the "Press me" button will change to another JPanel(SecondPanel) within the same JFrame

enter image description here

I have a Timer function which will add another card to the JFrame , the function will run after 6 seconds.

The problem is that I must expand or minimize the JFrame window before the card will appear in the JFrame. I want the card to appear in the JFrame without expanding or minimizing the JFrame window

can someone help me resolve this issue and explain to me what is happneing

Thanks

Main class used to run the project

package testing;

import java.io.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;


public class Testing extends JPanel 
{



    public static void main(String[] args) 
    {
        frame = new JFrame();
        LoginPanel lp = new LoginPanel();
        frame.add(lp);
        frame.pack();
        frame.validate();
        frame.setVisible(true);

    }

   static JFrame frame;
}

LoginPanel class

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

public class LoginPanel extends JPanel
{
    LoginPanel()
    {
        Loginbtn = new JButton("Press Me");
        Loginbtn.addActionListener(new LoginButtonListener());
        add(Loginbtn);


    }

     private class LoginButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {

            SecondPanel sp = new SecondPanel();
            Utility.ChangePanel(sp);
            sp.run();
        }
    }

    JButton Loginbtn;

}

SecondPanel class

package testing;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class SecondPanel extends JPanel
{
    SecondPanel()
    {
        setLayout(new GridLayout(2,2));

        //set deck image
        File deckfile = new File("./src/testing/Ace_Club_1_1.png"); //deck image file location

        try
        {    
          Deckimg = ImageIO.read(deckfile); //read deck image


        }

        catch (IOException e)
        {

        }

         Image scaledInstance = Deckimg.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
         DeckLabel = new JLabel(new ImageIcon(scaledInstance));
         add(DeckLabel);

    }

    public void run()
    {
        Timer timer = new Timer(5000, new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent arg0) 
            {
                // Code to be executed
                File Diamondfile = new File("./src/testing/Ace_Diamond_1_1.png"); //deck image file location

        try
        {    
          Diamondimg = ImageIO.read(Diamondfile); //read deck image


        }

        catch (IOException e)
        {

        }

         Image scaledInstance = Diamondimg.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
         DiamondLabel = new JLabel(new ImageIcon(scaledInstance));
         add(DiamondLabel);

            }
        });
        timer.setRepeats(false); // Only execute once
        timer.start(); // Go go go!



    }

    JLabel DeckLabel;
    JPanel DeckPanel;
    BufferedImage Deckimg;

    JLabel DiamondLabel;
    JPanel DiamondPanel;
    BufferedImage Diamondimg;



}

Utility class used to switch JPanels within the JFrame

package testing;

import java.security.MessageDigest;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Utility 
{    
    public static void ChangePanel(JPanel jp)
    {


        testing.Testing.frame.getContentPane().removeAll();
        testing.Testing.frame.add(jp);
        testing.Testing.frame.validate();


    }

}
Was it helpful?

Solution

After this line:

add(DiamondLabel);

add:

revalidate();
repaint();

This will tell the containing JPanel to re-layout all components it has, and then repaint itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top