How can I block the keyboard using the glass pane ?

Advertisement
I have a problem with the GlassPaneDemo from the Java Tutorial in
the
uiswing/components/example-swing
folder.
When the glass pane is visible it blocks the mouse input but the
keyboard input doesn't. For example if the glass pane is visible and
you press the F10 key, the menu will be activated. So the keyboard is
not at all blocked.
Can this problem be fixed ? I mean, to really block the keyboard
when the glass pane is visible.
Here I wrote down the specification written in the
Java Documentation at the setGlassPane method from the
RootPaneContainer interface:
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 the making it visible. by default it's not visible.
As it may be seen, one says that the keyboard input can be blocked.
HOW ?
In hope that I resolve the problem, I have made some changes such as,
adding a key listener to the glass pane add listening to the keyboard events,
but I failed to fix the problem.
Faithfully yours,
Sarmis
Advertisement

Replay

Here is an example that I think will work for you.
Note the consume() call on the event object. I think that is what you're after.
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class Comparer extends JFrame implements ActionListener
    private GlassComponent myGlassPane;
    private JTextField myField;
    public Comparer()
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(myField = new JTextField(20));
        setGlassPane(myGlassPane = new GlassComponent());
        JButton theButton = new JButton("Glass");
        theButton.addActionListener(this);
        getContentPane().add(theButton);
        pack();
    public void actionPerformed(ActionEvent anEvent)
        myGlassPane.setVisible(true);       
    public static void main(String[] args)
        new Comparer().setVisible(true);       
class GlassComponent extends JComponent implements AWTEventListener
    Window myParentWindow;
    public GlassComponent()
        super();
        this.setCursor( Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR) );
        setOpaque(false);
        addMouseListener( new MouseAdapter() {} );
    public void setVisible(boolean aVisibleBoolean)
        if(aVisibleBoolean)
            if(this.myParentWindow == null)
                this.myParentWindow = SwingUtilities.windowForComponent(this);
            Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
        else
            Toolkit.getDefaultToolkit().removeAWTEventListener(this);
        super.setVisible(aVisibleBoolean);
    public void eventDispatched(AWTEvent anEvent)
        if(anEvent instanceof KeyEvent && anEvent.getSource() instanceof Component)
            if(SwingUtilities.windowForComponent( (Component)anEvent.getSource()) == this.myParentWindow )
                ((KeyEvent)anEvent).consume();
} HTH,
Fredrik