cancelButton.setToolTipText("Return to the main menu."); cancelButton.setMnemonic(KeyEvent.VK_C); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String[] options = {"Yes", "No"}; int choice = JOptionPane.showOptionDialog(null, "Are you sure you " + "want to cancel the operation?\nAll the information will be lost.", "Return to the main menu", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (choice == YES_OPTION) { dispose(); //MainMenuUI mainMenu; // try { // UIManager.setLookAndFeel( // "javax.swing.plaf.metal.MetalLookAndFeel"); // mainMenu = new MainMenuUI("Main menu:", pl); // } catch (ClassNotFoundException | IllegalAccessException // | InstantiationException | UnsupportedLookAndFeelException ex) { // JOptionPane.showMessageDialog(null, ex.getMessage(), // "Error", JOptionPane.ERROR_MESSAGE); // } } } });