1

I am using the jline.ConsoleReader provided and adding an ActionListner for a key (say pressing 'q') using

q. addTriggerAction('q', new ActionListener(){ System.out.println("Triggered"); });

However after a certain time I need to stop triggering the action and remove the listener. How can I do that? I might need to add a new different ActionListener to the same action later

Sach
  • 845
  • 3
  • 9
  • 22
  • 1
    You'd need a reference to the original `ActionListener`, instead of create an inline class like you are, create an `ActionListener` and assign it an instance variable you can reference later – MadProgrammer Nov 12 '14 at 04:01
  • @MadProgrammer thank you for the answer. But I didn't quite get what you meant. Can you please elaborate? – Sach Nov 12 '14 at 04:05
  • 1
    Create an instance field of `ActionListener`, `private ActionListener triggerActionListener;`, assign a reference of `ActionListener` to it `triggerActionListener = new ActionListener(){ System.out.println("Triggered"); };`, add this to the `q`, `q. addTriggerAction('q', triggerActionListener);`, when needed, use this reference to remove it... – MadProgrammer Nov 12 '14 at 04:07
  • Thank you very much. I think this answers my question – Sach Nov 12 '14 at 04:08
  • @MadProgrammer well that didn't solve my problem. How can I set the action to null or change it even if I have a reference? (Without creating a new ActionListener object that is ) I edited my question too – Sach Nov 12 '14 at 04:32
  • Can't you do something like `q.removeActionListener(triggerActionListener)` ? – MadProgrammer Nov 12 '14 at 04:35
  • Have you tried `q.addTriggerAction('q', null)`? – MadProgrammer Nov 12 '14 at 04:38
  • there's no removeActionListner method so that's why I asked the question. And I think setting it to null should work :) Thanks – Sach Nov 12 '14 at 05:03

0 Answers0