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

popup menus on buttons look funny

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • 1.1, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.7
    • client-libs
    • None
    • x86
    • windows_95, windows_nt



      Name: sg39081 Date: 08/15/97


      If you attach a popup menu to a button (which admittedly is
      an odd thing to do) it looks wrong. The menu items get the
      button's caption for their names instead of their own names,
      and they look like buttons rather than like menu items. Here's
      an example, sorry if it's a bit long.


      import java.awt.*;
      import java.awt.event.*;
      import java.lang.*;
      import java.util.Hashtable;

      public class action4
          extends Frame
          implements ActionListener, MouseListener {

        PopupMenu pm = new PopupMenu();

        public static void main (String argv[]) {
          new action4();
        }

        public action4() {
          MenuItem item = new MenuItem("file-1");
          item.addActionListener(this);
          Menu m = new Menu("file");
          m.add(item);
          item = new MenuItem("file-2");
          m.add(item);
          MenuBar mb = new MenuBar();
          mb.add(m);

          setMenuBar(mb);
          setSize(100, 100);
          setLayout(new BorderLayout());

          Label l = new Label("label");
          addPopup(l, "label");
          add(l, "North");

          Panel p = new Panel();
          addPopup(p, "Panel");
          add(p, "Center");

          Button b = new Button("button");
          addPopup(b, "button");
          add(b, "South");

          show();
        }

        public void actionPerformed(ActionEvent e) {
          System.out.println("actionPerformed, event=" + e + ", mod=" + getMods(e));
          System.out.println(" command=" + e.getActionCommand());
          System.out.println(" param=" + e.paramString());
          System.out.println(" source=" + e.getSource());
        }

        String getMods(ActionEvent e) { return getMods(e.getModifiers()); }

        String getMods(MouseEvent e) { return getMods(e.getModifiers()); }

        String getMods(int mods) {
          String modstr = "";
          if ((mods & ActionEvent.SHIFT_MASK) == ActionEvent.SHIFT_MASK)
            modstr += (" SHIFT");
          if ((mods & ActionEvent.ALT_MASK) == ActionEvent.ALT_MASK)
            modstr += (" ALT");
          if ((mods & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK)
            modstr += (" CTRL");
          if ((mods & ActionEvent.META_MASK) == ActionEvent.META_MASK)
            modstr += (" META");
          return modstr;
        }

        public void mouseClicked (MouseEvent e) {
          mouseAction("mouseClicked", e);
        }

        public void mouseEntered (MouseEvent e) {
          // System.out.println("mouseEntered " + e);
          // printMods(e.getModifiers());
        }

        public void mouseExited (MouseEvent e) {
          // System.out.println("mouseExited " + e);
          // printMods(e.getModifiers());
        }

        public void mousePressed (MouseEvent e) {
          mouseAction("mousePressed", e);
          // System.out.println("mousePressed " + e);
          // printMods(e.getModifiers());
        }

        public void mouseReleased (MouseEvent e) {
          mouseAction("mouseReleased", e);
          // System.out.println("mouseReleased " + e);
          // printMods(e.getModifiers());
        }

        void mouseAction (String which, MouseEvent e) {
          Component c = e.getComponent();
          System.out.println(which + "e=" + e + ", mods=" + getMods(e) +
                             ", component=" + c);
          if (e.isPopupTrigger()) {
            System.out.println("isPopup");
            PopupMenu pm = getHash(c);
            pm.show(c, c.getSize().width/2, c.getSize().height/2);
          }
        }


        void addPopup(Component c, String name) {
          PopupMenu pm = new PopupMenu();
          MenuItem mi = new MenuItem(name + "-1");
          mi.addActionListener(this);
          pm.add(mi);

          mi = new MenuItem(name + "-2");
          pm.add(mi);

          setHash(c, pm);
          c.add(pm);
          c.addMouseListener(this);
        }


        Hashtable popupTable = new Hashtable();

        void setHash(Component c, PopupMenu p) {
          popupTable.put(c, p);
        }

        PopupMenu getHash(Component c) {
          return (PopupMenu)(popupTable.get(c));
        }

      }


      company - intermetrics , email - ###@###.###
      ======================================================================

      daniel.indrigo@Canada 1997-09-19 - Here's another one

      1. Create a PopupMenu and associate it with a non-
      TextField components such as a button or a check
      box. When the popupmenu is shown, rather than
      displaying MenuItems, it will display a menu
      of buttons or checkboxes, whichever kind of
      component it was associated with.

      2. Here is the source. The popupmenu is shown
      when the checkbox is clicked.

      --------------------------------------
      import java.awt.*;
      public class appTest extends java.applet.Applet implements java.awt.event.ItemListener {
      private java.awt.PopupMenu badMenu;
      private java.awt.Checkbox checkbox = null;
      private java.awt.MenuItem lMenuItem;

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

      setLayout(null);
      setSize(426, 240);
      checkbox = new java.awt.Checkbox();
      checkbox.setName("checkbox");
      checkbox.setBounds(26, 43, 125, 30);
      checkbox.setLabel("checkbox");
      add(checkbox, "checkbox");
      checkbox.addItemListener(this);
      }

      public void itemStateChanged(java.awt.event.ItemEvent e) {
      badMenu = new java.awt.PopupMenu();
      badMenu.setLabel("Bad Menu");

      lMenuItem = new java.awt.MenuItem();
      lMenuItem.setActionCommand("One");
      lMenuItem.setLabel("One");
      badMenu.add(lMenuItem);

      lMenuItem = new java.awt.MenuItem();
      lMenuItem.setActionCommand("Two");
      lMenuItem.setLabel("Two");
      badMenu.add(lMenuItem);

      checkbox.add(badMenu);
      badMenu.show(checkbox, 5, 5);
      }

      public static void main(java.lang.String[] args) {
      try {
      java.awt.Frame frame;
      try {
      Class aFrameClass = Class.forName("uvm.abt.edit.TestFrame");
      frame = (java.awt.Frame)aFrameClass.newInstance();
      } catch (java.lang.Throwable ivjExc) {
      frame = new java.awt.Frame();
      }
      appTest aappTest = new appTest();
      frame.add("Center", aappTest);
      frame.setSize(aappTest.getSize());
      aappTest.init();
      aappTest.start();
      frame.setVisible(true);

      aappTest.destroy();
      } catch (Throwable exception) {
      System.err.println("Exception occurred in main() of java.applet.Applet");
      }
      }

      }
      -------------------------------------------

      This doesn't seem to occur with jdk1.1.1 but does
      with jdk1.1.3 and jdk1.1.4

      ==========================================================================


      File BugTest1.java
      ------------------
      /**
       * This application demonstrates that the labels of the menu items in a
       * popup menus assigned to a button are ignored.
       * @author Mark Yablonski
       */

      import java.awt.*;
      import java.awt.event.*;
      import ButtonPopup;

      public class BugTest1 extends Frame
      {
        private java.awt.TextField text_;
        private ButtonPopup button_;
        public BugTest1 () {}

        public BugTest1 (String title)
        {
          this();
          setTitle(title);
        }

        public synchronized void show()
        {
          move(50, 50);
          super.show();
        }

        static public void main(String args[])
        {
          (new BugTest1()).show();
        }

        public void addNotify()
        {
          // Call parents addNotify method.
          super.addNotify();
          setLayout(new GridLayout(3,1));
          setSize(200,100);
          setTitle("Popup Bug Demo");
          Action listener = new Action();

          text_ = new java.awt.TextField();
          add(text_);
          button_ = new ButtonPopup();
          add(button_);
          text_.addActionListener(listener);
          java.awt.Button button = new java.awt.Button("Exit");
          add(button);
          button.addActionListener(listener);

          // Initialize button's popup menu
          PopupMenu popup = new PopupMenu("Items");
          MenuItem mi1 = new MenuItem("First Item");
          popup.add(mi1);
          mi1.setActionCommand("first command");
          mi1.addActionListener(listener);
          MenuItem mi2 = new MenuItem("Second Item");
          popup.add(mi2);
          mi2.setActionCommand("second command");
          mi2.addActionListener(listener);
          MenuItem mi3 = new MenuItem("Third Item");
          popup.add(mi3);
          mi3.setActionCommand("third command");
          mi3.addActionListener(listener);
          button_.add(popup);
        }

        class Action implements java.awt.event.ActionListener
        {
          public void actionPerformed (java.awt.event.ActionEvent e)
          {
            if (e.getSource() instanceof MenuItem)
            {
      System.out.println("Action Command: "+e.getActionCommand());
            }
            else if (e.getSource() instanceof TextField)
            {
      System.out.println("User entered: " + text_.getText());
            }
            else
      System.exit(0);
          }
        }
      }

      File ButtonPopup.java
      ---------------------
      import java.awt.*;
      import java.awt.event.*;

      /**
       * This is a button which has a popup menu.
       *
       * @author Mark Yablonski
       */
      public class ButtonPopup extends java.awt.Button
      {
        private PopupMenu popup_ = null;
        public Action listener = null;

        public ButtonPopup ()
        {
          listener = new Action();
          this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
          addActionListener(listener);
          setLabel("I Have A Popup Menu");
        }

        public void add (PopupMenu popup)
        {
          popup_ = popup;
          super.add(popup);
        }

        public void processMouseEvent (MouseEvent e)
        {
          if (e.isPopupTrigger() && popup_ != null)
            popup_.show(this, e.getX(), e.getY());
          else
            super.processMouseEvent(e);
        }

        public class Action implements java.awt.event.ActionListener
        {
          public void actionPerformed (ActionEvent e)
          {
            System.out.println("Button pressed");
          }
        }
      }

            duke J. Duke
            sgoodsunw Sheri Good (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: