-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.4.0
-
None
-
generic
-
generic
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 );
}
}
}
======================================================================
- duplicates
-
JDK-4500684 JCK1.4,JCK1.3a interactive:api/javax_swing/interactive/JButtonTests.html#JButton
-
- Closed
-