Question

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

Frame

Before play the game Picture of JDialog window of the game before playing

Before dispose Picture of JDialog window after playing for some time

After Dispose Picture of JDialog window 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**
                }
            }
        }
Was it helpful?

Solution

I have to guess, because there is at once too much information to try to read it all and too little information to solve your problem. The cause of the pieces that are appearing on the board from their previous positions, after they are supposed to be gone, is almost certainly not because of not 'disposing' of the pieces or from occupying memory after 'disposition'. The problem looks like you have a data structure that describes where pieces are and code that draws them in those places, and after resignation you are not clearing that data structure.

Good luck with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top