How to make JFrame Modal in Swing java

I created one GUI in which I used JFrame. How do I make it modal?

+60
java swing modal-dialog jframe
Sep 26 '09 at 15:07
source share
12 answers

It is best to use JDialog instead of JFrame if you want to make the mod modal. Check out the introduction of modality APIs in Java 6 for more information. There is also a tutorial .

Here is an example of code that will display the JPanel panel in a JDialog , which is modal to the Frame parentFrame . With the exception of the constructor, this follows the same pattern as opening the JFrame .

 final JDialog frame = new JDialog(parentFrame, frameTitle, true); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); 

Edit: the modality API link has been updated and a link to the tutorial has been added (a nod in @spork for a shock wave).

+65
Sep 26 '09 at 15:13
source share

You can create a class to which the reference to the parent JFrame , and holds it in the JFrame variable. You can then lock the frame that created your new frame.

 parentFrame.disable(); //Some actions parentFrame.enable(); 
+15
Feb 05 2018-12-12T00:
source share

just replace JFrame with JDialog in class

 public class MyDialog extends JFrame // delete JFrame and write JDialog 

and then write setModal(true); in the constructor

After that you can create your form in netbeans and the form becomes modal

+8
11 Feb '15 at 18:37
source share
  • Create a new JPanel form
  • Add the necessary components and code to it.



 YourJPanelForm stuff = new YourJPanelForm(); JOptionPane.showMessageDialog(null,stuff,"Your title here bro",JOptionPane.PLAIN_MESSAGE); 


Your modal dialogue is waiting ...

+6
May 22 '13 at 8:15
source share

As far as I know, JFrame cannot execute modal mode. Instead, use JDialog and call setModalityType(Dialog.ModalityType type) to set it modal (or non-modal).

+4
Sep 26 '09 at 15:14
source share

If you are ready to use JDialog instead of JFrame, you can set ModalityType to APPLICATION_MODAL .

This provides the identical behavior of your typical JOptionPane:

 import java.awt.event.ActionEvent; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; public class MyDialog extends JFrame { public MyDialog() { setBounds(300, 300, 300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setLayout(new FlowLayout()); JButton btn = new JButton("TEST"); add(btn); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showDialog(); } }); } private void showDialog() { JDialog dialog = new JDialog(this, Dialog.ModalityType.APPLICATION_MODAL); //OR, you can do the following... //JDialog dialog = new JDialog(); //dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); dialog.setBounds(350, 350, 200, 200); dialog.setVisible(true); } public static void main(String[] args) { new MyDialog(); } } 
+1
Dec 06 '12 at 17:35
source share

This static utility method reveals a modal JFrame by secretly opening a modal JDialog. I have used this successfully and with proper behavior on computers with Windows 7, 8 and 10 with multiple computers.

This is a good example for a very rarely used function of local classes.

 import javax.swing.*; import java.awt.Dialog; import java.awt.Dimension; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; // ... (class declaration) /** * Shows an already existing JFrame as if it were a modal JDialog. JFrames have the upside that they can be * maximized. * <p> * A hidden modal JDialog is "shown" to effect the modality. * <p> * When the JFrame is closed, this method listener will pick up on that, close the modal JDialog, and remove the * listener. * * made by dreamspace-president.com * * @param window the JFrame to be shown * @param owner the owner window (can be null) * @throws IllegalArgumentException if argument "window" is null */ public static void showModalJFrame(final JFrame window, final Frame owner) { if (window == null) { throw new IllegalArgumentException(); } window.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE); window.setVisible(true); window.setAlwaysOnTop(true); final JDialog hiddenDialogForModality = new JDialog(owner, true); final class MyWindowCloseListener extends WindowAdapter { @Override public void windowClosed(final WindowEvent e) { window.dispose(); hiddenDialogForModality.dispose(); } } final MyWindowCloseListener myWindowCloseListener = new MyWindowCloseListener(); window.addWindowListener(myWindowCloseListener); final Dimension smallSize = new Dimension(80, 80); hiddenDialogForModality.setMinimumSize(smallSize); hiddenDialogForModality.setSize(smallSize); hiddenDialogForModality.setMaximumSize(smallSize); hiddenDialogForModality.setLocation(-smallSize.width * 2, -smallSize.height * 2); hiddenDialogForModality.setVisible(true); window.removeWindowListener(myWindowCloseListener); } 
+1
Nov 28 '15 at 7:55
source share

There is some code that may help:

 import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class ModalJFrame extends JFrame { Object currentWindow = this; public ModalJFrame() { super(); super.setTitle("Main JFrame"); super.setSize(500, 500); super.setResizable(true); super.setLocationRelativeTo(null); JMenuBar menuBar = new JMenuBar(); super.setJMenuBar(menuBar); JMenu fileMenu = new JMenu("File"); JMenu editMenu = new JMenu("Edit"); menuBar.add(fileMenu); menuBar.add(editMenu); JMenuItem newAction = new JMenuItem("New"); JMenuItem openAction = new JMenuItem("Open"); JMenuItem exitAction = new JMenuItem("Exit"); JMenuItem cutAction = new JMenuItem("Cut"); JMenuItem copyAction = new JMenuItem("Copy"); JMenuItem pasteAction= new JMenuItem("Paste"); fileMenu.add(newAction); fileMenu.add(openAction); fileMenu.addSeparator(); fileMenu.add(exitAction); editMenu.add(cutAction); editMenu.add(copyAction); editMenu.addSeparator(); editMenu.add(pasteAction); newAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JFrame popupJFrame = new JFrame(); popupJFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { ((Component) currentWindow).setEnabled(true); } }); ((Component) currentWindow).setEnabled(false); popupJFrame.setTitle("Pop up JFrame"); popupJFrame.setSize(400, 500); popupJFrame.setAlwaysOnTop(true); popupJFrame.setResizable(false); popupJFrame.setLocationRelativeTo(getRootPane()); popupJFrame.setVisible(true); popupJFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); } }); exitAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.exit(0); } }); } public static void main(String[] args) { ModalJFrame myWindow = new ModalJFrame(); myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myWindow.setVisible(true); } } 
0
May 17 '13 at 7:33
source share

What I did in this case is that basically the jframe that I want to keep visible (like a menu frame), I focusableWindowState option in the properties window so that it is FALSE . Once this is done The jframes that I call do not lose focus until I close them.

0
Oct 31 '14 at 6:40
source share

As already mentioned, you can use JDialog. If you donโ€™t have access to the parent frame or want to freeze a hole application, just pass null as the parent:

final JDialog frame = new JDialog((JFrame)null, frameTitle, true); frame.setModal(true);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);

0
Aug 16 '19 at 15:13
source share

not sure about the content of your JFrame, if you ask about some input from users, you can use JOptionPane, this can also set JFrame as modal

  JFrame frame = new JFrame(); String bigList[] = new String[30]; for (int i = 0; i < bigList.length; i++) { bigList[i] = Integer.toString(i); } JOptionPane.showInputDialog( frame, "Select a item", "The List", JOptionPane.PLAIN_MESSAGE, null, bigList, "none"); } 
-2
Oct 23 '12 at 15:08
source share

The easiest way is to use the pack () method before rendering the JFrame object. here is an example:

 myFrame frm = new myFrame(); frm.pack(); frm.setVisible(true); 
-3
Mar 25 '17 at 17:07
source share



All Articles