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

Glasspane not catching keyboard events properly

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.2.0, 1.3.0
    • client-libs
    • x86
    • linux, windows_nt



      Name: rk38400 Date: 05/12/98


      I'm using WinNT v4.0, SP3 and JDK v1.1.6, Swing v1.0.2.

      I've attached a minimal sample program to illustrate the behavior.

      A couple weeks ago I sent in a bug report which described a problem
      where the glasspane of a JFrame would not trap keyboard events (although
      it would correctly trap mouse events). You replied to my report by
      saying that I could set the focus to the glasspane and that would
      accomplish the effect I was after.

      Here's a quote from the "setGlassPane()" method of the
      "RootPaneContainer" interface. This quote indicates I should simply be
      able to add a key listener to the glasspane and then make the glasspane
      visible:

          "The glassPane is always the first child of the
           rootPane and the rootPanes layout manager ensures
           that it's always as big as the rootPane. By default
           it's transparent and not visible. It can be used to
           temporarily grab all keyboard and mouse input by
           adding listeners and then making it visible.
           By default it's not visible."


      Furthermore, explicitly setting the focus to the glasspane (as you
      suggested) has two undesirable effects:

      1. If an underlying control (for example, a JButton on the content pane)
      has the focus, then setting the focus to the glasspane results in the
      focus rectangle being removed from the control.

      2. If a JTextField on the content pane has the focus when the glass pane
      is made visible, then the caret is removed from the JTextField when the
      glasspane receives the focus.

      I believe the glasspane should be able to trap all mouse/keyboard input
      without the underlying content pane changing its appearance (i.e.,
      carets and focus rectangles being removed).

      I was hoping you could re-evaluate this behavior and classify it as a
      bug -- it still seems to be a bug to me.


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


      public class Tester
      {
         public static void main(String[] args)
         {
            final JFrame f = new JFrame();
              
            f.addWindowListener(new WindowAdapter()
            {
               public void windowClosing(WindowEvent evt)
               {
                  ((Window)evt.getSource()).dispose();
                  System.exit(0);
               }
            });
            
            
            JPanel p = new JPanel(false);
            
            JButton glassPaneButton = new JButton("Make Glass Pane Visible");
            p.add(glassPaneButton);
            
            JTextField tf = new JTextField(15);
            p.add(tf);
            
            f.getContentPane().add(p, BorderLayout.CENTER);
            
            glassPaneButton.addActionListener(new ActionListener()
            {
               public void actionPerformed(ActionEvent evt)
               {
                  f.getGlassPane().setVisible(true);
                  // If you uncomment the line below, the glass pane will pick up
                  // all key presses. Unfortunately, any focus rectangle present
                  // on the underlying controls will also be removed.
                  //f.getGlassPane().requestFocus();
               }
            });
            
            f.getGlassPane().addMouseListener(new MouseAdapter()
            {
               public void mousePressed(MouseEvent evt)
               {
                  System.out.println(" ----- A mouse button was pressed...");
               }
            });
            
            f.getGlassPane().addKeyListener(new KeyAdapter()
            {
               public void keyPressed(KeyEvent evt)
               {
                  System.out.println(" ----- A key was pressed...");
               }
            });
            
            

            // The rest of this code simply changes the LaF...
            final JButton windowsButton = new JButton("Windows"),
                           motifButton = new JButton("Motif"),
                           metalButton = new JButton("Metal");
                     
            ActionListener LaFListener = new ActionListener()
            {
               public void actionPerformed(ActionEvent evt)
               {
                  JButton b = (JButton)evt.getSource();

                  try
                  {
                     if(b == windowsButton)
             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                     else if (b == motifButton)
             UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                     else
             UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel");

             SwingUtilities.updateComponentTreeUI(b.getRootPane());
          }
          catch(Exception e)
          {
          System.err.println("*** ERROR: " + e);
          System.exit(2);
          }
               }
            };
            
            windowsButton.addActionListener(LaFListener);
            motifButton.addActionListener(LaFListener);
            metalButton.addActionListener(LaFListener);
            
            JPanel LaFButtonsPanel = new JPanel(false);
            LaFButtonsPanel.add(windowsButton);
            LaFButtonsPanel.add(motifButton);
            LaFButtonsPanel.add(metalButton);
            
            f.getContentPane().add(LaFButtonsPanel, BorderLayout.SOUTH);

            f.setBounds(65, 65, 450, 190);
            f.setVisible(true);
         }
      }
      (Review ID: 30202)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            rkarsunw Ralph Kar (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: