How to make JDialog onTop only for his parent?
setParent in constructor correctly
should use setModalityType fe ModalityType.DOCUMENT_MODAL ModalityType.APPLICATION_MODAL instead of setModal
setModal valid for a container that is intialized / is parent for this JDialog
do not use more than one JFrame , use JDialog instead, reuse this container for another action
eg
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SuperConstructor extends JFrame { private static final long serialVersionUID = 1L; public SuperConstructor() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setPreferredSize(new Dimension(300, 300)); setTitle("Super constructor"); Container cp = getContentPane(); JButton b = new JButton("Show dialog"); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { FirstDialog firstDialog = new FirstDialog(SuperConstructor.this); } }); cp.add(b, BorderLayout.SOUTH); JButton bClose = new JButton("Close"); bClose.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { System.exit(0); } }); add(bClose, BorderLayout.NORTH); pack(); setVisible(true); } public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { @Override public void run() { SuperConstructor superConstructor = new SuperConstructor(); } }); } private class FirstDialog extends JDialog { private static final long serialVersionUID = 1L; FirstDialog(final Frame parent) { super(parent, "FirstDialog"); setPreferredSize(new Dimension(200, 200)); setLocationRelativeTo(parent); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setModalityType(Dialog.ModalityType.APPLICATION_MODAL); JButton bNext = new JButton("Show next dialog"); bNext.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { SecondDialog secondDialog = new SecondDialog(parent, false); } }); add(bNext, BorderLayout.NORTH); JButton bClose = new JButton("Close"); bClose.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { setVisible(false); } }); add(bClose, BorderLayout.SOUTH); pack(); setVisible(true); } } private int i; private class SecondDialog extends JDialog { private static final long serialVersionUID = 1L; SecondDialog(final Frame parent, boolean modal) {
source share