Modifying Text in a JTextComponent: Replace the first 3 characters with some text : JTextComponent « Swing « Java Tutorial






import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTextComponent textComp = new JTextField("Initial Text");
    Document doc = textComp.getDocument();

    // Replace the first 3 characters with some text
    int pos = 0;
    int len = 3;
    doc.remove(pos, len);
    doc.insertString(pos, "new text", null);
  }
}








14.14.JTextComponent
14.14.1.JTextComponent: the parent class for all the components used as textual views.
14.14.2.Drawing in the Background of a ComponentDrawing in the Background of a Component
14.14.3.Loading and Saving ContentLoading and Saving Content
14.14.4.Accessing the System ClipboardAccessing the System Clipboard
14.14.5.Default Editor Kit: cutActionDefault Editor Kit: cutAction
14.14.6.Register Keyboard action: registerKeyboardActionRegister Keyboard action: registerKeyboardAction
14.14.7.CaretListener Interface and CaretEvent ClassCaretListener Interface and CaretEvent Class
14.14.8.Restricting Caret Movement: NavigationFilterRestricting Caret Movement: NavigationFilter
14.14.9.Remove Key action from Text componentRemove Key action from Text component
14.14.10.Listening to Text Components Events with an ActionListenerListening to Text Components Events with an ActionListener
14.14.11.Listening to JTextField Events with an KeyListenerListening to JTextField Events with an KeyListener
14.14.12.Share Document
14.14.13.Listening to Text Components Events with an InputVerifierListening to Text Components Events with an InputVerifier
14.14.14.TextAction Name Constants
14.14.15.Using Text Component ActionsUsing Text Component Actions
14.14.16.Listening to Text Components Events with a DocumentListenerListening to Text Components Events with a DocumentListener
14.14.17.Text Component PrintingText Component Printing
14.14.18.GIF Writer
14.14.19.Text component supports both cut, copy and paste (using the DefaultEditorKit's built-in actions) and drag and drop
14.14.20.Overriding the Default Action of a JTextComponent
14.14.21.Modifying Text in a JTextComponent: Insert some text at the beginning
14.14.22.Modifying Text in a JTextComponent: Insert some text after the 5th character
14.14.23.Modifying Text in a JTextComponent: Append some text
14.14.24.Modifying Text in a JTextComponent: Delete the first 5 characters
14.14.25.Modifying Text in a JTextComponent: Replace the first 3 characters with some text
14.14.26.Limiting the Capacity of a JTextComponent
14.14.27.Filter all editing operations on a text component
14.14.28.Enabling Text-Dragging on a JTextComponent
14.14.29.Enumerating All the Views in a JTextComponent
14.14.30.Sharing a Document Between JTextComponents
14.14.31.Highlight a word in JTextComponent
14.14.32.Remove Highlighting in a JTextComponent
14.14.33.Listening for Caret Movement Events in a JTextComponent
14.14.34.Listening for Editing Changes in a JTextComponent
14.14.35.Moving the Caret of a JTextComponent
14.14.36.Setting the Blink Rate of a JTextComponent's Caret
14.14.37.Using the Selection of a JTextComponent
14.14.38.Better way to set the selection
14.14.39.Set the color behind the selected text
14.14.40.Set the caret color
14.14.41.Document and DocumentFilter