Search code examples
javaswingeventsjmenursyntaxtextarea

JMenuBar Not Receiving Key Combinations From RSyntaxTextArea


I'm having trouble having keypress events from a textarea reach a JMenBar. I have the menu bar generated at runtime from an XML file and the text area is created like so:

@Override
public void createUI() {
    this.textArea = new RSyntaxTextArea(25, 70);
    this.scrollPane = new RTextScrollPane(this.textArea);
    this.setLayout(new BorderLayout());
    this.add(this.scrollPane, BorderLayout.CENTER);
    this.textArea.setSyntaxEditingStyle("text/dm");
}

RSyntaxTextArea textArea;
RTextScrollPane scrollPane;

In the items in the menu, I use setAccelerator() to set the key combinations to use them. When I use said combinations in the text area, they never reach the menu item. I don't remember having this problem before, any ideas why it's happening now?

Problem Solved (Didn't want to wait 6 hours to answer my own question.):

I solved my problem by keeping a list of the key mappings used for the menus and then making the text area ignore them using the input map of the JComponent class.

Code:

for(KeyStroke ks : this.mappedKeyStrokes) {
    component.getInputMap().put(ks, "none");
}

Solution

  • When I use said combinations in the text area, they never reach the menu item [...] any ideas why it's happening?

    That's by design (of keyBindings): menu bindings are basically handled via the componentInputMap (== inputMap of type WHEN_IN_FOCUSED_WINDOW). InputMaps are served in sequence

    • WHEN_FOCUSED
    • WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
    • WHEN_IN_FOCUSED_WINDOW

    passing to the next in line only if not handled before.