I've created a class to display a modeless dialog box. I want to warn the user of something but I can not prevent them from accessing the application.
In the Test program below, the Dialog will only be created 3 times(same issue i see in my app), then it stops. Have no idea why. if i remove the line dialog.setModalityType(Dialog.ModalityType.MODELESS); ,everything works fine. Another thing is that if I set a breakpoint in the show() method it will hit the breakpoint, if I continue it will show the box 3 more times and stop. Is there something wrong with this code? Don't have much experience with this stuff, but i'm thinking that the while() loop is causing an issue, but I can figure why?
public class JavaApplication6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
while (true) {
showAlertBox();
}
}
public static void showAlertBox() {
AlertBox box = new AlertBox("Test" , "HI THERE");
int result = box.show();
if (result == JOptionPane.YES_OPTION) {
System.out.println("YES SELECTED");
}
}
}
public class AlertBox {
JOptionPane pane;
JDialog dialog;
Object selectedValue;
String mTitle;
public AlertBox(String title, String alertText) {
pane = new JOptionPane(alertText, JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION, null, null, null);
mTitle=title;
}
public int show() {
dialog = pane.createDialog(null, mTitle );
dialog.setModalityType(Dialog.ModalityType.MODELESS);
dialog.setVisible(true);
selectedValue = pane.getValue();
while (selectedValue == JOptionPane.UNINITIALIZED_VALUE) {
//wait
selectedValue = pane.getValue();
}
if(selectedValue instanceof Integer) {
return ((Integer)selectedValue).intValue();
} else {
return JOptionPane.CLOSED_OPTION;
}
}
}