0

Ok, I'm working on a JApplet for my school project. What I want it to do is each time a JButton is clicked, "a menu button", it deletes current content of the container and then adds a new JApplet to the container. I have it sort-of-working, the only error I'm getting is that it's not repainting the contents of the container, but if I adjust the window (I'm using appletviewer to display it currently) it will display what I want it to display. Below is an example of the code I use for my actionPerformed method...

  public void actionPerformed(ActionEvent event)
  {
     if(event.getSource() == word_guess)//JButton 
     {
        WordGuess w = new WordGuess(); //Applet wanted to be displayed

        c.remove(main);//removes current content of container
        c.remove(side);
        c.setLayout(new GridLayout(1,0)); //changes Layout 
        c.add(w);  

        w.init(); //calls the init method of WordGuess
        repaint(); //I tried to see if repainting would help, and it didn't
     }
 }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Feek
  • 297
  • 3
  • 15
  • 1) *"I'm working on a JApplet for my school project"* Why code an applet as opposed to a desktop app.? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Oct 04 '13 at 19:55

2 Answers2

1

Call revalidate(); method at the end of operation.

Masudul
  • 21,823
  • 5
  • 43
  • 58
0

You should try to put repaint() outside the if block, maybe that will work??

ozzyzig
  • 709
  • 8
  • 19