简体   繁体   中英

java swing hide and show JOptionPane programmatically

I've been researching hiding and showing Swing dialogs associated with our main GUI frame programmatically. A number of answers I looked at referenced using the getOwnedWindows() method to get all children associated with the frame and then setting the visibility to false. This works great for HIDING the dialog. But, I also want to turn the visibility back on and when I do I do not see any of the buttons or icons for the dialog. Just the frame for the dialog with the title.

To summarize. I create a JOptionPane dialog:

JOptionPane.showConfirmDialog( this, "Eggs are not green!", "True Statement", JOptionPane.YES_NO_CANCEL_OPTION );

I set the dialog visibility to false

      for (Window win : getOwnedWindows())
      {
         win.setVisible(false);
      }

I wait a bit and then set the visibility to true

      for (Window win : getOwnedWindows())
      {
         win.setVisible(true);
      }

I see this: 在此处输入图像描述

Here is the complete toy program. I tried revalidate and repaint on the child window. That had no effect.

import javax.swing.*;
import java.awt.*;

import static java.util.concurrent.TimeUnit.SECONDS;

public class DialogOnTimeoutCloser extends JFrame
{
   DialogOnTimeoutCloser()
   {
      super( "JOptionPane programmatic visibillity test." );

      SwingUtilities.invokeLater(() -> {

         getContentPane().setLayout( new FlowLayout() );
         setPreferredSize( new Dimension( 300, 300 ));
         setLocationRelativeTo(null);
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         pack();
         setVisible(true);

         JOptionPane.showConfirmDialog( this, "Eggs are not green!", "True Statement", JOptionPane.YES_NO_CANCEL_OPTION );
      });
   }

   private void closeAllDialogs()
   {
      for (Window win : getOwnedWindows())
      {
         win.setVisible(false);
      }
   }

   private void openAllDialogs()
   {
      for (Window win : getOwnedWindows())
      {
         win.setVisible(true);
      }
   }

   public static void main(String[] args) throws InterruptedException
   {
      DialogOnTimeoutCloser dialogOnTimeoutCloser = new DialogOnTimeoutCloser();
      Thread.sleep(SECONDS.toMillis( 5 ));
      dialogOnTimeoutCloser.closeAllDialogs();
      Thread.sleep(SECONDS.toMillis( 5 ));
      dialogOnTimeoutCloser.openAllDialogs();
   }
}

I would suggest you to avoid showing of a JOptionPane twice, because it's a one-time object. The problem is, that JOptionPane clears the dialog when it goes closed. If you really want to show it twice you have to prevent it. Here is an example (but it's a hack for me):

import static java.util.concurrent.TimeUnit.SECONDS;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class DialogOnTimeoutCloser extends JFrame
{
   DialogOnTimeoutCloser()
   {
      super( "JOptionPane programmatic visibillity test." );

      SwingUtilities.invokeLater(() -> {

         getContentPane().setLayout( new FlowLayout() );
         setPreferredSize( new Dimension( 300, 300 ));
         setLocationRelativeTo(null);
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         pack();
         setVisible(true);

         JOptionPane.showConfirmDialog( this, "Eggs are not green!", "True Statement", JOptionPane.YES_NO_CANCEL_OPTION );
      });
   }

   private void closeAllDialogs()
   {
      for (Window win : getOwnedWindows())
      {
          WindowListener[] ls = win.getWindowListeners();
          win.removeWindowListener(ls[0]);
          win.addWindowListener(new WindowAdapter() {
            
            @Override
            public void windowClosing(WindowEvent e) {
                ls[0].windowClosing(e);
                
            }
            
            @Override
            public void windowClosed(WindowEvent e) {
                // prevent clear window on close
                
            }
        });
         win.setVisible(false);
      }
   }

   private void openAllDialogs()
   {
      for (Window win : getOwnedWindows())
      {
         win.setVisible(true);
      }
   }

   public static void main(String[] args) throws InterruptedException
   {
      DialogOnTimeoutCloser dialogOnTimeoutCloser = new DialogOnTimeoutCloser();
      Thread.sleep(SECONDS.toMillis( 5 ));
      dialogOnTimeoutCloser.closeAllDialogs();
      Thread.sleep(SECONDS.toMillis( 5 ));
      dialogOnTimeoutCloser.openAllDialogs();
   }
}

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