setVisible() problem after button click
Hello,
I am having a big problem with the setVisible() method. It seems not
to work when an event such as a button click is involved in calling
it.
///// Pseudocode /////
display_messeges() {
loop {
add components to JFrame....
setVisible(true);
Sleep for a few seconds
}
}
The confusing thing is this will work when called from main, but does
not update the screen when called from actionPerformed method.
Am I doing somthing stupid here?...... it is driving me crazy
the test code is below...
Thanks
Sam
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Random;
public class set_visible_test extends JFrame implements ActionListener
{
private static set_visible_test demo;
private JLabel test_label;
private JButton button;
private JPanel test_panel;
Container window;
private int button_pressed=0;
public static void main(String[] args) {
demo = new set_visible_test();
demo.setSize(500,500);
demo.createGUI();
}
private void createGUI() {
// CREATE AND ADD A BUTTON TO THE FRAME
if (button_pressed ==0){
setDefaultCloseOperation(EXIT_ON_CLOSE);
window = getContentPane();
window.setLayout(new BoxLayout(window, BoxLayout.PAGE_AXIS));
test_panel = new JPanel(new FlowLayout());
button = new JButton("CLICK HERE");
button.addActionListener(this);
test_panel.add(button);
window.add(test_panel);
demo.setVisible(true);
}
else {
////// WHEN THE BUTTON IS CLICKED DISPLAY A MESSEGE
////// AND SLEEP FOR 5 SECONDS
////// THEN DISPLAY SECOND MESSEGE
System.out.println("button clicked");
setDefaultCloseOperation(EXIT_ON_CLOSE);
window = getContentPane();
window.setLayout(new BoxLayout(window, BoxLayout.PAGE_AXIS));
test_panel = new JPanel(new FlowLayout());
test_label = new JLabel("FIRST STAGE");
test_panel.add(test_label);
window.add(test_panel);
demo.setVisible(true);
/////////////////// this setVisible(true) command is not
working !!!!!!!!!!!!!!!
try {Thread.sleep(5000);} catch (InterruptedException e){}
setDefaultCloseOperation(EXIT_ON_CLOSE);
window.removeAll();
window = getContentPane();
window.setLayout(new BoxLayout(window, BoxLayout.PAGE_AXIS));
test_panel = new JPanel(new FlowLayout());
test_label = new JLabel("SECOND STAGE");
test_panel.add(test_label);
window.add(test_panel);
demo.setVisible(true);
}
}
public void actionPerformed(ActionEvent event) {
button_pressed=1;
demo.createGUI();
}
}