destroyGUI before createGUI
Hi all,
I have a problem with a simple game I'm working on.
Essentially every time I click on the "Nuova Partita" (new game) button I need to reset a few important variables and delete almost every object on the default frame, to then rebuild everything. But I haven't being able to do it
I've tried the command frame.removeAll() before the createAndShowGUI() in the "Nuova Partita" button listener but it simply remove everything without rebuilding it later. Maybe I have to add some sort of waiting command but don't know how to.
I've uploaded the Jar file with all the sources here:
http://4filehosting.com/file/1521/JSquares-jar.html
please take a look
Thanks
[717 byte] By [
MauriPza] at [2007-11-26 16:41:25]

# 2
Sorry, you are right
this code is enough to reproduce the error
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Principe {
static JFrame frame = new JFrame("Principe");
static int xcomps =10;
static boolean caricaG = false;
static int nGiocatori=0;
public void popolaCP(Container CP){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
for (int i=0;i< xcomps; i++){
panel.add(new JButton());
}
CP.add(panel);
}
private static void createAndShowGUI() {
//Create and set up the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
final JTextField numX = new JTextField("10");
JButton niu = new JButton("Nuova Partita");
panel2.add(numX);
panel2.add(niu);
niu.addMouseListener(new MouseListener(){
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
caricaG = true;
try{
xcomps = Integer.parseInt(numX.getText());
}catch(NumberFormatException f){
xcomps = 10;
}
createAndShowGUI();
}
});
if (caricaG){
Principe demo = new Principe();
panel.removeAll();
demo.popolaCP(panel);
}
frame.add(panel2,BorderLayout.NORTH);
frame.add(panel,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
all you need to do is click on "Nuova Partita", you'll see 10 new buttons at the bottom of the frame, than if you change the "10" in the TextField for a smaller number (let's say a 5) and click again on that button, and then you increase the width of the frame you should see 15 smaller buttons and 3 "Nuova Partita" buttons.
How can I delete those older objects?
thanks