java reset counter and close program with JOption pane - java

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);
}
}
});

Related

JCheckBox: showMessageDialog twice in ItemStateChanged event

Why is my program calling showMessageDialog twice in ItemStateChanged event, even I put it in if else?
private void checkBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
if(evt.getStateChange() == ItemEvent.SELECTED){
//System.out.println("Check box 1 selected");
JOptionPane.showMessageDialog(null, "Check box 1 selected");
}
else{
//System.out.println("Check box 1 deselected");
JOptionPane.showMessageDialog(null, "Check box 1 deselected");
}
}
If I only use System.out.println, the program will run correctly.
The state of the checkbox is not being updated before the JOptionPane is displayed. (Notice how the checkmark is not painted when the first option pane is displayed.
So what you need to do is make sure the state is updated and repainted before displaying the JOption pane by using SwingUtilities.invokeLater(...) to display the JOptionPane:
checkBox.addItemListener( new ItemListener()
{
public void itemStateChanged(java.awt.event.ItemEvent evt)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
if(evt.getStateChange() == ItemEvent.SELECTED)
{
JOptionPane.showMessageDialog(null, "Check box 1 selected");
}
else
{
JOptionPane.showMessageDialog(null, "Check box 1 deselected");
}
}
});
}
});

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);

How to exit Application from JMenu item?

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);
}
}

Categories

Resources