0

I have a problem with my really long and messy code (If there is a better way then please tell me) but the real problem I have is I want my JOptionPane.showConfirmDialog() to cancel the rest of the operation after cancel is clicked, my class here is suppose to set the "newDate" object to null if the cancel option is clicked, so far that is not happening so I think some fresh eyes will help. Here is the code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Object o = jComboBox1.getSelectedItem();
    String st = (String) o;
    Object o2 = jComboBox2.getSelectedItem();
    String st2 = (String) o2;
    newDate = new Dates(st, (int)jSpinner1.getValue(),
            (int)jSpinner2.getValue(), st2);
    switch (newDate.getDayOfWeek()) {
                    //Sundays
                    case "Sun":
                        if (newDate.getHour() == 11) {
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("am")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    //Mondays, Tuesdays, Wednesdays, Thursdays
                    case "Mon":
                    case "Tue":
                    case "Wed":
                    case "Thu":
                        if (newDate.getHour() == 9 || newDate.getHour() == 10 ||
                                newDate.getHour() == 11) {//9, 10, 11 am
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("am")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        } else if (newDate.getHour() == 3 || newDate.getHour() == 4 ||
                                newDate.getHour() == 5|| newDate.getHour() == 6) {//3, 4, 5, 6 pm
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("pm")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    //Fridays
                    case "Fri":
                        if (newDate.getHour() == 9 || newDate.getHour() == 10) {//9, 10
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("am")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        } else if (newDate.getHour() == 5|| newDate.getHour() == 6) {//5, 6 pm
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("pm")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    //Saturdays
                    case "Sat":
                        if (newDate.getHour() == 9 || newDate.getHour() == 10 ||
                                newDate.getHour() == 11) {//9, 10, 11 am
                            if (newDate.getMinute() >= 0) {
                                if (newDate.getDayNight().equals("am")) {
                                    int m = JOptionPane.showConfirmDialog(null, "Warning "
                                            + "Lessons is During Program Time!",
                                            "Warning!", OK_CANCEL_OPTION, WARNING_MESSAGE );
                                    if (m == 1 || m == -1) {
                                        newDate = null;
                                        break;
                                    }
                                }
                            }
                        }   
                        break;
                    default:
                        break;
                }
    dispose();
}     
ItsRainingHP
  • 139
  • 1
  • 4
  • 18

1 Answers1

0

Change the if statement to:

   if (m == JOptionPane.CLOSED_OPTION || m == JOptionPane.CANCEL_OPTION) {
       newDate = null;
       break;
   }

The CANCEL_OPTION has an int value of 2 while the conditional as it stands checks for 1.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189