Results 1 to 4 of 4
  1. #1
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    24
    Rep Power
    0

    Default 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:
    Java 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.
    Last edited by Norm; 12-14-2011 at 03:59 PM. Reason: added code tags

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Want to make a button appear later in program

    Call validate() to tell the layout manager to do a new layout.

  4. #4
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    24
    Rep Power
    0

    Default Re: Want to make a button appear later in program

    Thanks!
    I didn't know that option yet. It works now.

Similar Threads

  1. Replies: 4
    Last Post: 07-23-2011, 08:08 PM
  2. Make GUI Button Open Another GUI
    By Feriscool in forum New To Java
    Replies: 7
    Last Post: 06-16-2011, 03:16 PM
  3. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 11:32 AM
  4. Make the Button Work
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 1
    Last Post: 02-27-2010, 10:52 AM
  5. Make java applet a button
    By hervey in forum New To Java
    Replies: 31
    Last Post: 10-30-2008, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •