> Hello, I'm using a JTextArea in an application were the text content
> initially is read from a file; after that one should modify the text,
[quoted text clipped - 6 lines]
> Any help appreciated.
> Thanks
In the AWT class, TextArea, if you press the Insert key it puts the
TextArea into replace mode. Press it again and it takes it out.
JTextArea does not have this feature. If you want to modify the text
that is entered into a JTextArea you need to create your own Document.
Below is an example of how to make a replace document. It is by no
means a complete example but it does sort of work.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class test9 {
public static void main(String[] args) {
class ReplaceDocument extends PlainDocument {
boolean insertMode;
public void insertString(int offset, String str,
AttributeSet a)
throws BadLocationException {
super.insertString(offset,str,a);
if (insertMode && offset+str.length() < getLength())
remove(offset+str.length(), str.length());
}
public void toggleInsert() {
insertMode = !insertMode;
}
}
class InsertKeyListener extends KeyAdapter {
ReplaceDocument doc;
public InsertKeyListener(ReplaceDocument doc) {
super();
this.doc = doc;
}
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_INSERT)
doc.toggleInsert();
}
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ReplaceDocument repDoc = new ReplaceDocument();
JTextArea ta = new JTextArea(repDoc,
"Now is the time for all good men",10,32);
ta.addKeyListener(new InsertKeyListener(repDoc));
f.add(ta);
f.pack();
f.setVisible(true);
}
}

Signature
Knute Johnson
email s/nospam/knute/
pine newsgroup - 01 Aug 2006 01:04 GMT
>> Hello, I'm using a JTextArea in an application were the text content
>> initially is read from a file; after that one should modify the text, but
[quoted text clipped - 13 lines]
> make a replace document. It is by no means a complete example but it does
> sort of work.
...
Tanks lot, that's just what I need
--