How to make a “do not ask me again” dialog pop-up box with java?

Kevin Alemán

In one part of my program I use a JOptionPane to ask the user if they are sure of what he will do. But I do not want to bore the user asking that every time you try so I like to use the function of some dialog boxes in android that come with the "Do not ask again", but do not know how to implement that in my program, someone here you could help me? (Should have a Spanish StackOverflow) This is my code

if (jCheckBox2.isSelected() && jCheckBox1.isSelected()){
        JOptionPane.showConfirmDialog(null, "This action can cause problems, want to do it?");
        //here must be something to never ask again this 

    }
MadProgrammer

The basic idea is to take advantage of the fact the message parameter can actually be a Component. The problem then comes down to checking to see if the user selected the "Don't ask me again" option, storing and re-using it

Something like...

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DontAskMeAgainPanel extends JPanel {

    private JCheckBox dontAskMeAgain;

    public DontAskMeAgainPanel(Object message) {
        setLayout(new BorderLayout());
        if (message instanceof Component) {
            add((Component) message);
        } else if (message != null) {
            add(new JLabel(message.toString()));
        }
        dontAskMeAgain = new JCheckBox("Don't ask me again");
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.add(dontAskMeAgain);
        add(panel, BorderLayout.SOUTH);
    }

    public boolean dontAskMeAgain() {
        return dontAskMeAgain.isSelected();
    }

    private static Properties settings;

    protected static void loadProperties() {
        if (settings != null) {
            settings = new Properties();
            File source = new File("...");
            if (source.exists()) {
                try (Reader r = new FileReader(source)) {
                    settings.load(r);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        }
    }

    protected static void saveProperties() {
        if (settings != null) {
            settings = new Properties();
            File source = new File("...");
            try (Writer w = new FileWriter(source)) {
                settings.store(w, "Don't prompt for settings");
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }
    }

    public static int showConfirmDialog(Component parent, Object message, String key) {

        loadProperties();

        int result = JOptionPane.NO_OPTION;

        if (settings.containsKey(key + ".prompt") && !Boolean.parseBoolean(settings.getProperty(key + ".value"))) {
            result = Integer.parseInt(settings.getProperty(key + ".value"));
        } else {
            DontAskMeAgainPanel panel = new DontAskMeAgainPanel(message);
            result = JOptionPane.showConfirmDialog(parent, panel);
            if (panel.dontAskMeAgain()) {
                settings.put(key + ".prompt", "false");
                settings.put(key + ".value", Integer.toString(result));

                saveProperties();
            }
        }
        return result;
    }

}

As a basic starting point. I've used Properties as the backing store for simplicity, you could use a database or other persistent method (Preferences, XML, etc)

Then you could just use it something like...

int result = DontAskMeAgainPanel.showConfirmDialog(null, "This is annoying", "Annoying");
System.out.println("You selected " + result);
result = DontAskMeAgainPanel.showConfirmDialog(null, "This is annoying", "Annoying");
System.out.println("Then you selected " + result);

If you select "Don't ask me again" at the first prompt, then the second call will return the previously selected value

Now, somewhere, you're probably going to want to have the ability to reverse these decisions ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I configure ssh to ask me for my password in the terminal instead of in a dialog box?

From Dev

How to pop up a dialog box at 8pm (20:00 hours) every day using java?

From Dev

How to include a pop-up dialog box in subflow

From Dev

How to include a pop-up dialog box in subflow

From Dev

How to block the htaccess login dialog box in pop-up ads?

From Dev

How to make a pop-up dialog that does not stop the program execution?

From Dev

how to show variable's value in pop up dialog with java swing

From Dev

How do I create a JavaFX Alert with a check box for "Do not ask again"?

From Dev

How to do styling on buttons on a message box pop-up

From Dev

How to pop up a text box(or tooltip) via script in Java

From Dev

How to prevent a pop-up dialog-box from changing its position?

From Dev

How to make marquee text pop up again immediately after it leaves the screen view in Android

From Dev

How do is use the SetInterval function and the 'if' to make div's pop up?

From Dev

How do I make a "div" element pop up and darken the screen?

From Dev

Display facesontext message in pop up for validating input in a dialog box

From Dev

Xpages can't get dojo tooltip dialog box to pop up

From Dev

pop up alert dialog box from service in android

From Dev

Display facesontext message in pop up for validating input in a dialog box

From Java

How to make a edittext box in a dialog

From Dev

How do I make dialog windows in java?

From Dev

how to make a pop-up window using java

From Dev

Why does Java ask me to hit Enter again?

From Dev

How to create pop-up login box?

From Dev

How to make gdb not ask me "y or n"?

From Dev

GCM How do I detect if app is open and if so pop up an alert box, instead of normal notification flow?

From Dev

Pop up text box

From Dev

Box pop up on scroll

From Dev

TextArea on pop up box

From Dev

Show a dialog box next to a textarea without pop-up or alert box

Related Related

  1. 1

    How do I configure ssh to ask me for my password in the terminal instead of in a dialog box?

  2. 2

    How to pop up a dialog box at 8pm (20:00 hours) every day using java?

  3. 3

    How to include a pop-up dialog box in subflow

  4. 4

    How to include a pop-up dialog box in subflow

  5. 5

    How to block the htaccess login dialog box in pop-up ads?

  6. 6

    How to make a pop-up dialog that does not stop the program execution?

  7. 7

    how to show variable's value in pop up dialog with java swing

  8. 8

    How do I create a JavaFX Alert with a check box for "Do not ask again"?

  9. 9

    How to do styling on buttons on a message box pop-up

  10. 10

    How to pop up a text box(or tooltip) via script in Java

  11. 11

    How to prevent a pop-up dialog-box from changing its position?

  12. 12

    How to make marquee text pop up again immediately after it leaves the screen view in Android

  13. 13

    How do is use the SetInterval function and the 'if' to make div's pop up?

  14. 14

    How do I make a "div" element pop up and darken the screen?

  15. 15

    Display facesontext message in pop up for validating input in a dialog box

  16. 16

    Xpages can't get dojo tooltip dialog box to pop up

  17. 17

    pop up alert dialog box from service in android

  18. 18

    Display facesontext message in pop up for validating input in a dialog box

  19. 19

    How to make a edittext box in a dialog

  20. 20

    How do I make dialog windows in java?

  21. 21

    how to make a pop-up window using java

  22. 22

    Why does Java ask me to hit Enter again?

  23. 23

    How to create pop-up login box?

  24. 24

    How to make gdb not ask me "y or n"?

  25. 25

    GCM How do I detect if app is open and if so pop up an alert box, instead of normal notification flow?

  26. 26

    Pop up text box

  27. 27

    Box pop up on scroll

  28. 28

    TextArea on pop up box

  29. 29

    Show a dialog box next to a textarea without pop-up or alert box

HotTag

Archive