Want to make a button appear later in program
Hello,
I want to make a program with a small timeline. If one button is pressed, another one appears. I cant do it since repaint only works when i resize my window. This is what i typed:
Code:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class erbij extends Applet implements ActionListener {
private Button nummer1;
private Button nummer2;
private String tekst="";
public void init() {
nummer1= new Button("First one");
add(nummer1);
nummer1.addActionListener(this);
nummer2= new Button("Second one");
}
public void actionPerformed (ActionEvent drukken) {
if (drukken.getSource() == nummer1)
add(nummer2);
nummer2.addActionListener(this);
repaint();
if (drukken.getSource() == nummer2)
tekst = "You did it";
}
public void paint (Graphics g) {
g.drawString(tekst,100,100);
}
}
What did I do wrong? Why does repaint not repaint the second button if I pressed the first one.
Or is there an easier method to create a new button along the way.
It would be a great help if someone could assist!
Maarten.
Re: Want to make a button appear later in program
If you add components after the parent container has been realized (eg set to visible), you should call revalidate() on the container for those changes to take affect
Re: Want to make a button appear later in program
Call validate() to tell the layout manager to do a new layout.
Re: Want to make a button appear later in program
Thanks!
I didn't know that option yet. It works now.