Question

I'm showing up a Dialog from a JFrame, but when I click outside the dialog, the dialog gets hidden. It's supposed to the dialog won't let you do nothing unless you close it right?

This is my code:

The Dialog calling from First Dialog:

JProductStocking jps = JProductStocking.getProductStoking(JPanelTicket.this, oApp);
jps.setVisible(true);

And this is the JDIalog called:

public class JProductStocking extends javax.swing.JDialog implements BeanFactoryApp{
public JProductStocking(Component parent, boolean modal) {
        //super(parent, modal);
        initComponents();

    }

public static JProductStocking getProductStoking(Component parent, AppView app) {
        Window window = getWindow(parent);

        JProductStocking myMsg;
        if (window instanceof JFrame) { 
            myMsg = new JProductStocking((Frame) window, true);
        } else {
            myMsg = new JProductStocking((Dialog) window, true);
        }
        myMsg.init(app, parent);
        myMsg.applyComponentOrientation(parent.getComponentOrientation());
        return myMsg;
    }

     private static Window getWindow(Component parent) {
        if (parent == null) {
            return new JFrame();
        } else if (parent instanceof JFrame || parent instanceof Dialog) {
            return (Window) parent;
        } else {
            return getWindow(parent.getParent());
        }
    }

    public void init(AppView app, Component parent) {
        oApp = app;
       // m_dlSales = (DataLogicSales) app.getBean("com.openbravo.pos.forms.DataLogicSales");
        initComponents();
        ProductList = new ArrayList();
        this.setResizable(false);
        setLocationRelativeTo(parent);

    }
}

Im not calling the jDialog well? or what im doing wrong?

Was it helpful?

Solution 2

You did not pass the parent Window and no modal flag to the JDialog’s constructor so the default behavior of modeless was used. Note that besides that your code is unnecessary complicated.

Since Java 6 you can pass a Window to a Dialog’s constructor and it is allowed to be null so it is fail-safe. Combined with the existing method SwingUtilities.windowForComponent the entire code may look like:

public class JProductStocking extends javax.swing.JDialog
  implements BeanFactoryApp {

  public JProductStocking(Component parent, Dialog.ModalityType modality) {
    super(SwingUtilities.windowForComponent(parent), modality);
    initComponents();
  }
// …

Note that with the Java 6 Dialog.ModalityType you can configure a Dialog to block all other application’s windows besides the children of this dialog (APPLICATION_MODAL) or to block the dialog’s parents and their children only (DOCUMENT_MODAL). This provides much more control than the simple modal flag.

OTHER TIPS

The behavior you are looking for is called a "modal" dialog. You must pass 'true' to the dialog constructor:

public JProductStocking() {
        super((Frame)null, true); //better to pass an actual Frame, Window or Dialog object as a parent
        initComponents();

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top