Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:15:49+00:00 2026-05-24T05:15:49+00:00

In a Swing (J)Dialog, setModal sets the modality – that is, whether the dialog

  • 0

In a Swing (J)Dialog, setModal sets the modality – that is, whether the dialog should block input to other windows or not. Then, setVisible docs say for modal dialogs:

If the dialog is not already visible, this call will not return until the dialog is hidden by calling setVisible(false) or dispose.

Indeed, setVisible does return right away if the dialog is not modal. Sample code:

JDialog jd = new JDialog();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

/**
 * If set to false, setVisible returns right away.
 * If set to true, setVisible blocks until dialog is disposed.
 */
jd.setModal(false);

System.out.println("setting visible");
jd.setVisible(true);
System.out.println("set visible returned");

I want to make a dialog that doesn’t block input to other windows, but still does block the caller. What is a good way to do this, now that setVisible doesn’t block when the dialog is not modal?

Is there some rationale why setVisible‘s behavior depends on the modality?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:15:51+00:00Added an answer on May 24, 2026 at 5:15 am

    I need to make a dialog that doesn’t block input to other windows, but does block the caller so that I know when the dialog has been closed.

    I usually solve this not by blocking the caller, but by using a callback of some sort – a simple interface that the dialog invokes when it’s done. Let’s say your dialog has an “OK” and a “Cancel” button and you need to distinguish which one is pressed. Then you could do something like this:

    public interface DialogCallback {
        void ok();
        void cancel();
    }
    
    public class MyModelessDialog extends JDialog {
        private final DialogCallback cbk;
        private JButton okButton, cancelButton;        
    
        public MyModelessDialog(DialogCallback callback) {
            cbk = callback;
            setModalityType(ModalityType.MODELESS);
    
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onOK();
                }
            };
    
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onCancel();
                }
            };
    
            // Treat closing the dialog the same as pressing "Cancel":
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e)  {
                    onCancel();
                }
            };
        }
    
        private void onOK() {
            cbk.ok();
        }
    
        private void onCancel() {
            cbk.cancel();
        }
    }
    

    Then you just pass in an instance of DialogCallback to the constructor:

    MyModelessDialog dlg = new MyModelessDialog(new DialogCallback() {
        public void onOK() { 
            // react to OK
        }
        public void onCancel() { 
            // react to Cancel
        }
     });
    

    EDIT

    Is there some rationale why setVisible’s behavior depends on the modality?

    Well, that’s just how how modal windows are supposed to work, no? A modal window should block the current workflow when displayed, and a non-modal/modeless should not. See e.g. the Wikipedia pages on modal windows or dialog boxes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone show simple Java Swing code/web resource that will position the popup dialog
Simple question: Can a swing frame be completely modal ( block all others windows
I'm trying to use a Swing Dialog so that the user can choose an
I have a gateway application that comes up with a login dialog and then
In my Swing app, users can click a button to open a dialog panel
I have a Java swing application with a panel that contains three JComboBoxe s
I have a swing application that includes radio buttons on a form. I have
If you open a dialog in Swing, for example a JFileChooser, it goes somewhat
I'm maintaining this Swing app that has a print option. Users need to be
I'm working on swing application that relies on an embedded H2 database. Because I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.