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

Wrong status in FocusOut event when open system menu.

XMLWordPrintable

    • 1.2.2
    • generic
    • solaris_2.5.1

      When opening the system menu, FocusOut event will be sent to the components
      within the frame, and it should be a temporary one just like what we get
      when opening a user defined menu, not a permanent one. The following test
      case shows the problem.


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

      class TestFocus extends Component implements FocusListener {
      boolean haveFocus = false;
      public TestFocus(){
      super();
      addFocusListener(this);
      addMouseListener(new MouseFocusListener(this));
      }

              public Dimension getPreferredSize(){
      return new Dimension(200, 150);
              }

      public void focusGained(FocusEvent e){
      System.out.println("==>FocusGained:" + e);
      if (e.getSource() == this){
      haveFocus = true;
      paint(getGraphics());
      }
      }

      public void focusLost(FocusEvent e){
      System.out.println("==>FocusLost :" + e);
      if (e.getSource() == this){
      haveFocus = false;
      paint(getGraphics());
      }
      }

      public void paint(Graphics g){
      g.setColor(Color.yellow);
      Dimension size = getSize();
      g.fillRect(0, 0, size.width, size.height);
      if (haveFocus){
      g.setColor(Color.black);
      g.drawRect(0, 0, size.width - 1, size.height - 1);
      g.drawRect(1, 1, size.width - 3, size.height - 3);
      }
      }

              public static void main(String[] args) {
                      Frame f = new Frame("Test Focus");
      f.setLayout(new BorderLayout());
                      TestFocus test = new TestFocus();
      f.add(test);
                      f.setSize(new Dimension(320,200));

      //f.addFocusListener(test);
                      MenuBar mb = new MenuBar();
                      Menu m1 = new Menu("menu");
                      m1.add( new MenuItem("item1"));
                      m1.add( new MenuItem("item2"));
                      mb.add(m1);
                      f.setMenuBar(mb);
       
                      f.addWindowListener( new WindowAdapter() {
                              public void WindowClosing( WindowEvent e ) {
                                      e.getWindow().dispose();
                                      System.exit(0);
                              }
                              public void WindowClosed( WindowEvent e ) {
                                      System.exit(0);
                              }
                      } );
      f.pack();
                      f.setVisible(true);
      test.requestFocus();
              }

        class MouseFocusListener extends MouseAdapter {
          private Component target;
          MouseFocusListener(Component target) {
              this.target = target;
          }
          public void mouseClicked(MouseEvent e) {
              target.requestFocus();
          }
        }
      }

            duke J. Duke
            sherman Xueming Shen
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: