Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2006

Tip: Looking for answers? Try searching our database.

replacing text in JTextArea

Thread view: 
pine newsgroup - 31 Jul 2006 11:36 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 I
don't want to shift the whole text as soon as one input some characters,
but rather I want to replace them. I searched java docs, this and others
newsgroup, but I wasn't able to find out a replace function for input
characters.

What properties of JTextArea should I use to replace characters?
Any help appreciated.
Thanks
Knute Johnson - 31 Jul 2006 19:03 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,
[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

--


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.