I have an application that I want to create my own dialog screens. For example, the user cliks on one frame, and I want to open a window for the user to enter data, after the user finishes, he presses OK and then the main app continues.

On the class that I open the dialog, I open it with:

NewOkCancelDialog.abrePopup();
     System.out.println("Program should wait for the dialog to close");

And the 'NewOkCancelDialog' class is defined like:

public class NewOkCancelDialog extends javax.swing.JDialog {

The constructor is like:

public NewOkCancelDialog(java.awt.Frame parent, boolean modal) {
 super(parent, true);
 initComponents();
 this.setLocation(400,400);
    }

This methods are when the user activates the button and the popup closes..

private void cierraPopupOk(){
 resultados.setciudad1(this.fld_ciudad1.getText());
 if (resultados.getciudad1length() < 3) {
     System.out.println("Error en la entrada!");
 } else {
     doClose(RET_OK);
 }
    }
   
    
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
 cierraPopupOk();
    }                                        

 
  private void doClose(int retStatus) {
 returnStatus = retStatus;
 this.estaAbierto = false;
 setVisible(false);
 dispose();
    }

I would like to have the application in the first lines, after calling

NewOkCancelDialog.abrePopup();

to wait until the method doClose() in NewOkCancelDialog class is finnished. But right now, after the ...abrePopup() is called, it continues.

What am I doing wrong? or what am I not doing?

Dani AI

Generated

A modal JDialog will block the caller only if you actually show it synchronously. In many cases the dialog "shows" but the calling method continues because the dialog is being shown asynchronously (for example via EventQueue.invokeLater / SwingUtilities.invokeLater) or because the dialog was constructed/shown non‑modal. If you want the code after the call to wait, create a modal dialog and call setVisible(true) directly so the call does not return until the dialog is hidden/disposed.

Example pattern (caller side):

NewOkCancelDialog dlg = new NewOkCancelDialog(parentFrame, true);
dlg.pack();
dlg.setLocationRelativeTo(parentFrame);
dlg.setVisible(true);   // blocks until dialog is closed
if (dlg.getReturnStatus() == NewOkCancelDialog.RET_OK) {
    // read fields from dlg and continue processing
}

Troubleshooting checklist:

  • Inspect abrePopup() — if it uses invokeLater it will return immediately. Change it to construct the dialog and call setVisible(true) synchronously, or have abrePopup() itself call setVisible(true) and return the dialog/result.
  • If the caller is not on the EDT and you need to show the modal dialog and block the caller, use SwingUtilities.invokeAndWait(...) to show it (but never call invokeAndWait from the EDT; that deadlocks).
  • Make sure the dialog is actually modal (construct it with the modal flag or appropriate ModalityType).
  • Avoid doing long-running work on the EDT after the dialog closes; if post-processing is heavy, offload it to a SwingWorker.

Tie-back to the thread: 's suggestion (move follow-up processing into a method called after OK) is perfectly valid and often simple. The alternative above preserves a synchronous flow so your line after the popup call truly waits and then inspects the dialog's return status and data.

To follow up all the left processing after the ok button is clicked you can do like this

1) Define a new function in which you put all the code you want to process after the dialog box is clicked ok.

2) Now in your cierraPopupOk() call the function after your doClose(RET_OK) processing is finished.
Ex

.
.
.
.
.
private void cierraPopupOk(){
 resultados.setciudad1(this.fld_ciudad1.getText());
 if (resultados.getciudad1length() < 3) {
     System.out.println("Error en la entrada!");
 } else {
     doClose(RET_OK);
     newFunction();
 }
    }
..
.
.
public void newFunction()
{
// Here you put the code  whatever  you want to do after the ok button is clicked
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.