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

lightweight button mnemonic not invoked correctly if focus on heavyweight

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs
    • None



      Name: dm26566 Date: 09/18/2001


      The following program mixes heavweight and lightweight components, leaving
      focus on the heavyweight. If the lightweight button mnemonic is pressed,
      the lightweight button will be selected, however it will not be released.

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

                  /*
                   * This program demonstrates a problem with Mnemonics if
                   * a heavywight component has focus when a lightweight
                   * mnemonic is selected. What happens is that the key-pressed
                   * event is not passed to the lightweight component and only
                   * the key-released event is. This confuses the button into
                   * thinking that it is still selected. The solution is to
                   * provide focus to a swing component if swing and awt
                   * components are mixed.
                   */

                  public class Mnem extends JFrame {
                      public Mnem (boolean beingGood) {
                          super();
                          getContentPane().setLayout(new FlowLayout());

                          Button awtButton = new Button("AWT Button");

                          JButton swingButton = new JButton("Hold Meta-key/ALT+1");
                          swingButton.setMnemonic(KeyEvent.VK_1);


                          JLabel label = new JLabel((beingGood?"GOOD BEHAVIOR":"BAD
      BEHAVIOR"));
                          getContentPane().add(label);
                          getContentPane().add(awtButton);
                          getContentPane().add(swingButton);

                          addWindowListener(new WindowAdapter() {
                                  public void windowClosing(WindowEvent e) {
                                      System.exit(0);
                                  }
                              });

                          Dimension ScreenSize =
       Toolkit.getDefaultToolkit().getScreenSize();
                          Dimension thisSize = getSize();
                          int x = ScreenSize.width / 2 - thisSize.width / 2;
                          if (x < 0)
                              x = 0;
                          int y = ScreenSize.height / 2 - thisSize.height / 2;
                          if (y < 0)
                              y = 0;
                          setLocation(x, y);
                          pack();

                          if (beingGood) {
                              swingButton.requestFocus();
                          } else {
                              awtButton.requestFocus();
                          }
                          setVisible(true);
                      }

                      public static void main(String[] args) {
                          if (args.length > 0) {
                              new Mnem( true );
                          } else {
                              new Mnem( false );
                          }

                      }
                  }


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

            dmcduffisunw Dale Mcduffie (Inactive)
            dmcduffisunw Dale Mcduffie (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: