• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Applet Problem...

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What's that smell? Hey, sniff this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic