• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add Checkbox in JOptionPane  RSS feed

 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am developing an application with list of steps below.

1)Click delete button.
2)JOptionpane with yes no option --- code below




3)On clicking yes, further operation is performed.

My requirement is �

With this YES_NO option pane, I need to add one checkbox with message �Do not show this message again�. So that while selecting this checkbox, JOptionPane will not be displayed further.

Also let me know, if this possible then how to get the value of that checkbox.

Thanks in advance
Sharad
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second parameter is an object. While usually you just pass a String, you can pass a lot here:
- String[] for a bunch of labels below each other
- Component (e.g. JPanel) for that component
- Object[] for a combination of elements

In your case, you could pass an Object[] with the String as its first element and a JCheckBox as the second:
 
Sharad Kharya
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.
It works.


Going further in development:-


I need to update entry in xml file.

file location - C:\Program Files\MxAI\Client
file name - Config.xml

xml file entries -



If checkbox is selected i.e boolean dontShow=true,
Then I need to update the entry in xml file as true and save it for further reference.

As i am working on swing application & i didn�t have xml parsers available.

Please let me know how this can be achieved.


Thanks
Sharad
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for JDOM. That should be good enough for what you need.
 
anton hertz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My requirement is �

With this YES_NO option pane, I need to add one checkbox with message �Do not show this message again�. So that while selecting this checkbox, JOptionPane will not be displayed further.

Also let me know, if this possible then how to get the value of that checkbox.

Thanks in advance
Sharad


I ran into the same problem and google took me here. Based on the solutions already here I wrote a class that can be used anywhere, just save it in the same folder or package with your own code. This is the Final and Absolute solution to the lack of a checkbox in JOptionPane :


import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


/*Provides a confirm dialog with a checkbox*/
public class JOptionPaneWithCheckbox extends JOptionPane{


/*In your own code, create a JCheckbox with the required text, eg
JCheckBox dontAskBox = new JCheckBox("Don't ask this again");
Then call this method like the ones in JOptionPane.
Parameter optionType is
for example YES_NO_CANCEL_OPTION as in JOptionPane.
Because the checkbox was created in your own code, you can check anytime
whether it is selected or not:
if (dontAskBox.isSelected()) {xxxxx}
or: boolean dontAsk = dontAskBox.isSelected();
*/

public static int showConfirmDialogWithCheckBox(JCheckBox dontAskBox,
Object message,
String title,
int optionType) {


Object[] params = {message, dontAskBox};


int reply = showConfirmDialog (new JFrame(),
params,
title,
optionType);

return reply;



}

}

 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Don't hijack threads. Create your own thread instead.
2) Don't wake the zombies
3) Use code tags
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!