Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Loop through JLabel and change JLabel

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Loop through JLabel and change JLabel

    Hello Guys,

    Bit stuck here I'm trying to loop through a JLabel and change the label after sleeping for a second, the sleep is working fine but the JLable won't change it just stays static.

    Thanks

    here is my code replicated with the same problem

    import javax.swing.*;
     
    import java.awt.*;
     
    public class JpanelTest {
     
    	public static void main(String[] args){
     
    		try{
    		Thread myThread = new Thread();
    		JFrame frame = new JFrame();
     
    		frame.setSize(300,300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
     
    		Container contain = frame.getContentPane();
     
     
    		JPanel panel = new JPanel();
    		frame.add(panel);
    		for(int i = 1; i < 6; i++){
    		JLabel label = new JLabel("File number " + i + " is loaded");
    		panel.add(label);
    		myThread.sleep(1000);
    		contain.add(panel);
    		System.out.println("1 + " + i);
    		}
    		}
     
    		catch(InterruptedException e)
    	    {
    	    }
     
    }
    }

    I want it to look like "File number 1 is loaded"
    then clears and "File number 2 is loaded" etc

    Thanks
    Last edited by JoeBrown; April 11th, 2012 at 11:06 AM. Reason: solved


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop through JLabel and change JLabel

    Ok, I got it solved.

    I need to use the

    panel.validate();
    label.repaint();

    and then panel.remove(label);

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Loop through JLabel and change JLabel

    Glad you got it solved. As just a note, you might wish to read the following
    Threads and Swing
    You should do updates to Swing on the EDT

Similar Threads

  1. Adding JLabel on other Jlabel
    By mike416 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 29th, 2012, 11:50 AM
  2. Jlabel issues
    By Wadashe in forum Java Theory & Questions
    Replies: 2
    Last Post: February 27th, 2012, 02:39 PM
  3. JLabel
    By Poseidon in forum AWT / Java Swing
    Replies: 2
    Last Post: February 18th, 2012, 04:39 PM
  4. Change a JLabel value that is in another class?
    By jm24 in forum AWT / Java Swing
    Replies: 3
    Last Post: February 17th, 2012, 08:13 PM
  5. Everything but JLabel is displaying
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2011, 10:30 PM