How to create a modal JDialog on top of another modal JDialog

32,287

Solution 1

Not sure what exactly you have as a problem, but here is an example on how you can have multiple modal dialogs:

import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestDialog {

    protected static void initUI() {
        JPanel pane = newPane("Label in frame");
        JFrame frame = new JFrame("Title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);

    }

    public static JPanel newPane(String labelText) {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(newLabel(labelText));
        pane.add(newButton("Open dialog"), BorderLayout.SOUTH);
        return pane;
    }

    private static JButton newButton(String label) {
        final JButton button = new JButton(label);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Window parentWindow = SwingUtilities.windowForComponent(button);
                JDialog dialog = new JDialog(parentWindow);
                dialog.setLocationRelativeTo(button);
                dialog.setModal(true);
                dialog.add(newPane("Label in dialog"));
                dialog.pack();
                dialog.setVisible(true);
            }
        });
        return button;
    }

    private static JLabel newLabel(String label) {
        JLabel l = new JLabel(label);
        l.setFont(l.getFont().deriveFont(24.0f));
        return l;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                initUI();
            }
        });
    }
}

Solution 2

1.please read The New Modality API in Java SE 6

2.Is there a way to get the (J)Frame of a JDialog?

Window ancestor = SwingUtilities.getWindowAncestor(this);

or

Window ancestor = (Window) this.getTopLevelAncestor();

Solution 3

It is now 2021 and so about 8.5 years after the question was posted, but back in 2012 and still in these days by far and away the easiest solution is to simply create the JDialog with a Dialog as owner. Java has 5 constructors with a Dialog owner to offer, next to 5 similar ones with a Frame owner and 5 with a Window owner.

The issue that the OP was probably faced with was the default template for a JDialog in Netbeans or some other IDE. Netbeans (12.2) offers this as a template for a new JDialog Form...:

public class NewJDialog extends javax.swing.JDialog {

    /**
     * Creates new form NewJDialog
     */
    public NewJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

You can simply change the signature of the constructor to use a Dialog as owner instead of the default option of a Frame:

public class NewJDialog extends javax.swing.JDialog {

    /**
     * Creates new form NewJDialog
     */
    public NewJDialog(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

And away you go.

Share:
32,287
Erik Stens
Author by

Erik Stens

Updated on January 18, 2021

Comments

  • Erik Stens
    Erik Stens over 3 years

    I have a modal settings dialog which is a JDialog. In this settings window I placed some components including a button to yet another modal settings dialog which is also a JDialog. I made them JDialogs because that is the only way I know of to make a modal dialog.

    The problem is this: when I create the main settings dialog I have to construct the JDialog either without a parent Frame or with a parent Frame. Since my main window is a JFrame, I can just pass that to the main settings dialog constructor. But when I want to create the second modal settings dialog which should have the main settings dialog as a parent, I can't find a way to get the (J)Frame of the JDialog. I do want to pass that main settings dialog as a parent so that the second settings dialog centers on it when it is shown. Let's assume the second settings dialog has no constructor for passing a location, just the constructors of the JDialog.

    Is there a way to get the (J)Frame of a JDialog? Is there a design flaw in my setup and should I have used something else for these settings dialogs? (And if so, how should I make these alternative settings dialogs modal?)

    Thank you for your help, Erik

    UPDATE: Thank you all for your answers. They led me to understand that apparently it's not absolutely necessary to have an owner for a JDialog. I thought this was needed for the dialog to be able to disable the owner until the dialog is closed, but apparently the modality is independent of the owner. I also noticed that even with an owner the dialog still doesn't center on the owner, so now my code is like:

    public class CustomDialog extends JDialog {
        public CustomDialog(String title) {
            setModal(true);
            setResizable(false);
            setTitle(title);
    
            buildGUI();
        }
    
        public Result showDialog(Window parent) {
            setLocationRelativeTo(parent);
            setVisible(true);
            return getResult();
        }
    }
    

    This also allows for modal dialogs in modal dialogs.

    Thanks for all your help!

  • zygimantus
    zygimantus over 8 years
    Nice, but why new JDialog's are opened in staircase manner?
  • Guillaume Polet
    Guillaume Polet over 8 years
    @zygimantus That is directly caused by the call to dialog.setLocationRelativeTo(button)