Java have a int value using setText

后端 未结 7 987
梦如初夏
梦如初夏 2021-01-28 03:02

I\'m trying to set an int value using jTextField and the setText method. But of course setText wants a String. How do I get round this? I\'ll give you a snippet of the code:

相关标签:
7条回答
  • 2021-01-28 03:42

    I think you should write the code as

    seatsTF.setText(e.getNoOfSeats().toString());

    0 讨论(0)
  • 2021-01-28 03:45

    Setting an int converting it to a String not a big deal. Displaying a value is a problem. To take care of how the value is displayed properly in the textfield you may use a DecimalFormat to format the numeric value. But may be the number is locale specific then you need NumberFormat instance

    NumberFormat nf = NumberFormat.getInstance(locale);
    nf.setMaximumIntegerDigits(12);
    nf.setMaximumFractionDigits(0);
    nf.setMinimumFractionDigits(0);
    String s = nf.format(e.getNoOfSeats());
    seatsTF.setText(s);
    

    You may also need to read the tutorial on how to use the DecimalFormat.

    0 讨论(0)
  • 2021-01-28 03:51

    String#valueOf convert your int to String.

    String.valueOf(e.getAge()); will return the string representation of the int argument.

    seatsTF.setText(String.valueOf(e.Age()));
    ...
    
    0 讨论(0)
  • 2021-01-28 03:53

    To convert Integer Value to String you should

    MedicineTM medicine=tblmedicine.getSelectionModel().getSelectedItem();
    
        txtmedicine.setText(medicine.getMID());
        txtDescription.setText(medicine.getDescription());
        txtQty.setText(String.valueOf(medicine.getQty()));  // this is what i did
        cmbApproval.setValue(medicine.getApproval());
    
    0 讨论(0)
  • 2021-01-28 03:56

    Assuming age field is of type int, you could try something like:

            ageTF.setText( Integer.toString(e.getAge()) );
    
    0 讨论(0)
  • 2021-01-28 03:59

    Normal ways would be

    seatsTF.setText(Integer.toString(e.getNoOfSeats()));
    

    or

    seatsTF.setText(String.valueOf(e.getNoOfSeats()));
    

    but, this can be achieved with a concatenation like this:

    seatsTF.setText("" + e.getNoOfSeats());
    
    0 讨论(0)
提交回复
热议问题