• Post Reply Bookmark Topic Watch Topic
  • New Topic

Applet Problem...  RSS feed

 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying an applet I have been adding to through my study book, and it runs, but not right.. It's supposed to take the name I put in and display it back on the applet screen, but after I click the button I get nothing.. Here's the code:


Did I miss something, or is my pc just too slow to return the new output?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when adding/removing components in this manner you need to call validate()
(or revalidate() for swing components), and sometimes repaint() - safer to add it for all

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After adding components later in the applet lifecycle (like you are doing in the in the action handler), the applet layout needs to be refreshed. This is done with a call to "invalidate();".
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't learned the repaint method yet, but just did the invalidate and validate methods.. Thanks, I got it working now, plus I just read about the remove method.. Here's the working code..
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as a side thought, why do I need two method bodies instead of one like most of the programs I have made using Java? Is it just with Applets or what??
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeremy Parsons:
as a side thought, why do I need two method bodies instead of one like most of the programs I have made using Java? Is it just with Applets or what??


I don't understand the question. What two method bodies?
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void init()
{ //body #1 statements;}

public void actionPerformed()
{ //body#2 statements;}

Or blocks of code if that's what you call them.. I learned they are method bodies in the study units I am working with...
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's good software practice to carve up your program into classes and methods, with each having a clearly defined task. You'll get to that later in your studies. Code that does initialization has really nothing to do with code for handling an event. Besides, the init code is called once, while the event handler is called multiple times. You wouldn't want the init stuff to happen over and over again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!