How do i completely close a JDialog window without closing the Frame one?
I already tried using dispose(), but seems it hides the JDialog instead of realeasing it's memory, althought i believe this is not truth acoording to java API.
Is it correct to create a new JDialog when i hit the button from the Frame window, then i create a Menu inside JDialog class and pass this JDialog object to the Menu using a set method. When i choose the option of the menu "GiveUp", I try to close JDialog object using dispose(). Am i doing something wrong, or does anyone have a tip of what kind of problem i am having? I have already spent may hours trying to solve this problem
Ps: This is a chess game which have the option to giveup during the current match and start a new one, but althouth some of the objects are lost after the dispose, most of them continue at the same position of the screen or in over another object. Everytime i use System.exit(0) the game runs perfectly but when i use the dispose it always gives some problem.
Frame
Before play the game
Before dispose
After Dispose
public class Janela extends JFrame implements ActionListener {
JDialog d1;
public Janela() {
super("Jogo de Xadrez - v2.0");
button = new JButton("Abrir JDialog");
button.addActionListener(this);
this.getContentPane().add(button, BorderLayout.SOUTH);
this.setLocationRelativeTo(null);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
@Override
public void actionPerformed(ActionEvent ae) {
d1 = new TelaJogo();
**//HERE I INTEND TO CREATE A NEW JDIALOG WINDOW EVERY TIME I CHOOSE THE OPTION "GIVEUP" FROM THE MENU BAR**
}
public class TelaJogo extends JDialog {
private static int i=0;
private Menu menu;
public TelaJogo() {
System.out.println(i++);
this.setTitle("Jogo de Xadrez - v2.0");
JDialog.setDefaultLookAndFeelDecorated(true);
setSize(1000, 700);
setLocation((Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - getWidth() / 2, (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - getHeight() / 2);
pack();
menu = new Menu();
menu.setTela(this); // I PASS TO THE MENU THE JDIALOG OBJECT
setJMenuBar(menu.getMenu());
setVisible(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
}
Some methods and important parts of Menu Class:
public final class Menu implements ActionListener {
private static JMenuBar mb;
private JMenu menuArquivo;
JMenuItem sair, giveUp, comoJogar, sobre;
static TelaJogo tela;
public void setTela(TelaJogo t) {
tela = t;
}
public JMenuBar getMenu() {
return mb;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(sair)) {
int opcao = JOptionPane.showConfirmDialog(null, "Tem certeza que deseja sair do jogo?", "Sair do Jogo", JOptionPane.YES_NO_OPTION);
if (opcao == 0) {
System.exit(0);
}
} else if (e.getSource().equals(sobre)) {
JOptionPane.showMessageDialog(null, "Sobre");
} else if (e.getSource().equals(giveUp)) {
int opcao = JOptionPane.showConfirmDialog(null, "Tem certeza que deseja desistir do jogo?", "GiveUp", JOptionPane.YES_NO_OPTION);
if (opcao == 0) {
tela.setVisible(false);
tela.dispose();
**//HERE I TRY TO CLOSE THE JDIALOG WINDOW I'D LIKE TO USE SOMETHING LIKE SYSTEM.EXIT(0), BUT IT CAN'T CLOSE THE FRAME WINDOW**
}
}
}