Search code examples
javabuttonjframedispose

Why cant I use the jFrame.dispose() method within a jButtonActionPerformed method?


I want my jFrame (printJFrame) to close (using the .dispose method) when the user clicks the cancel button, but idk why it isn't working. (FYI the cancelJButton is within the printJFrame class so thats why im really confused.....) Any advice?????

private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {
printJFrame.dispose(); }


Solution

  • This question looks somewhat similar: https://stackoverflow.com/a/2358454/5604890 Here the dispose() call is being made from inside a SwingWorker.

    Alternatively, if you just want to hide your JFrame (and show it again at a later time) you could simply use JFrame.setVisible(false)