How to exit Application from JMenu item? - java

From the Jframe I have a menu, inside it I have a menu item called Exit. I want the program so when exit is clicked the application exits. but before a JOptionPane appears to ask if you would like to exit. I tried this but it won't work.
private void jExitActionPerformed(java.awt.event.ActionEvent evt)
{
String toExit = String.valueOf(jExit);
if(jExit.equals(evt.getActionCommand())){
int dialogButton = JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog (null, "Would You Like to Exit?","Warning",dialogButton);
if(dialogButton == JOptionPane.YES_OPTION){
System.exit(0);
}
}
}

Try these changes I've shown below. Basically I've taken showConfirmDialod method's return value to response variable. And use that in the if condition.
private void jExitActionPerformed(java.awt.event.ActionEvent evt)
{
String toExit = String.valueOf(jExit);
if (jExit.equals(evt.getActionCommand()))
{
int dialogButton = JOptionPane.YES_NO_OPTION;
int response = JOptionPane.showConfirmDialog(null, "Would You Like to Exit?", "Warning", dialogButton);
if (response == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}

This is the answer:
private void jExitActionPerformed(java.awt.event.ActionEvent evt) {
int response = JOptionPane.showConfirmDialog(this,"Do you want to Exit? ",
"Confirm",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}

Related

JFrame closing on dialog

public static int clickOnExit() {
int dialogButton=JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog(null, sharedConstants.exitMessage,"Confirm",dialogButton);
if(dialogButton == JOptionPane.YES_OPTION){return JFrame.EXIT_ON_CLOSE;}
else{return JFrame.DO_NOTHING_ON_CLOSE;}
}
for confirm(YES) it works, but i am not sure if cancel option is solved properly. i just want to cancel JOptionPane and keep frame opened.
You need to do three things:
Set your main application frame to do nothing on close.
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Register a WindowListener that listens to the windowClosing event.
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
maybeExit(); // Will not return if user clicks yes.
super.windowClosing(e);
}
});
Write code to conditionally call System.exit if the user confirms they wish to exit the application.
private void maybeExit() {
int yesNo = JOptionPane.showConfirmDialog(this, "Are you sure you wish to exit?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (yesNo == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
Some suggestions were useful. I solved it in this way:
frame.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
if (HandlingDialogBox.clickOnExit(frame) == 0) {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} else {
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
}
}
});
}
AND
public static int clickOnExit(final JFrame frame) {
return JOptionPane.showConfirmDialog(frame,sharedConstants.exitMessage,"Confirm",
JOptionPane.YES_NO_OPTION);
}
Sorry for bit messy parenthesis use, will clean that up later ...

java reset counter and close program with JOption pane

When my JButton is clicked 5 times I want to show a dialog asking if the user wants more questions
Yes or No
Yes should reset counter to 0 and allow more questions to be asked,
No should close out program when Clicked in the dialog box.
The way I have it now is resetting counter to 0 and I am not sure where to add the
System.exit(0);
here is my code
b1.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e)
{
clicked++;
if (clicked >= 5) {
Object[] options = {
"No, thanks",
"Yes, please"
};
JOptionPane.showOptionDialog(frame,
"Would you like more math questions? ",
"Math Questions",
JOptionPane.YES_NO_CANCEL_OPTION, System.exit(0);
JOptionPane.QUESTION_MESSAGE,
null,
options ,
options[1]);
} else {
clicked = 0;
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e)
{
clicked++;
if (clicked >= 5) {
Object[] options = {
"No, thanks",
"Yes, please"
};
int response = JOptionPane.showOptionDialog(frame,
"Would you like more math questions? ",
"Math Questions",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options ,
options[1]);
if (response == 1)
clicked = 0; //reset
else
System.exit(0);
}
}
});

How to stop closing JFrame form?

I want to Stop accidentally closings in my project. I'm using JFrame Form as Home page. When I click Home window's close button I put Exit cord in Yes Option. I want to stop closing when I click No Option. Is there any way. Here is my cord. I'm using netbeans 7.3
private void formWindowClosing(java.awt.event.WindowEvent evt) {
int i= JOptionPane.showConfirmDialog(null, "Are you sure to exit?");
if (i == JOptionPane.YES_OPTION) {
System.exit(0);
} else{
new Home().setVisible(true);
}
}
How about
class MyGUI extends JFrame {
public MyGUI() {
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// <- don't close window
// when [X] is pressed
final MyGUI gui = this;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(gui,
"Are you sure to exit?", "Closing dialog",
JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
setSize(200, 200);
setVisible(true);
}
//demo
public static void main(String[] args) {
new MyGUI();
}
}
You can simply do that like this
Integer opt = JOptionPane.showConfirmDialog(null, "This application cannot continue without login to the database\n Are you sure you need to exit...?", "Application X", JOptionPane.YES_NO_OPTION);
if(opt== JOptionPane.NO_OPTION){
this.setVisible(true);
}else{
System.exit(0);
}

Ask user if wants to quit via JOptionPane

I'm using this code to confirm the user whether wants to quit or not when he CLICKS THE RED CROSS CLOSE BUTTON OF JFrame (right upper corner)
Object[] options = {"Quit, My Computing Fellow", "No, I want to Work more"};
int Answer = JOptionPane.showOptionDialog(null, "What would you like to do? ","Quit:Continue", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options,options[1]);
if(Answer == JOptionPane.YES_OPTION){
System.exit(0);
}
else if (Answer == JOptionPane.CANCEL_OPTION) {
return;
}
but the problem is if the user clicks CANCEL_OPTION the Frame Closes at all, but i want the user to still open the Frame and not allow the Frame to close. Guide me If I'm doing the blunder or something else?
just do this:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
You can try something like this:
import javax.swing.*;
import java.awt.event.*;
public class MyFrame extends JFrame
{
public MyFrame()
{
setTitle("Close Me");
setSize(200,200);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent evt)
{
Object[] options = {"Quit, My Computing Fellow", "No, I want to Work more"};
int answer = JOptionPane.showOptionDialog(MyFrame.this, "What would you like to do? ","Quit:Continue", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options,options[1]);
if(answer == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
}
public static void main(String st[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
MyFrame mf = new MyFrame();
mf.setVisible(true);
}
});
}
}
As a side note I would suggest you to stick with java naming conventions. For example the variable name should never start with capital letter, class name should always start with capital letter .. And many more. Have a look at here Code Conventions for the Java Programming Language
try something like this:
{
...
yourFrame.setDefaultCloseOperation(close());
...
}
private int close() {
if(yourCondition)
return JFrame.DO_NOTHING_ON_CLOSE;
else
return JFrame.EXIT_ON_CLOSE;
}
I have a Real blunder
Object[] options = {"Quit, My Computing Fellow", "No, I want to Work more"};
int Answer = JOptionPane.showOptionDialog(null, "What would you like to do?","Quit:Continue", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options,options[1]);
if(Answer == JOptionPane.YES_OPTION){
System.exit(0);
}
else if (Answer == JOptionPane.CANCEL_OPTION) {
return;
}
It was clear that I have two Options i.e YES_NO_OPTION and I was calling the CANCEL_OPTION that was a real blunder so, the else-if should be changed to:
else if (Answer == JOptionPane.NO_OPTION) {
this.setDefaultCloseOperation(myclassreference.DO_NOTHING_ON_CLOSE);
}
after this; its bingo !!! I've done!

How to tame the X on the JOptionPane Dialog boxes?

Also, right now whenever I click the 'X" button on top right, the dialog boxes behaves as if I clicked OK (on messages) or YES (on questions). When the user clicks the X, I want DO_Nothing.
In the code below, when i click on the X on the dialog box, it pops out the 'eat!'. Apparently, the X is acting as 'YES' Option, which it should not.
int c =JOptionPane.showConfirmDialog(null, "Are you hungry?", "1", JOptionPane.YES_NO_OPTION);
if(c==JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "eat!", "Order",JOptionPane.PLAIN_MESSAGE);
}
else {JOptionPane.showMessageDialog(null, "ok cool", "Order",JOptionPane.PLAIN_MESSAGE);}
Changed to show how to ignore the cancel button on Dialog box per OP clarification of question:
JOptionPane pane = new JOptionPane("Are you hungry?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
JDialog dialog = pane.createDialog("Title");
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
}
});
dialog.setContentPane(pane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
dialog.setVisible(true);
int c = ((Integer)pane.getValue()).intValue();
if(c == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "eat!", "Order",JOptionPane.PLAIN_MESSAGE);
}
else if (c == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "ok cool", "Order",JOptionPane.PLAIN_MESSAGE);
}
You can't do what you want through the usual JOptionPane.show* methods.
You have to do something like this:
public static int showConfirmDialog(Component parentComponent,
Object message, String title, int optionType)
{
JOptionPane pane = new JOptionPane(message, JOptionPane.QUESTION_MESSAGE,
optionType);
final JDialog dialog = pane.createDialog(parentComponent, title);
dialog.setVisible(false) ;
dialog.setLocationRelativeTo(parentComponent);
dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.setModal(true);
dialog.setVisible(true) ;
dialog.dispose();
Object o = pane.getValue();
if (o instanceof Integer) {
return (Integer)o;
}
return JOptionPane.CLOSED_OPTION;
}
The line that actually disables the close button is:
dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

Categories

Resources