Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help repainting my applet with a new panel

 
david mohr
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the code I have so far.
What I want to do is;
every second repaint/refresh the screen with a new panel.

Currently this kinda works with the appletviewer, I need to choose the menu item (restart) 3 times for it to start displaying the 2 panels.

Nothing happens when I user IE browser

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;


public class testApp1 extends Applet implements ActionListener, Runnable
{
JButton logonBtn;
JButton testBtn1;
JButton testBtn12;
JPanel mainPanel = new JPanel();;

Thread engine = null;
JPanel[] pnls = {createPanel(),createPanel2()};


public void init()
{

setSize(400,300);
setVisible(true);

Thread me = Thread.currentThread();


}


public void run() {
Thread me = Thread.currentThread();

while (true)
{
for(int i = 0; i < 2; i++)
{
try{

this.add(pnls[i]);
pnls[i].setVisible(true);

repaint();
Thread.sleep(1000);

pnls[i].setVisible(false);

this.remove(pnls[i]);

repaint();
}
catch (InterruptedException e) {}
}
}
}


public void start() {
engine = new Thread(this);
engine.start();
}

public JPanel createPanel()
{
JPanel mainPanel2 = new JPanel();
mainPanel2.add(testBtn1 = new JButton("asdf"));
mainPanel2.setLayout(new FlowLayout());
mainPanel2.setEnabled(false);
mainPanel2.setVisible(false);

return mainPanel2;

}

public JPanel createPanel2()
{
JPanel mainPanel22 = new JPanel();
mainPanel22.add(testBtn12 = new JButton("asdf22"));
mainPanel22.setLayout(new FlowLayout());
mainPanel22.setEnabled(false);
mainPanel22.setVisible(false);

return mainPanel22;

}

}


Thanks for your help.
David
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you remove component(s) from a visible container, you need to call validate() on that container; this is the signal to the layout manager to fix things up appropriately. Adding a call to validate() before your repaint() call should improve matters.

But ugh, what are you trying to do here exactly? Adding and removing components every second isn't the basis for any normal programming technique that I'm aware of.
 
david mohr
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for your help.

I am writing a automated test that will display a new panel every second. The program I am testing should recognize the new panel and fill it with the correct credentials.

David M
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic