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

Incorrect focus events from JMenuBar, JMenu, JMenuItem

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs
    • x86
    • windows_nt



      Name: nt126004 Date: 09/25/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)


      Navigating through the JMenu(Bar) causes permanent focusGained, focuslost
      events on JComponents, e.g. JTextField, placed in the contentPane of a JFrame.

      When focus goes from a JTextField to a JMenu, the FocusListener of the
      JTextField gets a temporary focus lost. While navigating through the JMenu
      several focusLost and focusGained events ocured; not only when moving the mouse
      cursor from one JMenu to another JMenu, but also when moving from a sub JMenu
      which contents several JMenuItems to the super JMenu.
      What I expect is that the focus always remains at the JMenu as long as I don't
      leave the JMenu.

      Example:
      Execute the program.
      Set the focus into the second Prename field.
      Select with the mouse cursor the Management menu. The focusListener gets an
      temporary focusLost event.
      Then select the Management->act menu by moving the mouse cursor on this menu.
      After that return the mouse cursor to the Management menu. The focusListener
      gets a focusGained event.

      event history:
      FocusListener_1.focusLost() with Event :java.awt.event.FocusEvent
      [FOCUS_LOST,temporary,opposite=javax.swing.JPopupMenu.......
      caused by : javax.swing.JTextField[top panel_Feld_2,.......
      FocusListener_1.focusGained() with event :java.awt.event.FocusEvent
      [FOCUS_GAINED,permanent,opposite=javax.swing.JPopupMenu
      caused by : javax.swing.JTextField[top panel_Feld_1,....
      FocusListener_1.focusLost() with Event :java.awt.event.FocusEvent
      [FOCUS_LOST,permanent,opposite=javax.swing.JPopupMenu
      caused by : javax.swing.JTextField[top panel_Feld_1,.............

      Example Program :

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;


      public class Application1 {
        boolean packFrame = false;

        /**Construct the application*/
        public Application1() {
          Frame1 frame = new Frame1();
          //Validate frames that have preset sizes
          //Pack frames that have useful preferred size info, e.g. from their layout
          if (packFrame) {
            frame.pack();
          }
          else {
            frame.validate();
          }
          //Center the window
          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
          Dimension frameSize = frame.getSize();
          if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
          }
          if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
          }
          frame.setLocation((screenSize.width - frameSize.width) / 2,
      (screenSize.height - frameSize.height) / 2);
          frame.setVisible(true);
        }
        /**Main method*/
        public static void main(String[] args) {
          try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          }
          catch(Exception e) {
            e.printStackTrace();
          }
          new Application1();
        }
      }


      class Frame1 extends JFrame {
        JPanel contentPane;
        JMenuBar jMenuBar1 = new JMenuBar();
        JMenu jMenuFile = new JMenu();
        JMenuItem jMenuFileExit = new JMenuItem();
        JMenu jMenuVerwaltung = new JMenu();
        JMenu jMenuVerwaltungVorgang = new JMenu();
        JMenuItem jMenuItemVerwaltungVorgangDelete = new JMenuItem();

        JMenu jMenuHelp = new JMenu();
        JMenuItem jMenuHelpAbout = new JMenuItem();
        BorderLayout borderLayout1 = new BorderLayout();

        FocusListener fl_1;


        /**Construct the frame*/
        public Frame1() {
          enableEvents(AWTEvent.WINDOW_EVENT_MASK);
          try {
            jbInit();
          }
          catch(Exception e) {
            e.printStackTrace();
          }
        }
        /**Component initialization*/
        private void jbInit() throws Exception {
          //setIconImage(Toolkit.getDefaultToolkit().createImage
      (Frame1.class.getResource("[Your Icon]")));
          contentPane = (JPanel) this.getContentPane();
          contentPane.setLayout(borderLayout1);
          this.setSize(new Dimension(600, 600));
          this.setTitle("FocusManager test");

          fl_1 = new FocusListener() {
            public void focusGained( FocusEvent e ) {
              System.out.println("FocusListener_1.focusGained() with event :");
              System.out.println( e.toString());
              System.out.println( "caused by : " + e.getSource() );
      // Component comp = e.getOppositeComponent();
      // if ( comp != null ) {
      // System.out.println( "oppositeComponent = " + comp.getName() );
      // }
            }

            public void focusLost( FocusEvent e ) {
              System.out.println("FocusListener_1.focusLost() with Event :");
              System.out.println(e);
              System.out.println( "caused by : " + e.getSource() );
      // Component comp = e.getOppositeComponent();
      // if ( comp != null ) {
      // System.out.println( "oppositeComponent = " + comp.getName() );
      // }

            }
          };

          JPanel topPanel = new oberesPanel("top panel", fl_1);
          contentPane.add( topPanel );

          jMenuFile.setText("File");
          jMenuFile.setName("File");
          jMenuFileExit.setText("Exit");
          jMenuFileExit.setName("File_Exit");
          jMenuFileExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              jMenuFileExit_actionPerformed(e);
            }
          });

          jMenuVerwaltung.setText("Management");
          jMenuVerwaltung.setName("Management");
          jMenuVerwaltungVorgang.setText("act");
          jMenuVerwaltungVorgang.setName("Management_act");
          jMenuItemVerwaltungVorgangDelete.setText("delete");
          jMenuItemVerwaltungVorgangDelete.setName("delete");

          jMenuHelp.setText("Help");
          jMenuHelp.setName("Help");
          jMenuHelpAbout.setText("About");

          jMenuFile.add(jMenuFileExit);
          jMenuVerwaltung.add(jMenuVerwaltungVorgang);
          jMenuVerwaltungVorgang.add(jMenuItemVerwaltungVorgangDelete);

          jMenuHelp.add(jMenuHelpAbout);
          jMenuBar1.add(jMenuFile);
          jMenuBar1.add(jMenuVerwaltung);
          jMenuBar1.add(jMenuHelp);
          jMenuBar1.setName("MenuBar");
          this.setJMenuBar(jMenuBar1);
        }

        /**File | Exit action performed*/
        public void jMenuFileExit_actionPerformed(ActionEvent e) {
          System.exit(0);
        }

        /**Overridden so we can exit when window is closed*/
        protected void processWindowEvent(WindowEvent e) {
          super.processWindowEvent(e);
          if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            jMenuFileExit_actionPerformed(null);
          }
        }
      }

      class oberesPanel extends JPanel {
        JLabel jLabel1 = new JLabel();
        JLabel jLabel2 = new JLabel();
        JTextField jTextField1 = new JTextField();
        JLabel jLabel3 = new JLabel();
        JTextField jTextField2 = new JTextField();

        public oberesPanel(String text, FocusListener fl) {
          try {
            jbInit(text, fl);
          }
          catch(Exception e) {
            e.printStackTrace();
          }
        }
        private void jbInit(String text, FocusListener fl) throws Exception {

          jLabel1.setFont(new java.awt.Font("Dialog", 1, 16));
          jLabel1.setText(text);
          jLabel1.setBounds(new Rectangle(135, 12, 117, 17));
          this.setLayout(null);
          jLabel2.setText("Surname :");
          jLabel2.setBounds(new Rectangle(27, 48, 61, 17));
          jTextField1.setBounds(new Rectangle(99, 46, 170, 21));
          jTextField1.addFocusListener( fl );
          jTextField1.setName(text + "_Feld_1" );

          jLabel3.setText("Prename :");
          jLabel3.setBounds(new Rectangle(25, 93, 61, 17));
          jTextField2.setBounds(new Rectangle(100, 90, 170, 21));
          jTextField2.addFocusListener( fl );
          jTextField2.setName(text + "_Feld_2" );

          this.add(jLabel1, null);
          this.add(jLabel2, null);
          this.add(jTextField1, null);
          this.add(jLabel3, null);
          this.add(jTextField2, null);
        }


      }
      (Review ID: 131307)
      ======================================================================

            son Oleg Sukhodolsky (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: