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

If JFrame is resized then JMenu's dropdown changes its position

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1.5
    • client-libs
    • None

      Using swing-1.0.1 with /set/java/jdk/JavaSoft/sparc-S2/jdk1.1.5.
      Solaris 5.6.

      To reproduce:

      1) Shrink a JFrame so it's very narrow.
      2) Drop down some of the menus.
      - There's a gap between the dropdown and the menubar
      3) Restore the JFrame back to its original size.
      4) Drop down the menus again.
      - dropdown still in the wrong position.

      Another symptom is the Dropdown may partially cover the menubar.

      Here's a sample program:

      /**
       * JComps demonstrates the use of menus in Swing and adding and removing
       * Swing components to a JPanel.
       *
       */

      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;

      /**
       * JComps
       *
       */
      public class JComps extends JFrame implements ActionListener, ContainerListener
      {
        JLabel lOutput = new JLabel("Action goes here");
        Container contentPane;
        JPanel compsPanel;
        int i = 0;

      /**
       * JComps constructor
       *
       */
        public JComps() {
          super("JComps");
          setSize(500,500);
          contentPane = getContentPane();
          contentPane.setLayout(new BorderLayout());
          JMenuBar jmb = new JMenuBar();
          JMenu jCompMenu = new JMenu("Component");
          JMenuItem textAreaItem = new JMenuItem("Add JTextArea");
          textAreaItem.addActionListener(this);
          JMenuItem listItem = new JMenuItem("Add JList");
          listItem.addActionListener(this);
          JMenuItem labelItem = new JMenuItem("Add JLabel");
          labelItem.addActionListener(this);
          JMenu jRemoveMenu = new JMenu("Remove");
          JMenuItem rItem = new JMenuItem("Remove Component");
          rItem.addActionListener(this);
          JMenuItem exitItem = new JMenuItem("Exit");
          exitItem.addActionListener(this);
          jCompMenu.add(textAreaItem);
          jCompMenu.add(listItem);
          jCompMenu.add(labelItem);
          jCompMenu.add(exitItem);
          jRemoveMenu.add(rItem);
          jmb.add(jCompMenu);
          jmb.add(jRemoveMenu);
          setJMenuBar(jmb);
          compsPanel = new JPanel();
          compsPanel.addContainerListener(this);
          compsPanel.setLayout(new BorderLayout());
          compsPanel.setBackground(Color.white);
          Label lbl = new Label("Component chosen from menu");
          compsPanel.add(lbl, "South");
          contentPane.add(lOutput, "South");
          contentPane.add(compsPanel, "Center");
          addMouseListener(new MyMouseAdapter());
        }

          /** All menu item commands are handled by this method
          */
          public void actionPerformed(ActionEvent e) {
              String cmd = e.getActionCommand();

              if (cmd.equals("Add JTextArea")) {
                  lOutput.setText("JTextArea menuitem chosen");
                  JTextArea jta;
                  jta = new JTextArea("This is text inside a JTextArea");
                  jta.setMinimumSize(new Dimension(1, 1));
                  compsPanel.add(jta, "Center");
      compsPanel.validate();
              } else if (cmd.equals("Add JList")) {
                  lOutput.setText("JList menuitem chosen");

                  // instantiate new list
                  String[] items = {"one", "two", "free", "four"};
                  JList list = new JList(items);
                  compsPanel.add(list, "Center");
      compsPanel.validate();
              } else if (cmd.equals("Add JLabel")) {
                  lOutput.setText("JLabel menuitem chosen");
             
                  // instantiate new label
                  JLabel lb = new JLabel("this is a JLabel");
                  // lb.addActionListener(this); ** won't compile **

                  compsPanel.add(lb, "Center");
      compsPanel.validate();
              } else if (cmd.equals("Remove Component")) {
                  lOutput.setText("Remove component menuitem chosen");
                  
                  // remove previous component from container
                  Component cmp = compsPanel.getComponent(1);

                  if (cmp != null) {
                      compsPanel.remove(cmp);
                      compsPanel.validate();
                  }
              } else if (cmd.equals("Exit")) {
                System.out.println("Exit menuitem was chosen");
                dispose();

              }
          } // end of actionPerformed(ActionEvent e)

         // Container event handler methods
         public void componentAdded(ContainerEvent e) {
             System.out.println("COMPONENT_ADDED: " + e.getChild());
         }
         public void componentRemoved(ContainerEvent e) {
             System.out.println("COMPONENT_REMOVED: " + e.getChild());
         }

      /**
       * not implemented
       */
          class MyMouseAdapter extends MouseAdapter {}
      /**
       * not implemented
       */
          class MyMouseMotionAdapter extends MouseMotionAdapter {}
      /**
       * not implemented
       */
          class MyKeyAdapter extends KeyAdapter {}

        public static void main(String args[]) {
            JComps jc = new JComps();
            jc.setVisible(true);
        }

      }





            gsaab Georges Saab
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: