Updating a KeyEventDispatcher's private variables?
In my code, I have your typical:
manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
lmList =new DefaultListModel();
listFirst =new JList(lmList);
dispatcher =new MyDispatcher(listFirst, manager);
manager.addKeyEventDispatcher(dispatcher);
But I'd like to be able to
dispatcher.vSetLastClicked(listSecond);
AFTER THE FACT (after the "addKeyEventDispatcher"), so that the keyboard dispatcher will send all keypresses to the SECOND list instead of the first. Is this possible?
Or does "addKeyEventDispatcher" make a COPY of the referenced location I sent in?
I ask because it seems the answer is YES. I expected "dispatcher" to be a reference, whose member variables could be altered -- but my experimentation proves otherwise.
Any ideas?
Matthew
[940 byte] By [
CathInfoa] at [2007-11-27 5:35:08]

# 1
Here's what KeyboardFocusManager does:
public void addKeyEventDispatcher(KeyEventDispatcher dispatcher) {
if (dispatcher != null) {
synchronized (this) {
if (keyEventDispatchers == null) {
keyEventDispatchers = new java.util.LinkedList();
}
keyEventDispatchers.add(dispatcher);
}
}
}
Does this answer your question?
rebola at 2007-7-12 15:03:54 >

# 2
Thanks for the code snippet -- it is very helpful because it shows that addKeyEventDispatcher adds the ADDRESS YOU SEND IN to a linked list.
But I'm even more confused now, because I have been completely unable to modify member variables of my KeyEventDispatcher-derived class.
If only the address is added, I should be able to call member functions and change the member variables in that instance of MyDispatcher -- the one which was added to the linked list.
public class MyDispatcher implements KeyEventDispatcher {
JComponent comp;
KeyboardFocusManager manager;
/** Creates a new instance of MyDispatcher */
public MyDispatcher(JComponent c1, KeyboardFocusManager kfManager) {
this.comp = c1;
this.manager = kfManager;
}
public void vSetLastClicked(JComponent c1) {
this.comp = c1;
}
public boolean dispatchKeyEvent(KeyEvent e) {
boolean ynResult;
char ch;
try {
if (e.getSource().getClass().getName().compareToIgnoreCase("javax.swing.JDialog") == 0) {
manager.redispatchEvent(comp, e);
return true;
}
if (e.getSource() != comp) {
manager.redispatchEvent(comp, e);
return true;
}
} catch (Exception ex) {
}
finally {
return false;
}
}
}
# 3
I tested with the code you supplied, and it seems to work ok for me. What happens? Or what did you expect to happen?
Could it be that your naming the MyDispatcher instance 玠ispatcher?complicates things since, if you declare it in a Container, you override the event router named 玠ispatcher?in java.awt.Container? Maybe you should try with another variable name.
rebola at 2007-7-12 15:03:54 >

# 4
I tried changing the variable name from "dispatcher" to something unique.
Still, I get the same result.
The undesirable result is that my KeyEventDispatcher ALWAYS sends key traffic to the first component (which I initially set the private variable to) but even after I send in a reference to the SECOND component, the dispatcher doesn't send all traffic there instead (as it should).
Am I missing something?
Matthew