Here's a generic-friendly version:
public class MapUtil {
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
List<Entry<K, V>> list = new ArrayList<>(map.entrySet());
list.sort(Entry.comparingByValue());
Map<K, V> result = new LinkedHashMap<>();
for (Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}
In answer to the question of keyboard chords specifically, I do not believe there is a ready-made option available to you at this point.
However, it should be simple enough to model. I would create a single class, perhaps KeyboardChordProvider
. It will need to know about keyboard events at the form level. As stated elsewhere, the Form.KeyPreview
property must be true
. It may be enough for this provider to subscribe to the Form.KeyPress
event. You could do all of this in the provider's constructor if you passed in the form.
You would need to register the potential keystrokes with the provider.
Internally this instance will track the current state. Whenever a keystroke is observed that represents the first key of the chord, you would update the provider's state and raise an event so that a subscriber could set text: (CTRL+W) was pressed. Waiting for second key of chord...
If the next keystroke matches a potential secondary option, then you have a match and could raise a ChordPressed
event containing the details of the strokes entered. Alternatively, you might just call a particular callback that was given to the provider at the time the chord was registered (to avoid having a switch statement or some other dispatch in the ChordPressed
event handler).
If, at any time, a keystroke does not match a potential next option, then you would reset the state of the provider.
Internal to the provider you might model the possible keystrokes using a tree structure. The current state of the provider is just a particular tree node. At the beginning, the root node would be active. If a child matches a keystroke then it becomes the current node in anticipation of the next stroke. If the child was a leaf node, then an entire chord has matched and you would raise ChordPressed
event (passing the chain of strokes that got you to that point) or invoke the callback stored in the leaf. Whenever no keystroke matches a child, reset back to making the root node active.
I think this design would achieve what you want.
Best Answer
Check out the How To Use Key Bindings section of the Java tutorial.
You need to create and register an
Action
with your component'sActionMap
and the register a (KeyStroke
, Action Name) pair in one of your application's component'sInputMap
s. Given that you don't have aJMenuBar
you could simply register the key bindings with a top-levelJPanel
in your application.For example: