Swinger Hack: Blow your stack
Here's a common Swing problem. You're a XYZListener and you've just received a fresh new XYZEvent. So you make some changes to XYZ, coloring it blue, doubling its value and removing its tartar sauce attributes. Unfortunately this all fails with an unpleasant error:XYZ cannot be modified by XYZ's own Listener
Here's when this happens:
The recommended solution
All over the Internet forums, people hurt you by asking you to work around this problem in painful ways:
my 1337 hack solution
Instead of fighting the locks by changing your code, you can get outside of them by changing when your code executes: Post a Runnable the the event queue to solve the problem later! So although this code doesn't work:
public void changedUpdate(DocumentEvent e) {
String uppercase = myTextField.getText().toUpperCase();
myTextField.setText(uppercase);
}
This code changes the time that setText() is executed, to happen after the listeners have all been notified:
public void changedUpdate(DocumentEvent e) {
final String uppercase = myTextField.getText().toUpperCase();
// avoid infinite loops
if(uppercase.equals(myTextField.getText())) return;
// schedule update
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myTextField.setText(uppercase);
}
});
}
Note that although this hack will help you get your work done, it has some weaknesses when compared to the recommended solution: