简体   繁体   中英

Make a JDialog always on top of parent (JFrame) but user still can interact with parent

I'm working on building a text editor program like NotePad. I want to make FindDialog always on top of MainFrame but user still can edit the text at JTextArea in MainFrame as NotePad.

Please help me!!!

I have used method jdialog.setModal(true). It make dialog always on top of parent but user can't edit the text at parent.

Edit: method setAlwaysOnTop() make dialog on top of all windows (include browsers,other programs..) so i can't use it

I have detected that we can use super(parent) to achieve this.

class MyDialog extends JDialog {

    public MyDialog(JFrame parent) {
        super(parent);
    }

    /* Other codes */

}

There are various modality types (can be) supported. Use JDialog.setModalityType method and choose the relevant modality type. For more information check out the javadoc here: https://docs.oracle.com/javase/7/docs/api/java/awt/Dialog.html#setModalityType(java.awt.Dialog.ModalityType)

BTW, calling setModal(true) is equivalent to setModalityType(Dialog.ModalityType.MODELESS) . In this case user can edit the parent.

So you can try either:

setModalityType(Dialog.ModalityType.DOCUMENT_MODAL)

or as you creating your JDialog pass the modality

new JDialog(parent, "Title", Dialog.ModalityType.DOCUMENT_MODAL);

There is also this useful tutorial about the modality from Oracle: https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html

Notice that there is a slight difference between Document and Application modal. Choose the appropriate one for your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM