How to convert JTextField to String and String to JTextField?

前端 未结 4 942
余生分开走
余生分开走 2020-12-20 19:00

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

相关标签:
4条回答
  • 2020-12-20 19:23

    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

    0 讨论(0)
  • 2020-12-20 19:30

    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

    0 讨论(0)
  • 2020-12-20 19:35
    // 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

    0 讨论(0)
  • 2020-12-20 19:37

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

    0 讨论(0)
提交回复
热议问题