How to make JDialog onTop only for your parent?

Suppose we have several JFrame windows visible at the same time, and for each JDialog window. When our windows are in cascade mode and for the dialogs setAlwaysOnTop is true , then all dialogs will be visible in the last window.

I just want to associate the Dialog component with my owner, so when you switch between frames, you will get only one dialog at the top and you will not lose this dialog when you click on the frame.

Dialogs have a constructor similar to this:

 setAlwaysOnTop(true); setModal(false); 

Thanks in advance!

+4
source share
4 answers
 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) { //super(parent); // Makes this dialog unfocusable as long as FirstDialog is visible setPreferredSize(new Dimension(200, 200)); setLocation(300, 50); setModal(modal); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setTitle("SecondDialog " + (i++)); setModalityType(Dialog.ModalityType.APPLICATION_MODAL); JButton bClose = new JButton("Close"); bClose.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { setVisible(false); } }); add(bClose, BorderLayout.SOUTH); pack(); setVisible(true); } } } 
+5
source

In the API, one of the JDialog constructors is JDialog(Dialog owner, boolean modal) , which means that you can create a dialog and specify the parent container as well as the modality. In the modal section, setting it to true means that this dialog will be modal, and you will not be able to access the parent window while the dialog is displayed.

But again, you can use the setModal() method to accomplish the same thing.

+1
source

just set Model true and just set Relativelocation(parent); and do not use setontop(true) for JDialog.

and then if u is back open, time u will get the ontop dialog every time. but it will be different if you drag the parent frame.

0
source

I managed to solve this problem by creating a focus listener that does this work. Then you can install this listener in a window in which you want the dialog to always be displayed before closing. Here is what worked for me:

 public class WindowFocusListenerDialogFocus implements WindowFocusListener { private JFrame _dialogFrame; public WindowFocusListenerDialogFocus(JFrame dialogFrame) { _dialogFrame = dialogFrame; } @Override public void windowLostFocus(WindowEvent e) { System.out.println("Focus lost!"); } @Override public void windowGainedFocus(WindowEvent e) { System.out.println("Focus gained!"); _dialogFrame.toFront(); } } 
0
source

All Articles