Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!

anton hertz

Greenhorn
+ Follow
since Oct 12, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by anton hertz



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;



}

}

12 years ago