Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6483868

JDialog.setVisible(false) does not hide dialog.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P4
    • None
    • 5.0
    • client-libs
    • x86
    • linux

    Description

      FULL PRODUCT VERSION :
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Linux sydeqws17 2.4.19-4GB-SMP #1 SMP Thu Apr 22 09:17:16 EST 2004 i686 unknown

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      SUSE 8.X, XFree86-4.3.0-121, KDE 3.1.1

      A DESCRIPTION OF THE PROBLEM :
      When showing and hiding a JDilaog in quick succession, Swing reports that the JDialog is not showing, but about 50% of the time (must be a timing issue) the dialog window stays visible (although its contents are blank) and in the case of a modal dialog, the focus cannot be put on the parent frame - buttons and menus work OK but the keyboard input cannot be grabbed by the parent.

      Calling setVisible(false) again has no effect, but calling setVisible(true) will paint the contents of the dialog and followed by setVisible(false) will remove the dialog.

      Only occurs in JDK 1.5.X on Linux (checked windows, but not Mac OS) - checked 1.3.X and 1.4.X and they were OK (although maybe they just paint the dialog faster and don't trigger the timing issue?).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Display a frame, then show and hide a dialog in 2 consecutive swing events.

      The below example usually triggers the problem first go, but depending upon the speed of the window manager may require pushing the "show&hide" button multiple times to trigger the problem. On a Redhat9/Gnome2.2 machine, it only happenned about 30% of the time.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      dialog should not be shown
      ACTUAL -
      dialog is shown with no contents painted and focus cannot be put on parent frame

      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.FlowLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.SwingUtilities;

      public class Bogus extends JFrame
      {

          private JDialog dialog;
          
          public Bogus()
          {
              setTitle("Bogus Frame");
              setLocation(100, 100);
              getContentPane().setLayout(new BorderLayout());
              JPanel panel = new JPanel(new BorderLayout());
              getContentPane().add(panel);
              JTextArea text = new JTextArea("The buttons will work, but this text can't be edited while the Dialog is \"showing\"\nAlso, the \"X\" button will not close the dialog.\nstdout shows that swing thinks it's not visible\n\nIn addition, if the dialog is showing when swing thinks it isn't and you then hit \"setVisible(true)\"\nit won't repaint the contents properly even if I call repaint(), until you roll the mouse over the dialogs button");
              JScrollPane scrollPane = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
              panel.add(scrollPane, BorderLayout.CENTER);
              JPanel buttons = new JPanel(new FlowLayout());
              panel.add(buttons, BorderLayout.NORTH);
              JButton showHide = new JButton("Show&Hide");
              buttons.add(showHide);
              showHide.addActionListener(new ActionListener()
              {
                  public void actionPerformed(ActionEvent e)
                  {
                      showDialog();
                  }
              });

              JButton show = new JButton("setVisible(true)");
              buttons.add(show);
              show.addActionListener(new ActionListener()
              {
                  public void actionPerformed(ActionEvent e)
                  {
                      SwingUtilities.invokeLater(new Runnable(){
                          public void run()
                          {
                              dialog.setVisible(true);
                              dialog.repaint();
                          }
                      });
                  }
              });

              JButton hide = new JButton("setVisible(false)");
              buttons.add(hide);
              hide.addActionListener(new ActionListener()
              {
                  public void actionPerformed(ActionEvent e)
                  {
                      dialog.setVisible(false);
                  }
              });
              pack();
              
              dialog = new JDialog(this, true);
              dialog.setLocation(25, 25);
              dialog.setTitle("Bogus Dialog");
              dialog.getContentPane().setLayout(new FlowLayout());
              dialog.getContentPane().add(new JLabel("A Label"));
              JButton close = new JButton("setVisible(false)");
              close.addActionListener(new ActionListener() {

                  public void actionPerformed(ActionEvent e)
                  {
                      dialog.setVisible(false);
                  }
                  
              });
              dialog.getContentPane().add(close);
              dialog.pack();
              
              addWindowListener(new WindowAdapter()
              {

                  public void windowClosing(WindowEvent e)
                  {
                      System.exit(0);
                  }
                  
              });
              
              new Thread(new Runnable(){
                  public void run()
                  {
                      while (true)
                      {
                          try
                          {
                              Thread.sleep(2000);
                          }
                          catch (InterruptedException e)
                          {
                          }
                          System.out.println("isVisible():" + dialog.isVisible());
                      }
                  }
              }).start();
              
          }
          
          public void showDialog()
          {
              SwingUtilities.invokeLater(new Runnable(){
                  public void run()
                  {
                      System.out.println("Before setVisible(true): isVisible():" + dialog.isVisible());
                      // Modal, so this will block
                      dialog.setVisible(true);
                  }
              });
              SwingUtilities.invokeLater(new Runnable(){
                  public void run()
                  {
                      System.out.println("Before setVisible(false): isVisible():" + dialog.isVisible());
                      dialog.setVisible(false);
                      System.out.println("After setVisible(false): isVisible():" + dialog.isVisible());
                  }
              });
          }
          
          public static void main(String[] args)
          {
              System.err.println("os.name:" + System.getProperty("os.name"));
              SwingUtilities.invokeLater(new Runnable(){
                  public void run()
                  {
                      final Bogus b = new Bogus();
                      b.setVisible(true);
                      b.showDialog();
                  }
              });
          }
          
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      When displaying the dialog, had to time when it was displayed and if on linux and hadn't displayed for more than x milliseconds (500-1000), wait till then... Even this was not reliable in some cases

      Attachments

        Issue Links

          Activity

            People

              vbaranovsunw Vyacheslav Baranov (Inactive)
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: