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

Using synchronized with show() causes hang after dimissing a modal dialog.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.6, 1.2.0
    • client-libs
    • x86
    • windows_95, windows_nt



      Name: clC74495 Date: 06/18/98


      compiled under jdk1.1.6 with the ea2 version of the jit.
      javac DialogTest
      java DialogTest

      Puts up a frame with a button. Pressing the button shows a modal
      dialog. After dismissing the modal dialog, the application is
      hung and must be killed. Happens under both 95 and NT.

      Also, show has been deprecated in java.awt.Component but is used
      in java.awt.Dialog. This doesn't make sense. Compiler gives
      deprecation warnings.

      ----------------------------------------------------------------

      import java.awt.*;
      import java.awt.event.*;
      import java.applet.*;
      import java.util.*;

      public class DialogTest extends Applet implements ActionListener
      {
      public static void main(String args[])
      {
      Applet applet;
      try {
      applet = new DialogTest();
      applet.init();
      applet.start();
              } catch(Exception e) {
      e.printStackTrace();
      }
      }

      public Dimension getAppletSize()
      {
      return new Dimension(500, 125);
      }

      public void init()
      {
      super.init();

      BasicFrame frame = new BasicFrame(this, "Test");
      Dimension size = getAppletSize();
      frame.setSize(frame.getInsets().left + frame.getInsets().right + size.width,
      frame.getInsets().top + frame.getInsets().bottom + size.height);
      frame.add(this, "Center");

              setLayout(null);
      setBackground(new Color(0x336666));

              Button button = new Button("Test");
              button.setBounds(200, 20, 75, 25);
              button.addActionListener(this);
              add(button);

      frame.show();

          }

          public void actionPerformed(ActionEvent event)
          {
              BasicDialog dialog;
              Frame frame = getParentFrame(this);
              dialog = new BasicDialog(frame, "Test", "OK");
              dialog.show();
          }

          public static Frame getParentFrame(Component sibling)
      {
      Frame parent = null;
              Component current = sibling;
              while((current = current.getParent()) != null)
              {
                  if(current instanceof Frame)
                  {
                      parent = (Frame) current;
                      break;
                  }
              }
              if(parent == null)
              {
                  throw(new RuntimeException("Can't find Parent Frame"));
              }
              return parent;
      }

          class BasicFrame extends Frame implements WindowListener
          {
              protected Applet applet;

              public BasicFrame(Applet applet, String title)
              {
           super(title);
           this.applet = applet;
           setBackground(Color.lightGray);
           addWindowListener(this);
              }

              public void windowClosed(WindowEvent event)
              {
              }

              public void windowDeiconified(WindowEvent event)
              {
              }

              public void windowIconified(WindowEvent event)
              {
              }

              public void windowActivated(WindowEvent event)
              {
              }

              public void windowDeactivated(WindowEvent event)
              {
              }

              public void windowOpened(WindowEvent event)
              {
              }

              public void windowClosing(WindowEvent event)
              {
                  System.exit(0);
              }
          }

          class BasicDialog extends Dialog implements ActionListener, WindowListener
          {
              protected Button acceptButton = null;

              public BasicDialog(Frame parent, String title, String acceptTitle)
              {
           super(parent, title, true);
           addWindowListener(this);
           setResizable(false);
                  setSize(400, 300);

                  if(acceptTitle != null) {
                   acceptButton = new Button(acceptTitle);
                  }
              }

              public void addNotify()
              {
                  super.addNotify();

                  setLayout(new BorderLayout());

                  if(acceptButton != null) {
               acceptButton.addActionListener(this);
                      add(acceptButton, "South");
                  }
              }

              public void actionPerformed(ActionEvent event)
              {
                  Button source = (Button) event.getSource();
                  if(source == acceptButton) {
                      close();
                  }
              }

              public void centerDialogInParent()
              {
                  Rectangle parentBounds = getParent().getBounds();
                  if(parentBounds.width > 0 && parentBounds.height > 0) {
                   Rectangle bounds = getBounds();
                   setLocation(parentBounds.x + (parentBounds.width - bounds.width)/ 2,
                              parentBounds.y + 35);
                  }
              }

          // First thing, show has been deprecated in java.awt.component but still exists
          // in java.awt.dialog.
          //
          // Secondly adding the synchronized keyword to show causes major problems after
          // the dialog has been shown once and disposed.

              public synchronized void show()
          // public void show()
              {
                  centerDialogInParent();
               super.show();
              }

              public void setVisible(boolean b)
              {
                  centerDialogInParent();
               super.setVisible(b);
              }

              public void close()
              {
                  dispose();
              }

              public void windowClosed(WindowEvent event)
              {
              }

              public void windowDeiconified(WindowEvent event)
              {
              }

              public void windowIconified(WindowEvent event)
              {
              }

              public void windowActivated(WindowEvent event)
              {
              }

              public void windowDeactivated(WindowEvent event)
              {
              }

              public void windowOpened(WindowEvent event)
              {
              }

              public void windowClosing(WindowEvent event)
              {
                  close();
              }
          }
      }
      (Review ID: 33837)
      ======================================================================

            rkhansunw Robi Khan (Inactive)
            clucasius Carlos Lucasius (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: