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 8 of 8

Thread: repaint not updating screen

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post repaint not updating screen

    Hello,
    I am really green when it comes to Java, so this is probably extremely simple, but I have been struggling with it and just can't get it right. I wrote the simple program below to try to get a handle on it, but it does not work. I am trying to add another line of text when a button is pressed, and to update it every subsequent time that the button is pressed. I am working with the code below, and it only seems to repaint and add the last line when the stop method is invoked (when I bring the window down and pull it back up). Could somebody please tell me what I am missing?
    PS. I was planning to add more functionality like different messages depending on the number entered, but I haven't gotten that far, since I couldn't even get it to update once.
    Last edited by LoganCale; April 8th, 2012 at 08:04 PM. Reason: fixed code - not properly wrapped and re-posted that part below


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: repaint not updating screen

    When you change the contents of a container you must tell the container to redo the layout of the components that have been added to it. The Container class has a method you should call to tell it to redo the layout of its components.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint not updating screen

    Thank you for your response. I'm sorry to be so dull (at least I'm feeling that way right now), but I tried what's below and a few other configurations and am still having the same trouble. Is this what you meant, or did I miss the boat entirely?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener; 
     
    public class HelloMyFriends extends JApplet implements ActionListener 
    { 
    	 Container con = getContentPane();
         JLabel header;
    	 JTextField enterFriends;
    	 JButton btnFriend;
    	 double myCommunity; 
     
     	final int NUM_RANGES = 7;
     	int[] costRangeLimit = {1,5,25,50,200};
     	String[] status = {"you have a friend", "a good group", "It's a party", "That's a big party", "Now it's a community"};
     	String strNumFriends;
     	int numFriends;    	
     	int sub = NUM_RANGES-1;
     
     	JLabel friendStatus = new JLabel("Yes, you have a friend");
     
         public void init()  
         { 
        	 con.setLayout(new FlowLayout()); 
        	 header = new JLabel("How many friends do you have?");
             btnFriend = new JButton("Update friends");
    		 enterFriends = new JTextField ("1",6);
    		  con.add(header);
    		  con.add(enterFriends);
    		  con.add(btnFriend); 
     
              btnFriend.addActionListener(this); 
             } 
             public void paint(Graphics g) 
             { 
            	 super.paint(g);
     
            	 } 
     
            public void actionPerformed(ActionEvent e)  
             {  if (e.getSource()==btnFriend)
                  {
                      con.add(friendStatus);
                      con.repaint();
                      con.setLayout(getLayout());
                  }
     
              else if (e.getSource()!=btnFriend)  
              	 { 
                 }
                  repaint();
             }        
            public void stop()
            	{
            	repaint();
            	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: repaint not updating screen

    What method is the code calling to tell the container to do a lay out of its components?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint not updating screen

    I thought I was using
    con.setLayout(new FlowLayout());
    for that?...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: repaint not updating screen

    That sets the layout manager. You need to call the method that tells the container to have the layout manager re do the layout.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    LoganCale (April 8th, 2012)

  8. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: repaint not updating screen

    a-ha! I was forgetting about
     con.revalidate();
    Thank you so much!

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: repaint not updating screen

    Glad you got it to work.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  2. g.setColor or repaint is not working
    By sonicjr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 14th, 2011, 07:19 AM
  3. Help with repaint
    By AraHabs in forum AWT / Java Swing
    Replies: 5
    Last Post: November 5th, 2011, 04:40 PM
  4. Repaint,
    By Time in forum AWT / Java Swing
    Replies: 3
    Last Post: May 21st, 2010, 11:23 PM
  5. Repaint doesn't repaint?
    By PotataChipz in forum AWT / Java Swing
    Replies: 6
    Last Post: January 18th, 2010, 09:56 PM

Tags for this Thread