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?
The class JTextField is a component that allows editing of a single line of text.
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();
You may do this-
jTextField.setText("This swing.");
jTextField.getDocument().insertString(5, "is", null);
And jTextField
will show-
This is swing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With