Custom Document for Comma Separated Values in a JTextField

Hi,

I tried to create a custom structured Document implementation for comma separated values (on one line) in a JTextField:

I want to have one LeafElement for each value (to store as an attribute the Object associated with this value). So, if I plug a DocumentListener to the JTextField, I can easily see which value(s) have been added/removed (see ElementChange).

I used the PlainDocument implementation as a starting point and replaced the logic related to the '\n' character by the ',' (see #insertUpdate).

Then I realized that the View must be customized too. So I created a View with a custom #paint method to draw each LeafElement on the same line.

But, now, I try to fix the ElementChange returned by the #insertUpdate and #removeUpdate methods.

Indeed, I played with a JTextArea with two lines "Bart\nisa" and plugged a debugging DocumentListener.

And the event I get when I type 'L' before "isa" is really strange:

- Two elements removed,

- Two elements added.

In the PlainDocument#insertUpdate, the code updates the 'offset' but I don't understand the logic behind.

Could you explain me why I got this result?

Could you give me an hint?

Best Regards,

El Barto.

[1273 byte] By [El_Bartoa] at [2007-10-3 9:22:10]
# 1

When you say the offset gets updated, I assume you're talking about this bit:

int offset = chng.getOffset();

int length = chng.getLength();

if (offset > 0) {

offset -= 1;

length += 1;

}

The result is that, if a character is added at the very beginning or end of a line, the edit gets treated as a multi-line edit, which means the LeafElements on either side of the edit get removed and reconstructed. I discovered this behavior a few years ago, and I never have figured out what purpose it serves. I think you'll just have to try overriding the method to eliminate that behavior, and see what happens.

uncle_alicea at 2007-7-15 4:35:46 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for this suggestion.
El_Bartoa at 2007-7-15 4:35:46 > top of Java-index,Desktop,Core GUI APIs...