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]
# 1
I'd suggest you reproduce your problem in a small self-contained class and post the source here.I for one am not inclined to go downloading Jar files from wherever and then pick through piles of code just to find someone else's bug.
itchyscratchya at 2007-7-8 23:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 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

MauriPza at 2007-7-8 23:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 3

All you have to do is replace the content pane or call removeAll on the content pane. Based on your code though, I'd recommend you read the [url http://java.sun.com/docs/books/tutorial/uiswing/]Swing Tutorial[/url] because you'll learn alot more and learn faster than asking questions about things as relatively simple as this.

Jasprea at 2007-7-8 23:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 4

The basic code for adding and removing components on a visible GUI is:

somePanel.remove(....);

somePanel.add(...);

somePanel.revalidate();

somePanel.repaint(); // only required sometimes.

Basically the revalidate tells the panel that changes have been made to it so the LayoutManager is invoked and components are repositioned and repainted.

So you don't need to recreate the GUI from scratch.

camickra at 2007-7-8 23:08:26 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thank you, problem solved, I'll know read the tutorial suggested.thanks
MauriPza at 2007-7-8 23:08:26 > top of Java-index,Desktop,Core GUI APIs...