Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a 'String' in a 'JTextField' to a specific position?

I want to insert a String in a JTextField. Something like-

myTextField.insert(text, position);

How can it be done? Is there any simple way to do this?

like image 494
Mad_Matt Avatar asked Jul 15 '12 16:07

Mad_Matt


People also ask

Can the program put text in JTextField?

The class JTextField is a component that allows editing of a single line of text.

How do I add text to a JFrame?

You need to create an instance of the JTextField class and add it to your panel and JFrame. Creating a new text field is the same as instantiating any object. The code here creates the JFrame, adds a label, and then adds a text field for the user to enter some information: JTextField textField = new JTextField();


1 Answers

You may do this-

jTextField.setText("This  swing.");    
jTextField.getDocument().insertString(5, "is", null);

And jTextField will show-

This is swing.

like image 59
Mohayemin Avatar answered Sep 20 '22 05:09

Mohayemin