简体   繁体   中英

Modal Window on top of Modal Window in java swing?

I have the following problem:

I have a main application window (JFrame) that fills the whole screen.
When a button is clicked, a smaller window appears, to let the user input some Data. While the User does this, the main window should neither jump in front of it, nor allow interaction.
Solution to that: open a modal JDialog. Lets call that Dialog 1.
But when a button within this Dialog is clicked, a NEW window (yes/no-dialog) is supposed to pop up.. and, again, needs to act modal on the already modal JDialog Dialog 1. Trying to do that, the second Dialog keeps disappearing behind the first one.
I tried making Dialog 1 a JFrame but then, of course, I loose the "modal" bit. Forcing Dialog 1 to stay in from still keeps the main window's Button clickable.
What am I missing? How can I put a modal swing window OVER another modal swing window?

Edit: minimal example of one not-really working version:

Main class for opening:

public class MainWin extends JFrame {

public MainWin(){
    this.setSize(800,800);
    JButton b = new JButton("click hehe");
    this.getContentPane().add(b);
    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dia1(MainWin.this);
        }
    });
    this.setVisible(true);
}

}

Main window:

 public class MainWin extends JFrame {

public MainWin(){
    this.setSize(800,800);
    JButton b = new JButton("click hehe");
    this.getContentPane().add(b);
    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dia1(MainWin.this);
        }
    });
    this.setVisible(true);
}
}

First Dialog:

public class Dia1 extends JDialog {


public Dia1(final JFrame parent){
    super(parent, true);
    this.setSize(400, 400);
    JButton b = new JButton("click hehe");
    this.getContentPane().add(b);
    this.setVisible(true);
    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            new Dia2(parent);
        }
    });
}
}

Second Dialog:

public class Dia2 extends JDialog {


public Dia2(JFrame parent){
    super(parent, true);
    this.setSize(200, 200);
    JButton b = new JButton("click hehe");
    this.getContentPane().add(b);
    this.setVisible(true);
}

}

PS: I just realised: Dialog 2 is not hidden, as I suspected.. it is simply not there. Most likely because the parent window is blocked from the Modal Dialog?

Here is an MCVE of modality working as advertised.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ModalOverModal {

    private JComponent ui = null;

    ModalOverModal() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridLayout());
        ui.setBorder(new EmptyBorder(40,40,40,40));

        final JButton b1 = new JButton("Open Modal Dialog");
        b1.setMargin(new Insets(40, 200, 40, 200));
        ui.add(b1);

        final JButton b2 = new JButton("Open 2nd Modal Dialog");

        ActionListener al1 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(b1, b2);
            }
        };
        b1.addActionListener(al1);

        ActionListener al2 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(b2, "Close Me!");
            }
        };
        b2.addActionListener(al2);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                ModalOverModal o = new ModalOverModal();

                JFrame f = new JFrame("Modal over Modal");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

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