Mnemonic clashes

In our Swing application we use an MVC pattern enabling us to reuse views throughout the application.

One of our challanges with reusing views is the mnemonics. A view/panel containing JButtons doesn't not "know" where it will be used. Its mnemonics are read from properties files along with the button text.

For example: View A having button with text "OK" and mnemonic "O" can be placed in the same window as View B having button with text "Open" and mnemonic "O".

This clash can of course be avoided by changing the mnemonic of one of the buttons (by changing the properties file), for example button "Open" in view B can get the mnemonic "P" instead.

But if view B is also used in other JFrames, this "P" mnemonic might cause other clashes in another JFrames...

And also, if we want two instances of View A inside the same JFrame, we will definitely get a mnemonic clash.... Changing the properties file will not help...

Question:

1. Can anyone help me with "best-practices" regarding mnemonics in the MVC pattern?

2. Is there a simple way to detect mnemonic clashes at runtime without having to go through the entire swing hierarchy (using getComponents()) recursively on each swing component in the JFrame).. I do this today, but when a view has a JTree or JTable with cellRenderers/cellEditors containing JButtons, these are a bit more tricky... I would rather directly access the JFrame's "mnemonic map" (if there is such a thing) instead of scanning through all the Swing components...

3. Just an idea: if there is a simple answer to question 2, i was thinking of creating a wrapper to the mnemonic handler in Swing, so that if a mnemonic is used, I would first check if there is a clash. if true, i would pop up a JDialog with clones of all the buttons that have this mnemonic, letting the user use the tab key (or mouse) to select the correct button. When the user selects a clone, the JDialog is closed and the action is delegated to the original button in the JFrame.

Any comments to this idea? Is it possible? :)

cheers

[2109 byte] By [shmokka] at [2007-10-2 22:16:18]
# 1

In general, you should select only ONE mnemonic per function. The tricky part is finding a disjunct set of characters for all your functions. I personally would be feeling uncomfortable if I had to explain my customers "when you see this window, you have to use the 'o'-key for OK, but if you see this window as well, you have to use the 'k'-key for the same function".

Oh, and talking about user-friendlyness... So far I've seen absolutely NO GUI that would have legitimated two OK buttons at the same time...

> 2. Is there a simple way to detect mnemonic clashes

> at runtime without having to go through the entire

> swing hierarchy

Create a global set with all currently active mnemonics. When adding a new one to the GUI, check whether this set contains the same character or not.

falke2203a at 2007-7-14 1:33:20 > top of Java-index,Desktop,Developing for the Desktop...
# 2

> In general, you should select only ONE mnemonic per

> function. The tricky part is finding a disjunct set

> of characters for all your functions. I personally

> would be feeling uncomfortable if I had to explain my

> customers "when you see this window, you have to use

> the 'o'-key for OK, but if you see this window as

> well, you have to use the 'k'-key for the same

> function".

i agree..

> Oh, and talking about user-friendlyness... So far

> I've seen absolutely NO GUI that would have

> legitimated two OK buttons at the same time...

>

i agree again. but what about two "Add" buttons then? two subpanels in the same window, both offering a functionality to add something...

Both panels are imported (as reusable "tiles") and "plugged" into the window.

This is plain MVC, but how does MVC handle the mnemonic challenge?

shmokka at 2007-7-14 1:33:20 > top of Java-index,Desktop,Developing for the Desktop...
# 3

> Create a global set with all currently active

> mnemonics. When adding a new one to the GUI, check

> whether this set contains the same character or not.

the problem is that i am "not supposed" to know details about buttons and mnemnoics of the sub-view which I add to my frame...

i am only supposed to know about the functionality it provides, and plug it into my window and provide a context for it. How many buttons and so on is the sub-views' own responsibility, not the container (the JFrame)

and also: the sub-view can hide and show buttons dynamically according to model updates etc, the components/buttons that are visible when the view is first created may not be the same 5 minutes later...

shmokka at 2007-7-14 1:33:20 > top of Java-index,Desktop,Developing for the Desktop...