3

In my current project, I am using AutoCompleteDecorate from SwingX. AutoCompleteDecorator.decorate(jComboBox1);

However, I want to override the backspace action. Originally, using AutoCompleteDecorate.decorate(JComboBox), pressing backspace move the selection in combobox to the left and doesn't delete the previous character. I want to implement the default function of backspace (which is to delete previous character) even if I AutoCompleteDecorate my JComboBox.

Please help me solve my problem. Thank you in advance.

Lion
  • 18,729
  • 22
  • 80
  • 110
dmfrl
  • 337
  • 8
  • 17
  • You may need to post some relevant code snippets especially precisely concise. – Lion Jan 24 '12 at 08:56
  • why the downvote? It's a completely clear question ... – kleopatra Jan 24 '12 at 09:19
  • @kleopatra that's same plus_one_point in the user_profile as up_vote – mKorbel Jan 24 '12 at 09:36
  • @user1157559 are you running some additional code from JComboBox, meaning SqlConnection or another long running taks ??? – mKorbel Jan 24 '12 at 09:38
  • @mKorbel ehhh ... don't understand what you mean – kleopatra Jan 24 '12 at 09:39
  • lots of peoples here up/down vote questions for taking Electorate badge, I saw similair invasion here on todays morning (CET Time) and Swing rellevant answerers preffered to close question(s) rather than down_voting (excuding Rob) – mKorbel Jan 24 '12 at 09:41
  • @mKorbel ahh ... thanks for the explanation :-) But as I said: the question is perfectly clear and makes sense (read: I don't know the answer, hehe) – kleopatra Jan 24 '12 at 09:48
  • I saw similair issue 2-3 weeks back, when Item/Action Listener calls another long running code ==> concurency, then Caret or selection HightLighter freeze, that reason why I asked my question to OP – mKorbel Jan 24 '12 at 09:54
  • @mKorbel nothing to do with concurrency - it's specific SwingX autocomplete behaviour :-) – kleopatra Jan 24 '12 at 09:57

1 Answers1

4

Not trivial - the decorator goes a long way to implement the select-instead-of-delete ;-)

First you need to define which behaviour you want. Then implement a Action that does as you intend and place it in the editor's ActionMap:

Action myBackspace = ...
ActionMap map = ((JComponent) decorateCombo.getEditor().getEditorComponent())
     .getActionMap();
map.put("nonstrict-backspace", myBackspace);

This is vague because because I can't know what exactly you want, best to look at the source of AutoComplete to get an idea of how to implement myBackspace

Edit

Just to elaborate a bit on the vagueness: my first thought was to simply re-install the default backspace binding like:

InputMap map = ((JComponent) decorateCombo.getEditor().getEditorComponent())
    .getInputMap();
map.put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_BACK_SPACE, 0), 
            DefaultEditorKit.deletePrevCharAction);

that's most probably not what is expected: assuming the caret is somewhere in the middle of a contained element in an editable combo, then the text from the caret to the end is selected, consequently the deletePrev deletes the selected not the prev char. Which might lead the way to implement the custom action: first clear the selection, then deletePrev, then check if the new word is in the list and re-select (or not). Hard to tell without knowing the requirement.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • _Thank you very much, actually i'm trying to bring back the default backspace binding since yesterday. Anyway, i'll take that as my guide._ – dmfrl Jan 25 '12 at 01:20