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

Dialog shown after pack() even if hidden

XMLWordPrintable

    • 1.0.2
    • sparc
    • solaris_2.4, solaris_2.5
    • Verified

      This is using the nightly build on 6 Dec.

      I have an application that creates a Dialog ahead of it being needed,
      so that when the time comes, I can just go `show()' it.

      Even though the dialog reports itself as `hidden', it is nevertheless
      made visible on the screen after the call of pack. (and continues to
      report itself hidden.)

      Attached is a test case program. The dialog (labelled HelpAboutDialog)
      is not supposed to be visible until the Help...About... menu entry is
      selected on the main window. Note the print statement after the
      dialog packs itself; it reports 'hidden' even though it is on the screen.

      -- Jon

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

      import java.awt.Canvas;
      import java.awt.Color;
      import java.awt.Dialog;
      import java.awt.Dimension;
      import java.awt.Event;
      import java.awt.Frame;
      import java.awt.Graphics;
      import java.awt.GridBagConstraints;
      import java.awt.GridBagLayout;
      import java.awt.Menu;
      import java.awt.MenuBar;
      import java.awt.MenuItem;
      import java.awt.Rectangle;


      public class Main extends Frame
      {
        public static void main(String [] args) {
          new Main().show();
        }
          
        Main() {
          MenuBar mb = new MenuBar();
          Menu helpMenu = new Menu("Help");
          mb.add(helpMenu);
          mb.setHelpMenu(helpMenu);
          setMenuBar(mb);
          add("Center", new DemoCanvas(200, 100, Color.yellow));
          pack();

          helpMenu.add(new HelpAbout(this));
        }
      }

      class HelpAbout extends MenuItem
      {
        public HelpAbout(Frame parent) {
          super("About");
          this.parent = parent;
          dialog = new HelpAboutDialog(parent);
        }

        public void postEvent(Event evt) {
          if (evt.id == Event.ACTION_EVENT && getLabel().equals(evt.arg)) {
            Dimension s = dialog.size();
            Rectangle b = parent.bounds();
            dialog.move(b.x + (b.width - s.width)/2, b.y + (b.height - s.height)/2);
            dialog.show();
          }
        }

        private Frame parent;
        private Dialog dialog;
      }


      class HelpAboutDialog extends Dialog
      {
        HelpAboutDialog(Frame parent) {
          super(parent, "HelpAboutDialog", false);
          setResizable(false);
          GridBagLayout gbl = new GridBagLayout();
          GridBagConstraints c = new GridBagConstraints();
          setLayout(gbl);
          c.gridwidth = GridBagConstraints.REMAINDER;
          c.anchor = GridBagConstraints.CENTER;
          c.insets.top = c.insets.bottom = c.insets.left = c.insets.right = 10;

          Color[] colors = {Color.red, Color.green, Color.blue};

          for (int i = 0; i < colors.length; i++) {
            DemoCanvas dc = new DemoCanvas(100, 30, colors[i]);
            gbl.setConstraints(dc, c);
            add(dc);
          }

          pack();
          System.out.println(this);
        }

        public boolean handleEvent(Event evt) {
          switch (evt.id) {
          case Event.WINDOW_DESTROY:
            dispose();
            return true;
          }

          return false;
        }
      }


      class DemoCanvas extends Canvas
      {
        DemoCanvas(int w, int h, Color c) {

          this.c = c;
          d = new Dimension(w, h);
        }

        public void paint(Graphics g) {
          Dimension s = size();
          g.setColor(c);
          g.drawLine(0, 0, s.width, s.height);
          g.drawLine(s.width, 0, 0, s.height);
          g.drawRect(0, 0, s.width-1, s.height-1);
        }

        public Dimension minimumSize() { return d; }
        public Dimension preferredSize() { return d; }

        Color c;
        Dimension d;
      }

      The description field as copied from bug report 1233258 follows:

      If you create a Dialog object and call pack() on it, the dialog will become
      mapped prematurely (even if show() is never called!). This causes the
      Dialog to be visible even though it's state in "not showing" , therefore if
      you subsequently call hide(), the Dialog will remain because it believes
      it is not showing and doesn't need to be taken down.

      I suspect that this may be a Solaris-only bug caused by a Motif idiosyncrasy.

            amfowler Anne Fowler (Inactive)
            jjg Jonathan Gibbons
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: