Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflicting mnemonics in Java Swing

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?

like image 523
Rex Avatar asked Nov 13 '22 01:11

Rex


1 Answers

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.

like image 93
davidXYZ Avatar answered Dec 23 '22 17:12

davidXYZ