• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why is the component not shown

 
sushant prabhu
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my code is
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//<applet code = "addcomp.class" width=400 height=400></applet>
public class addcomp extends Applet
{
Button b;
public void init()
{
b = new Button("click to add a label");
b.addActionListener(this);
add(b);
}

public void actionPerformed(ActionEvent e)
{
add(new Label("mylabel"));
}
}
my problem is whenever i click the button the label which is added is not immediateley shown on the applet. i have to manually resize or iconify the applet after which the label becomes visible on the applet. I tried calling the repaint() method manually after inside the actionPerformed() method but to no vail.
How can i acheive this
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call the validate() method instead of the repaint() method. Repaint just paints the screen. Validate tells the layout manager to re-configure the layout( which you'll have to do if you are adding a new component on-the-fly... ).

-Nate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic