How to open the “don't ask me again” dialog using java?

In one part of my program, I use JOptionPane to ask the user if they are sure what they will do. But I don’t want the user to ask that every time you try, I like to use the function of some of the dialog boxes in Android that come with “Don't ask again”, but don’t know how to implement this in my program, someone here, could you help me? (Must have 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 } 
+5
source share
3 answers

The basic idea is to take advantage of the fact that the message parameter can actually be Component . Then the problem boils down to checking whether the user has selected the “Don't ask me again” option, saving and reusing it

Sort of...

 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 base starting point. I used Properties as a repository for simplification, you can use a database or other persistent method ( Preferences , XML , etc.)

Then you could just use it somehow 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 "Do not ask me again" in the first prompt, the second call will return the previously selected value

Now, somewhere, you probably want to be able to reverse these decisions;)

+8
source

JOptionPane also allows you to display Component as a message. Therefore, this problem can be solved as follows:

 JPanel msgPanel = new JPanel(); JLabel msg = new JLabel("some message"); msgPanel.add(msg); JCheckBox jcb = new JCheckBox("do not ask me again"); msgPanel.add(jcb); JOptionPane.showConfirmDialog(null , msgPanel , "some title" , JOptionPane.OK_CANCEL_OPTION); 
+5
source

Using some of the answers here, I managed to solve my problem, I will talk about it here if someone might be interested:

I have a flag that is valid only in the current session until the chat window is reset, the flag is available in the "parameters" section of my application, and another appears when the reset button is pressed.

The ActionListener button code is below, with the necessary comments:

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; jButtonReset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //this checkbox is the one available in app settings if (!surpassResetConfirmCheckBox.isSelected()) { JPanel msgPanel = new JPanel(); // message inside confirmation dialog JLabel msg = new JLabel("Are you sure you wish to clear current conversation?"); msgPanel.add(msg); JCheckBox jcb = new JCheckBox("do not ask me again."); jcb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // if-else statement below is used to update checkbox in settings whenever this one is changed. if (jcb.isSelected()) { surpassResetConfirmCheckBox.setSelected(true); } else { surpassResetConfirmCheckBox.setSelected(false); } } }); msgPanel.add(jcb); // JOptionPane returns an integer value ,we'll use it later int result = JOptionPane.showConfirmDialog(null, msgPanel, "Reset Conversation", JOptionPane.OK_CANCEL_OPTION); // check user response.. if (result == JOptionPane.OK_OPTION) { // if user clicked Ok , call your method here // reset(); } else if (result == JOptionPane.CANCEL_OPTION || result == JOptionPane.CLOSED_OPTION) { // if user closed the dialog or clicked cancel, uncheck the checkbox in settings surpassResetConfirmCheckBox.setSelected(false); } } else { // if checkbox in settings is checked, call your method immediately // reset(); } } }); } 
+2
source

All Articles