title: Disposing a Modal dialog using a condition
body: <p>I have a modal jDialog that i wish to dispose when a particular condition is met after showing the error message in another Dialog using JOptionPane. I have tried to use the dispose() method after the JOptionPane dialog but my modal dialog still opens up.
  The relevant part of my code is below:</p>
  
  <pre><code>    import java.awt.Component;
      import java.sql.*;
      import java.text.SimpleDateFormat;
      import javax.swing.JOptionPane;
      import javax.swing.table.DefaultTableModel;
  
      public class ReviewPatients extends javax.swing.JDialog {
  
      public ReviewPatients(java.awt.Frame parent, boolean modal) {
      super(parent, modal);
  
      jScrollPane1 = new javax.swing.JScrollPane();
      jTable1 = new javax.swing.JTable();
  
      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  
      jTable1.setModel(new javax.swing.table.DefaultTableModel(
          new Object [][] {
  
          },
          new String [] {
              "Name", "Address", "Number"
          }
      ) {
          Class[] types = new Class [] {
  java.lang.String.class, java.lang.String.class, java.lang.Integer.class
          };
          boolean[] canEdit = new boolean [] {
              false, false, false
          };
  
          public Class getColumnClass(int columnIndex) {
              return types [columnIndex];
          }
  
          public boolean isCellEditable(int rowIndex, int columnIndex) {
              return canEdit [columnIndex];
          }
      });
      jScrollPane1.setViewportView(jTable1);
  
      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
              .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1012, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addGap(0, 0, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
              .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 526, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addGap(0, 0, Short.MAX_VALUE))
      );
  
      pack();
  
  
      jScrollPane1.setVisible(false);
  
  e: 
  {    
             try
              {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/doctor","root","Pranav123");
              String query="SELECT * FROM records_table WHERE Name LIKE 'some_name';";
              Statement st = con.createStatement();
              ResultSet rs= st.executeQuery(query);
  
  
  
              DefaultTableModel tmodel = (DefaultTableModel) jTable1.getModel();
  
                  //Clearing the table
                  int rows=tmodel.getRowCount();
                  while(rows&gt;0)
                  {
                      tmodel.removeRow(0);
                      rows--;
                  }
                  jTable1.setModel(tmodel);
  
              while(rs.next())
              {
  
                  //Putting data into table
                  tmodel.addRow(new Object[] {rs.getString(1),rs.getString(2),rs.getInt(4)});
                  jTable1.setModel(tmodel);
              }
  
              }
              catch(Exception e)
              {
                  System.out.println("Error: "+e);
              }
  
          if(jTable1.getRowCount()==0)
              {
                  JOptionPane.showMessageDialog(this, "No records exist!");
                  dispose();
                  break e;
              }
  
  
  
           //Showing data   
          jScrollPane1.setVisible(true);
  
  
  }    
  
  public static void main(String args[]) {
  java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
              ReviewPatients dialog = new ReviewPatients(new javax.swing.JFrame(), true);
              dialog.setVisible(true);
          }
      });
  }
  </code></pre>
  
  <p>e is the label that I have used to exit the block whenever an error occurs.
  Any help would be greatly appreciated.</p>