Search code examples
javamultithreadingswingjframedispose

Disable a frame before dispose


if("Back".equals(e.getActionCommand()))
{
    escape.setVisible(false);
    paused = false;
    running = !running;
    Graphics g = start.getGraphics();
    g.dispose();
    Component glassPane = start.getGlassPane();
    glassPane.setVisible(!glassPane.isVisible());
    Main.main(null);
    Main.running = true;
    start.dispose();
}

This is my code for disabling my jframe, loading a new one, and disposing it. Whenever I do this, when I return to the frame I get strange graphical errors. One of the graphical errors!

How do I close the frame completly?


Solution

    • create private/public(depends of your code design) variable for JFrame instance

    • then you can't bothering with dispose of visible JFrame and then re_create a new one

    • create class or local instace that returns JPanel with game content

    • call JFrame#getContentPane#removeAll()

    • call JFrame#getContentPane#add(new GamePanel())

    • maybe there are easiest ways how to reset whatever (depends of your code design) to the start_up statuses