Hi All,

I am a newbie at java and need some help. I have a JFrame setup where basically I want to show two pictures. I have everything setup however at sleep, timers ect dont currently work and I dont know why. The snipplet of code below should show imagePanel1 first then after 5seconds remove imagePanel1 then add imagePanel2 to the frame. However when I start it the first 5seconds shows nothing but a transparent frame then it shows imagePanel2 after 5seconds. Anyone know why this might be happening and how to fix it. I've already tried to use Thread.sleep(5000); but still no luck and same is happening.

static class previewAction implements ActionListener {
	
	final int seconds = 5;
	final long start = System.currentTimeMillis();
	
	public void actionPerformed(ActionEvent e) {
		
		previewFrame.setVisible(true);
		imageLabel1.setIcon(imageIcon1);
		imagePanel1.add(imageLabel1);
		imageLabel2.setIcon(imageIcon2);
		imagePanel2.add(imageLabel2);
		previewFrame.getContentPane().add(imagePanel1);
		
		
		long a1 = System.currentTimeMillis();   
		while(true) {   
		       long b1 =  System.currentTimeMillis();   
		       if(b1-a1 >= 5000) break;   // Elapsed 5s   
		       previewFrame.getContentPane().remove(imagePanel1);
		       previewFrame.getContentPane().add(imagePanel2);  
		}  
	}
}

Google the Swing Event Dispatch Thread - this is a standard problem that's well documented - but basically your ActionPerformed method blocks all screen updates etc until it has finished.
The right way to handle delays in a Swing GUI is to use a javax.swing.Timer - once again this is well documented on the web.

commented: Quick and helpful. Thanks again +0

Will it still work using a javax.swing.Timer inside the ActionPerformed method blocks?

and thanks for the reply

This is well documented on the web,with lots of examples, but basically you need to split this into two methods:
1. In the first method you display the first panel and start a Timer with a 5 second delay
2. When the Timer fires it calls a second method in which you remove the first panel and display the second one.

cheers James thanks for your help. Understand what to do now.

Okey now I've tried doing a swing.timer which James proposed but im still having trouble. It only shows the first image straight away now so I dont think my timer is working. Anyone have a clue where im going wrong?

(Update 1)I have put a System.out.println in my timers if statement and it shows up in the console so I dont know why the two commands below are not working still.

previewFrame.getContentPane().remove(imagePanel1);
previewFrame.getContentPane().add(imagePanel2);

(Update 2)I have used the previewFrame.repaint(); and this removes the imagePanel1 and shows a gray empty JFrame but it is not adding the imagePanel2 to display the 2nd image. Am I doing something wrong?

(Update 3)SOLVED! WORKING! I missed out previewFrame.validate(); It now works perfect! :)

static class previewAction implements ActionListener {
	
	public void actionPerformed(ActionEvent e) {
		
		previewFrame.setVisible(true);
		imageLabel1.setIcon(imageIcon1);
		imagePanel1.add(imageLabel1);
		imageLabel2.setIcon(imageIcon2);
		imagePanel2.add(imageLabel2);
		previewFrame.getContentPane().add(imagePanel1);
		
		t.start();
	}
}

static javax.swing.Timer t = new javax.swing.Timer(delay, new ActionListener() {
    
	public void actionPerformed(ActionEvent e) {
    	if (slidecounter == 1) {
    	    previewFrame.getContentPane().remove(imagePanel1);
    	    previewFrame.getContentPane().add(imagePanel2);
             previewFrame.repaint();
    	    System.out.println("Test");
    	    slidecounter++;
    	} if (slidecounter == 2) {
    		//add code later
    		slidecounter++;
    	} if (slidecounter == 3) {
    		t.stop();
    	}
    }
 });

Well done Steveo. Isn't it doubly satisfying when you get something to work AND you know it's done "right", not a bodge?

Haha thanks James, it is great to finally getting it working. Thanks for pointing me in the right direction :D Now I need to do some transition effects on these images so hopefully it aint too much work :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.