0

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3480792
  • 94
  • 1
  • 7
  • 4
    You are not passing any parent `Window` to `JDialog’s` constructor so it can’t know which parent to block. It should hit your eye as you have made the `//super(parent, modal);` comment in the constructor… – Holger May 15 '14 at 09:42
  • @Holger I dont know how i can forget that, THANKS! Post it like an answer and i will accept it. – user3480792 May 15 '14 at 09:46
  • @Holger Providing a parent will only tell the dialog to remain on top of it if I am not mistaken. It won't prevent the user from interacting with any visible portion of the parent. For that, the dialog must be modal. – schmop May 15 '14 at 09:49
  • @schmop: yes, but it’s still the same problem: the desired constructor was not called. Whether the parent is relevant to blocking depends on the modality type. – Holger May 15 '14 at 11:02
  • @user3480792 use local variable for JFrame, create only one JDialog, reuse this Object for another action, – mKorbel May 15 '14 at 11:50

2 Answers2

3

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();

    }
schmop
  • 1,440
  • 1
  • 12
  • 21
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.

Holger
  • 285,553
  • 42
  • 434
  • 765