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

"Default" Button should be activated on Enter PRESS (not RELEASE)

XMLWordPrintable

    • beta
    • x86
    • windows_nt



      Name: dbT83986 Date: 12/18/98


      Hello,

      I'm using JDK v1.2b4 on WinNT v4.0, SP3.

      There's a subtle problem with the behavior of the "default" button under
      the Windows LaF. I've included a short sample program to illustrate the
      behavior. By "default" button I'm referring to the parameter to the
      method, JRootPane.setDefaultButton(JButton).

      When you press a default button in Windows (here I mean the Windows OS,
      not the Swing Windows LaF), the button is activated as soon as the ENTER
      key is pressed (i.e., before the key is released). Open any (native)
      dialog in Windows that has a default button and press the ENTER key.
      You'll see that as soon as the key is pressed the default button is
      activated.

      Under the Swing Windows LaF, however, the default button is not
      activated until after the ENTER key is *released*. This behavior is
      not consistent with true Windows behavior.

      Regards.

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


      class Tester
      {
         public static void main(String[] args)
         {
            final JFrame f = new JFrame();

            // WindowListener for main frame window.
            f.addWindowListener(new WindowAdapter()
            {
               public void windowClosing(WindowEvent evt)
               {
                  evt.getWindow().dispose();
                  System.exit(0);
               }
            });

            JButton button = new JButton("Howdy Ho");
            f.getContentPane().add(button, BorderLayout.NORTH);
            button.addActionListener(new ActionListener()
            {
               public void actionPerformed(ActionEvent evt)
               {
                  System.out.println("**** Button was pressed ****");
               }
            });
            
            f.getRootPane().setDefaultButton(button);

            //--------------------
            // The remaining code simply allows for a change in the LaF....
            //--------------------
            JPanel LaFPanel = new JPanel();

            final JButton windowsLaF = new JButton("Windows"),
                          motifLaF = new JButton("Motif"),
                          metalLaF = new JButton("Metal");

            LaFPanel.add(windowsLaF);
            LaFPanel.add(motifLaF);
            LaFPanel.add(metalLaF);

            ActionListener LaFListener = new ActionListener()
            {
               public void actionPerformed(ActionEvent evt)
               {
                  Object src = evt.getSource();

                  try
                  {
                     if(src == windowsLaF)
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                     else if(src == motifLaF)
             UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                     else
             UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel");

                     SwingUtilities.updateComponentTreeUI(f);
                  }
                  catch(Exception e)
                  {
                     System.err.println("*** ERROR IN CHANGING LAF: " + e);
                  }
               }
            };

            windowsLaF.addActionListener(LaFListener);
            motifLaF.addActionListener(LaFListener);
            metalLaF.addActionListener(LaFListener);

            f.getContentPane().add(LaFPanel, BorderLayout.SOUTH);
            f.setBounds(50, 50, 300, 300);
            f.setVisible(true);
         }
      }
      (Review ID: 37015)
      ======================================================================

            amfowler Anne Fowler (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: