0

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;
    }

}

}
splungebob
  • 5,357
  • 2
  • 22
  • 45
Trav
  • 21
  • 3
  • A modal dialog will block the progress of whatever code called it. If you see 3 instances, that must mean the loop completed 3 times before any dialog was on-screen. – Andrew Thompson Sep 06 '14 at 04:11
  • @AndrewThompson - The box is not modal. The code creates a modeless box, and the waits(in the while loop) for the user to press one of the buttons. What i meant by 3 instances was that the box appears, user click "Yes", box disappears. New box appears, ... This will only happen 3 times. The 3rd time the user clicks "yes", a new box will not appear. – Trav Sep 06 '14 at 12:15
  • I know it was a long time, but do you remember if you ever resolved this issue? – Chris94 May 25 '20 at 17:03

0 Answers0