wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes mnemonics and added characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "mnemonics and added characters" Watch "mnemonics and added characters" New topic
Author

mnemonics and added characters

Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
Here's one for you. Sometimes when I add a mnemonic to a JLabel, the corresponding JTextField gets an added character. That is, if the mnemonic is Alt C for Change, and the user selects Alt C, a C is added to the JTextField.
Martha Cofran
Greenhorn

Joined: Jan 31, 2001
Posts: 11
This is actually a documented java1.2 bug. It has been fixed in java1.3. As yet, I've not been able to find anyone with a good work around.
Sriram Sankar
Greenhorn

Joined: Mar 01, 2001
Posts: 22
Hello,
I also had the same problem. But i used another approach. Instead of the mnemonics, use the keystroke class. It gives you lots of options. Atleast it is working for me.
Pls create a keystroke method and register it to a jbutton, then it will work.
Here is the sample code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KeyStrokeSample {
public static void main(String args[]) {
JFrame frame = new ExitableJFrame("KeyStroke Sample");
JButton buttonA = new JButton(
"<html><center>FOCUSED
control alt 7");
JButton buttonB =
new JButton(
"<html><center>FOCUS/RELEASE
VK_ENTER");
JButton buttonC =
new JButton(
"<html><center>ANCESTOR
VK_F4+SHIFT_MASK");
JButton buttonD =
new JButton(
"<html><center>WINDOW
' '");
// Define ActionListener
ActionListener actionListener =
new ActionListener() {
public void actionPerformed(
ActionEvent actionEvent) {
System.out.println(
"Activated");
}
};
KeyStroke controlAlt7 = KeyStroke.getKeyStroke(
"control alt 7");
buttonA.registerKeyboardAction(
actionListener, controlAlt7, JComponent.WHEN_
FOCUSED);

KeyStroke enter = KeyStroke.getKeyStroke(
KeyEvent.VK_ENTER, 0, true);
buttonB.registerKeyboardAction(
actionListener, enter, JComponent.WHEN_FOCUSED);

KeyStroke shiftF4 = KeyStroke.getKeyStroke(
KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
buttonC.registerKeyboardAction(
actionListener, shiftF4,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

KeyStroke space = KeyStroke.getKeyStroke(' ');
buttonD.registerKeyboardAction(
actionListener, space,
JComponent.WHEN_IN_FOCUSED_WINDOW);

Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(2,2));
contentPane.add(buttonA);
contentPane.add(buttonB);
contentPane.add(buttonC);
contentPane.add(buttonD);
frame.setSize(400, 200);
frame.setVisible(true);
}
}

NOTE: If you try to run the previous program with JFC/Swing release 1.1, a NullPointerException will be generated.

Sriram
 
wood burning stoves
 
subject: mnemonics and added characters
 
Similar Threads
Set mnemonic key
Using Label Mnemonic corrupts focused field?
setDisplayedMnemonic corrupts JTextField??
"Hot keys in Swings -- URGENT"
setMnemonic