Results 1 to 7 of 7
- 09-05-2012, 02:18 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 11
- Rep Power
- 0
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 } }
- 09-05-2012, 02:51 AM #2
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.
- 09-05-2012, 02:55 AM #3
Member
- Join Date
- Aug 2012
- Posts
- 11
- Rep Power
- 0
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?
- 09-05-2012, 03:04 AM #4
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.
- 09-05-2012, 03:08 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 11
- Rep Power
- 0
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.
- 09-05-2012, 03:14 AM #6
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.
- 09-05-2012, 06:14 AM #7
Similar Threads
-
FrameView doesn't show new JPanel until I resize
By metazone in forum SWT / JFaceReplies: 3Last Post: 01-05-2012, 03:52 PM -
my JDialog doesn't want to resize as I say :P
By patr1c1a in forum New To JavaReplies: 3Last Post: 11-25-2010, 02:12 PM -
How to resize the Label when the Shell resizes using a Layout
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:40 PM -
How to resize the Label when the Shell resizes using a Listener mechanism
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks