5

I'm working on a complex desktop app with multiple tabbed panes for workflow, each one stuffed with different buttons, labels and other UI elements. Each of them require a mnemonic defined, and these often come into conflict because of running out of letters to define.

I have noticed that on Windows, if there is the same mnemonic defined for two controls, then pressing it will cycle between them, and they activate upon releasing the key. With Swing, the mnemonics simply won't activate if you define 2 of them with the same key.

Is there a workaround for this?

Rex
  • 801
  • 1
  • 17
  • 26
  • 2
    not sure, maybe mistake in concept maybe in the code, for better help sooner post an SSCCE, – mKorbel Aug 01 '12 at 07:54
  • This sounds to me like you are not coding this yourself, but are letting a GUI builder do it for you. There's no such thing as a "mnemonic" in Java syntax. – Marko Topolnik Aug 01 '12 at 08:03
  • 5
    @MarkoTopolnik: http://docs.oracle.com/javase/6/docs/api/javax/swing/JLabel.html#setDisplayedMnemonic%28int%29; http://docs.oracle.com/javase/6/docs/api/javax/swing/AbstractButton.html#setMnemonic%28char%29 – JB Nizet Aug 01 '12 at 08:08
  • 1
    "There's no such thing as a "mnemonic" in Java syntax"????? – Vikram Aug 07 '12 at 19:52
  • When I define the same mnemonic for multiple `Action`s that I apply to `JMenuItem`s and `JButton`s, they cycle through when I press the mnemonic. Not sure what makes yours not work. A [SSCCE](http://sscce.org) would really help. – javatarz Sep 09 '12 at 19:28
  • This is for the case where you want to directly access a control by using alt+shortcut key, not for activating actions. So let's say I have a combobox and a textfield, with an associated label for each. If both labels have the same mnemonic letter, then alt+key doesn't work. – Rex Sep 10 '12 at 11:05

1 Answers1

1

My suggestion would be to use a KeyListener and then differentiate the actions based on what tab is showing.

Pseudo-code:

public void keyPressed(KeyEvent e){
    //assuming 'O' activates Open button on two different tabs
    if(key == 'O'){
        if(activeTab == tab1)
            doStuff1();
        else if(activeTab == tab2)
            doStuff2();
    }
}

You can find a way to make it work in real code.

davidXYZ
  • 719
  • 1
  • 8
  • 16