user733046
user733046

Reputation: 101

How to convert JTextField to String and String to JTextField?

How to convert JTextField to String and String to JTextField in Java?

Upvotes: 9

Views: 123841

Answers (4)

Darren Burgess
Darren Burgess

Reputation: 4312

JTextField allows us to getText() and setText() these are used to get and set the contents of the text field, for example.

text = texfield.getText();

hope this helps

Upvotes: 4

Bozho
Bozho

Reputation: 597016

// to string
String text = textField.getText();

// to JTextField
textField.setText(text);

You can also create a new text field: new JTextField(text)

Note that this is not conversion. You have two objects, where one has a property of the type of the other one, and you just set/get it.

Reference: javadocs of JTextField

Upvotes: 7

Harry Joy
Harry Joy

Reputation: 59650

how to convert JTextField to string and string to JTextField in java

If you mean how to get and set String from jTextField then you can use following methods:

String str = jTextField.getText() // get string from jtextfield

and

jTextField.setText(str)  // set string to jtextfield
//or
new JTextField(str)     // set string to jtextfield

You should check JavaDoc for JTextField

Upvotes: 15

Andreas Dolk
Andreas Dolk

Reputation: 114757

The JTextField offers a getText() and a setText() method - those are for getting and setting the content of the text field.

Upvotes: 4

Related Questions