Results 1 to 7 of 7
  1. #1
    gregsakas is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Why doesn't the label appear until I resize the applet?

    I am using Eclipse and when I initialize the applet, it displays the labels in the init() function fine but for the labels inside the keyPressed() function, I have to resize the applet to get them to appear after I press enter. Why is this?
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Test extends Applet implements KeyListener {
    	
    	Label greeting = new Label("Reaction Test");
    	Font big = new Font("TimesRoman", Font.BOLD, 45);
    	boolean start = false;
    	public void init () {
    		setFocusable(true);
    		greeting.setFont(big);
    		add(greeting);
    		add(new Label("Press Enter to begin."));
    		this.addKeyListener(this);
    		requestFocus();
    	}
    	public void keyPressed(KeyEvent ke) {
    		add(new Label("TEST"));
    		if(ke.getKeyCode()==KeyEvent.VK_ENTER) 
    			start = true;
    		if(start==true) {
    			add(new Label("Once you see a letter appear on the screen,"));
    			add(new Label("press the shown letter as soon as possible.")); 
    			add(new Label("Press any key to begin."));
    		}
    	}
    	@Override
    	public void keyReleased(KeyEvent ke) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void keyTyped(KeyEvent ke) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Why doesn't the label appear until I resize the applet?

    You need to tell the container the labels have been added to so that it can re do the layout and make them visible.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gregsakas is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Why doesn't the label appear until I resize the applet?

    I'm sorry, I'm new to java. How would I do that?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Why doesn't the label appear until I resize the applet?

    Look at the Container class's methods in the API doc. You need to call one of them to tell the container to layout its contents again.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gregsakas is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Why doesn't the label appear until I resize the applet?

    Thank you so much. All it took was this.doLayout(); right after I added the strings.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Why doesn't the label appear until I resize the applet?

    The API doc for doLayout says:
    Most programs should not call this method directly, but should invoke the validate method instead.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Why doesn't the label appear until I resize the applet?

    Why are you using java.awt.Applet in this day and age? The Swing replacement, javax.swing.JApplet is morethan 10 years old!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. FrameView doesn't show new JPanel until I resize
    By metazone in forum SWT / JFace
    Replies: 3
    Last Post: 01-05-2012, 03:52 PM
  2. my JDialog doesn't want to resize as I say :P
    By patr1c1a in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 02:12 PM
  3. Replies: 0
    Last Post: 07-11-2008, 04:40 PM
  4. Replies: 0
    Last Post: 07-11-2008, 04:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •